@@ -294,6 +294,7 @@ impl<'a> DependencyCollector<'a> {
294
294
kind : DependencyKind ,
295
295
source_type : SourceType ,
296
296
needs_stable_name : bool ,
297
+ optional : bool ,
297
298
) -> ast:: Expr {
298
299
// If not a library, replace with a require call pointing to a runtime that will resolve the url dynamically.
299
300
if !self . config . is_library && !self . config . standalone {
@@ -302,7 +303,7 @@ impl<'a> DependencyCollector<'a> {
302
303
span,
303
304
kind,
304
305
None ,
305
- false ,
306
+ optional ,
306
307
source_type,
307
308
needs_stable_name,
308
309
) ;
@@ -329,7 +330,7 @@ impl<'a> DependencyCollector<'a> {
329
330
)
330
331
} ;
331
332
let mut flags = DependencyFlags :: empty ( ) ;
332
- flags. set ( DependencyFlags :: OPTIONAL , span. is_dummy ( ) ) ;
333
+ flags. set ( DependencyFlags :: OPTIONAL , optional || span. is_dummy ( ) ) ;
333
334
flags. set ( DependencyFlags :: NEEDS_STABLE_NAME , needs_stable_name) ;
334
335
self . items . push ( DependencyDescriptor {
335
336
kind,
@@ -812,41 +813,48 @@ impl<'a> Fold for DependencyCollector<'a> {
812
813
} ;
813
814
let mut node = node. clone ( ) ;
814
815
815
- let ( specifier, span, needs_stable_name) = if let Some ( s) = self . match_new_url ( & arg. expr ) {
816
- s
817
- } else if let Lit ( ast:: Lit :: Str ( str_) ) = & * arg. expr {
818
- let ( msg, docs) = if kind == DependencyKind :: ServiceWorker {
819
- (
820
- "Registering service workers with a string literal is not supported." ,
821
- "https://parceljs.org/languages/javascript/#service-workers" ,
822
- )
816
+ let ( specifier, span, needs_stable_name, optional) =
817
+ if let Some ( s) = self . match_new_url ( & arg. expr ) {
818
+ s
819
+ } else if let Lit ( ast:: Lit :: Str ( str_) ) = & * arg. expr {
820
+ let ( msg, docs) = if kind == DependencyKind :: ServiceWorker {
821
+ (
822
+ "Registering service workers with a string literal is not supported." ,
823
+ "https://parceljs.org/languages/javascript/#service-workers" ,
824
+ )
825
+ } else {
826
+ (
827
+ "Registering worklets with a string literal is not supported." ,
828
+ "https://parceljs.org/languages/javascript/#worklets" ,
829
+ )
830
+ } ;
831
+ self . diagnostics . push ( Diagnostic {
832
+ message : msg. to_string ( ) ,
833
+ code_highlights : Some ( vec ! [ CodeHighlight {
834
+ message: None ,
835
+ loc: SourceLocation :: from( & self . source_map, str_. span) ,
836
+ } ] ) ,
837
+ hints : Some ( vec ! [ format!(
838
+ "Replace with: new URL('{}', import.meta.url)" ,
839
+ str_. value,
840
+ ) ] ) ,
841
+ show_environment : false ,
842
+ severity : DiagnosticSeverity :: Error ,
843
+ documentation_url : Some ( String :: from ( docs) ) ,
844
+ } ) ;
845
+ return node;
823
846
} else {
824
- (
825
- "Registering worklets with a string literal is not supported." ,
826
- "https://parceljs.org/languages/javascript/#worklets" ,
827
- )
847
+ return node;
828
848
} ;
829
- self . diagnostics . push ( Diagnostic {
830
- message : msg. to_string ( ) ,
831
- code_highlights : Some ( vec ! [ CodeHighlight {
832
- message: None ,
833
- loc: SourceLocation :: from( & self . source_map, str_. span) ,
834
- } ] ) ,
835
- hints : Some ( vec ! [ format!(
836
- "Replace with: new URL('{}', import.meta.url)" ,
837
- str_. value,
838
- ) ] ) ,
839
- show_environment : false ,
840
- severity : DiagnosticSeverity :: Error ,
841
- documentation_url : Some ( String :: from ( docs) ) ,
842
- } ) ;
843
- return node;
844
- } else {
845
- return node;
846
- } ;
847
849
848
- node. args [ 0 ] . expr =
849
- Box :: new ( self . add_url_dependency ( specifier, span, kind, source_type, needs_stable_name) ) ;
850
+ node. args [ 0 ] . expr = Box :: new ( self . add_url_dependency (
851
+ specifier,
852
+ span,
853
+ kind,
854
+ source_type,
855
+ needs_stable_name,
856
+ optional,
857
+ ) ) ;
850
858
851
859
match opts {
852
860
Some ( opts) => {
@@ -980,7 +988,7 @@ impl<'a> Fold for DependencyCollector<'a> {
980
988
981
989
if let Some ( args) = & node. args {
982
990
if !args. is_empty ( ) {
983
- let ( specifier, span, needs_stable_name) =
991
+ let ( specifier, span, needs_stable_name, optional ) =
984
992
if let Some ( s) = self . match_new_url ( & args[ 0 ] . expr ) {
985
993
s
986
994
} else if let Lit ( ast:: Lit :: Str ( str_) ) = & * args[ 0 ] . expr {
@@ -1019,6 +1027,7 @@ impl<'a> Fold for DependencyCollector<'a> {
1019
1027
DependencyKind :: WebWorker ,
1020
1028
source_type,
1021
1029
needs_stable_name,
1030
+ optional,
1022
1031
) ;
1023
1032
1024
1033
// Replace argument with a require call to resolve the URL at runtime.
@@ -1065,18 +1074,19 @@ impl<'a> Fold for DependencyCollector<'a> {
1065
1074
return expr;
1066
1075
}
1067
1076
1068
- if let Some ( ( specifier, span, needs_stable_name) ) = self . match_new_url ( & node) {
1077
+ if let Some ( ( specifier, span, needs_stable_name, optional ) ) = self . match_new_url ( & node) {
1069
1078
let url = self . add_url_dependency (
1070
1079
specifier,
1071
1080
span,
1072
1081
DependencyKind :: Url ,
1073
1082
self . config . source_type ,
1074
1083
needs_stable_name,
1084
+ optional,
1075
1085
) ;
1076
1086
1077
1087
// If this is a library, we will already have a URL object. Otherwise, we need to
1078
1088
// construct one from the string returned by the JSRuntime.
1079
- if !self . config . is_library && !self . config . standalone {
1089
+ if !self . config . is_library && !self . config . standalone && !optional {
1080
1090
return Expr :: New ( NewExpr {
1081
1091
span : DUMMY_SP ,
1082
1092
callee : Box :: new ( Expr :: Ident ( Ident :: new_no_ctxt ( "URL" . into ( ) , DUMMY_SP ) ) ) ,
@@ -1387,7 +1397,10 @@ impl Fold for PromiseTransformer {
1387
1397
}
1388
1398
1389
1399
impl < ' a > DependencyCollector < ' a > {
1390
- fn match_new_url ( & mut self , expr : & ast:: Expr ) -> Option < ( JsWord , swc_core:: common:: Span , bool ) > {
1400
+ fn match_new_url (
1401
+ & mut self ,
1402
+ expr : & ast:: Expr ,
1403
+ ) -> Option < ( JsWord , swc_core:: common:: Span , bool , bool ) > {
1391
1404
use ast:: * ;
1392
1405
1393
1406
if let Expr :: New ( new) = expr {
@@ -1409,7 +1422,7 @@ impl<'a> DependencyCollector<'a> {
1409
1422
1410
1423
if let Some ( arg) = args. get ( 1 ) {
1411
1424
if self . is_import_meta_url ( & arg. expr ) {
1412
- return Some ( ( specifier, span, false ) ) ;
1425
+ return Some ( ( specifier, span, false , false ) ) ;
1413
1426
}
1414
1427
}
1415
1428
}
@@ -1430,7 +1443,7 @@ impl<'a> DependencyCollector<'a> {
1430
1443
false
1431
1444
} ;
1432
1445
1433
- return Some ( ( specifier, span, needs_stable_name) ) ;
1446
+ return Some ( ( specifier, span, needs_stable_name, true ) ) ;
1434
1447
}
1435
1448
}
1436
1449
}
@@ -1443,7 +1456,7 @@ impl<'a> DependencyCollector<'a> {
1443
1456
Expr :: Member ( member) => member. span ,
1444
1457
_ => unreachable ! ( ) ,
1445
1458
} ;
1446
- return Some ( ( specifier. into ( ) , span, false ) ) ;
1459
+ return Some ( ( specifier. into ( ) , span, false , false ) ) ;
1447
1460
}
1448
1461
1449
1462
None
0 commit comments