@@ -3,6 +3,7 @@ pub mod async_module;
3
3
pub mod cjs;
4
4
pub mod constant_condition;
5
5
pub mod constant_value;
6
+ pub mod dynamic_expression;
6
7
pub mod esm;
7
8
pub mod node;
8
9
pub mod pattern_mapping;
@@ -123,6 +124,7 @@ use crate::{
123
124
references:: {
124
125
async_module:: { AsyncModule , OptionAsyncModule } ,
125
126
cjs:: { CjsRequireAssetReference , CjsRequireCacheAccess , CjsRequireResolveAssetReference } ,
127
+ dynamic_expression:: DynamicExpression ,
126
128
esm:: { module_id:: EsmModuleIdAssetReference , EsmBinding , UrlRewriteBehavior } ,
127
129
node:: PackageJsonReference ,
128
130
require_context:: { RequireContextAssetReference , RequireContextMap } ,
@@ -337,6 +339,7 @@ struct AnalysisState<'a> {
337
339
first_import_meta : bool ,
338
340
tree_shaking_mode : Option < TreeShakingMode > ,
339
341
import_externals : bool ,
342
+ ignore_dynamic_requests : bool ,
340
343
}
341
344
342
345
impl < ' a > AnalysisState < ' a > {
@@ -774,6 +777,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
774
777
first_import_meta : true ,
775
778
tree_shaking_mode : options. tree_shaking_mode ,
776
779
import_externals : options. import_externals ,
780
+ ignore_dynamic_requests : options. ignore_dynamic_requests ,
777
781
} ;
778
782
779
783
enum Action {
@@ -1063,7 +1067,10 @@ pub(crate) async fn analyse_ecmascript_module_internal(
1063
1067
DiagnosticId :: Lint (
1064
1068
errors:: failed_to_analyse:: ecmascript:: NEW_URL_IMPORT_META . to_string ( ) ,
1065
1069
) ,
1066
- )
1070
+ ) ;
1071
+ if options. ignore_dynamic_requests {
1072
+ continue ;
1073
+ }
1067
1074
}
1068
1075
analysis. add_reference ( UrlAssetReference :: new (
1069
1076
origin,
@@ -1131,6 +1138,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1131
1138
origin,
1132
1139
source,
1133
1140
compile_time_info,
1141
+ ignore_dynamic_requests,
1134
1142
..
1135
1143
} = state;
1136
1144
fn explain_args ( args : & [ JsValue ] ) -> ( String , String ) {
@@ -1186,7 +1194,13 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1186
1194
DiagnosticId :: Lint (
1187
1195
errors:: failed_to_analyse:: ecmascript:: DYNAMIC_IMPORT . to_string ( ) ,
1188
1196
) ,
1189
- )
1197
+ ) ;
1198
+ if ignore_dynamic_requests {
1199
+ analysis. add_code_gen ( DynamicExpression :: new_promise ( Vc :: cell (
1200
+ ast_path. to_vec ( ) ,
1201
+ ) ) ) ;
1202
+ return Ok ( ( ) ) ;
1203
+ }
1190
1204
}
1191
1205
analysis. add_reference ( EsmAsyncAssetReference :: new (
1192
1206
origin,
@@ -1219,7 +1233,11 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1219
1233
DiagnosticId :: Lint (
1220
1234
errors:: failed_to_analyse:: ecmascript:: REQUIRE . to_string ( ) ,
1221
1235
) ,
1222
- )
1236
+ ) ;
1237
+ if ignore_dynamic_requests {
1238
+ analysis. add_code_gen ( DynamicExpression :: new ( Vc :: cell ( ast_path. to_vec ( ) ) ) ) ;
1239
+ return Ok ( ( ) ) ;
1240
+ }
1223
1241
}
1224
1242
analysis. add_reference ( CjsRequireAssetReference :: new (
1225
1243
origin,
@@ -1262,7 +1280,11 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1262
1280
DiagnosticId :: Lint (
1263
1281
errors:: failed_to_analyse:: ecmascript:: REQUIRE_RESOLVE . to_string ( ) ,
1264
1282
) ,
1265
- )
1283
+ ) ;
1284
+ if ignore_dynamic_requests {
1285
+ analysis. add_code_gen ( DynamicExpression :: new ( Vc :: cell ( ast_path. to_vec ( ) ) ) ) ;
1286
+ return Ok ( ( ) ) ;
1287
+ }
1266
1288
}
1267
1289
analysis. add_reference ( CjsRequireResolveAssetReference :: new (
1268
1290
origin,
@@ -1327,7 +1349,10 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1327
1349
DiagnosticId :: Lint (
1328
1350
errors:: failed_to_analyse:: ecmascript:: FS_METHOD . to_string ( ) ,
1329
1351
) ,
1330
- )
1352
+ ) ;
1353
+ if ignore_dynamic_requests {
1354
+ return Ok ( ( ) ) ;
1355
+ }
1331
1356
}
1332
1357
analysis. add_reference ( FileSourceReference :: new ( source, Pattern :: new ( pat) ) ) ;
1333
1358
return Ok ( ( ) ) ;
@@ -1367,7 +1392,10 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1367
1392
DiagnosticId :: Lint (
1368
1393
errors:: failed_to_analyse:: ecmascript:: PATH_METHOD . to_string ( ) ,
1369
1394
) ,
1370
- )
1395
+ ) ;
1396
+ if ignore_dynamic_requests {
1397
+ return Ok ( ( ) ) ;
1398
+ }
1371
1399
}
1372
1400
analysis. add_reference ( FileSourceReference :: new ( source, Pattern :: new ( pat) ) ) ;
1373
1401
return Ok ( ( ) ) ;
@@ -1398,7 +1426,10 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1398
1426
DiagnosticId :: Lint (
1399
1427
errors:: failed_to_analyse:: ecmascript:: PATH_METHOD . to_string ( ) ,
1400
1428
) ,
1401
- )
1429
+ ) ;
1430
+ if ignore_dynamic_requests {
1431
+ return Ok ( ( ) ) ;
1432
+ }
1402
1433
}
1403
1434
analysis. add_reference ( DirAssetReference :: new ( source, Pattern :: new ( pat) ) ) ;
1404
1435
return Ok ( ( ) ) ;
@@ -1419,17 +1450,27 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1419
1450
JsValue :: member ( Box :: new ( args[ 1 ] . clone ( ) ) , Box :: new ( 0_f64 . into ( ) ) ) ;
1420
1451
let first_arg = state. link_value ( first_arg, in_try) . await ?;
1421
1452
let pat = js_value_to_pattern ( & first_arg) ;
1422
- if !pat. has_constant_parts ( ) {
1453
+ let dynamic = !pat. has_constant_parts ( ) ;
1454
+ if dynamic {
1423
1455
show_dynamic_warning = true ;
1424
1456
}
1425
- analysis. add_reference ( CjsAssetReference :: new (
1426
- origin,
1427
- Request :: parse ( Value :: new ( pat) ) ,
1428
- issue_source ( source, span) ,
1429
- in_try,
1430
- ) ) ;
1457
+ if !dynamic || !ignore_dynamic_requests {
1458
+ analysis. add_reference ( CjsAssetReference :: new (
1459
+ origin,
1460
+ Request :: parse ( Value :: new ( pat) ) ,
1461
+ issue_source ( source, span) ,
1462
+ in_try,
1463
+ ) ) ;
1464
+ }
1465
+ }
1466
+ let dynamic = !pat. has_constant_parts ( ) ;
1467
+ if dynamic {
1468
+ show_dynamic_warning = true ;
1469
+ }
1470
+ if !dynamic || !ignore_dynamic_requests {
1471
+ analysis. add_reference ( FileSourceReference :: new ( source, Pattern :: new ( pat) ) ) ;
1431
1472
}
1432
- if show_dynamic_warning || !pat . has_constant_parts ( ) {
1473
+ if show_dynamic_warning {
1433
1474
let ( args, hints) = explain_args ( & args) ;
1434
1475
handler. span_warn_with_code (
1435
1476
span,
@@ -1439,7 +1480,6 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1439
1480
) ,
1440
1481
) ;
1441
1482
}
1442
- analysis. add_reference ( FileSourceReference :: new ( source, Pattern :: new ( pat) ) ) ;
1443
1483
return Ok ( ( ) ) ;
1444
1484
}
1445
1485
let ( args, hints) = explain_args ( & args) ;
@@ -1465,6 +1505,9 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1465
1505
errors:: failed_to_analyse:: ecmascript:: CHILD_PROCESS_SPAWN . to_string ( ) ,
1466
1506
) ,
1467
1507
) ;
1508
+ if ignore_dynamic_requests {
1509
+ return Ok ( ( ) ) ;
1510
+ }
1468
1511
}
1469
1512
analysis. add_reference ( CjsAssetReference :: new (
1470
1513
origin,
@@ -1499,6 +1542,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1499
1542
errors:: failed_to_analyse:: ecmascript:: NODE_PRE_GYP_FIND . to_string ( ) ,
1500
1543
) ,
1501
1544
) ;
1545
+ // Always ignore this dynamic request
1502
1546
return Ok ( ( ) ) ;
1503
1547
}
1504
1548
analysis. add_reference ( NodePreGypConfigReference :: new (
@@ -1588,6 +1632,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
1588
1632
errors:: failed_to_analyse:: ecmascript:: NODE_EXPRESS . to_string ( ) ,
1589
1633
) ,
1590
1634
) ;
1635
+ // Always ignore this dynamic request
1591
1636
return Ok ( ( ) ) ;
1592
1637
}
1593
1638
match s {
@@ -2187,19 +2232,29 @@ async fn value_visitor_inner(
2187
2232
if * compile_time_info. environment ( ) . node_externals ( ) . await ? {
2188
2233
// TODO check externals
2189
2234
match & * * name {
2190
- "path" => JsValue :: WellKnownObject ( WellKnownObjectKind :: PathModule ) ,
2191
- "fs/promises" => JsValue :: WellKnownObject ( WellKnownObjectKind :: FsModule ) ,
2192
- "fs" => JsValue :: WellKnownObject ( WellKnownObjectKind :: FsModule ) ,
2193
- "child_process" => JsValue :: WellKnownObject ( WellKnownObjectKind :: ChildProcess ) ,
2194
- "os" => JsValue :: WellKnownObject ( WellKnownObjectKind :: OsModule ) ,
2195
- "process" => JsValue :: WellKnownObject ( WellKnownObjectKind :: NodeProcess ) ,
2235
+ "node:path" | "path" => {
2236
+ JsValue :: WellKnownObject ( WellKnownObjectKind :: PathModule )
2237
+ }
2238
+ "node:fs/promises" | "fs/promises" => {
2239
+ JsValue :: WellKnownObject ( WellKnownObjectKind :: FsModule )
2240
+ }
2241
+ "node:fs" | "fs" => JsValue :: WellKnownObject ( WellKnownObjectKind :: FsModule ) ,
2242
+ "node:child_process" | "child_process" => {
2243
+ JsValue :: WellKnownObject ( WellKnownObjectKind :: ChildProcess )
2244
+ }
2245
+ "node:os" | "os" => JsValue :: WellKnownObject ( WellKnownObjectKind :: OsModule ) ,
2246
+ "node:process" | "process" => {
2247
+ JsValue :: WellKnownObject ( WellKnownObjectKind :: NodeProcess )
2248
+ }
2196
2249
"@mapbox/node-pre-gyp" => {
2197
2250
JsValue :: WellKnownObject ( WellKnownObjectKind :: NodePreGyp )
2198
2251
}
2199
2252
"node-gyp-build" => {
2200
2253
JsValue :: WellKnownFunction ( WellKnownFunctionKind :: NodeGypBuild )
2201
2254
}
2202
- "bindings" => JsValue :: WellKnownFunction ( WellKnownFunctionKind :: NodeBindings ) ,
2255
+ "node:bindings" | "bindings" => {
2256
+ JsValue :: WellKnownFunction ( WellKnownFunctionKind :: NodeBindings )
2257
+ }
2203
2258
"express" => JsValue :: WellKnownFunction ( WellKnownFunctionKind :: NodeExpress ) ,
2204
2259
"strong-globalize" => {
2205
2260
JsValue :: WellKnownFunction ( WellKnownFunctionKind :: NodeStrongGlobalize )
0 commit comments