1
1
use rspack_core:: { ConstDependency , RuntimeGlobals , RuntimeRequirementsDependency , SpanExt } ;
2
+ use rspack_error:: { Severity , TraceableError } ;
2
3
use swc_core:: {
3
- common:: Spanned ,
4
- ecma:: ast:: { CallExpr , Callee , Expr , Ident , UnaryExpr } ,
4
+ common:: { SourceFile , Span , Spanned } ,
5
+ ecma:: ast:: { CallExpr , Ident , UnaryExpr } ,
5
6
} ;
6
7
7
8
use crate :: {
8
9
dependency:: ModuleArgumentDependency ,
9
10
parser_plugin:: JavascriptParserPlugin ,
10
11
utils:: eval:: { self , BasicEvaluatedExpression } ,
11
- visitors:: { JavascriptParser , expr_matcher , expression_not_supported , extract_member_root } ,
12
+ visitors:: { JavascriptParser , create_traceable_error } ,
12
13
} ;
13
14
15
+ fn expression_not_supported (
16
+ file : & SourceFile ,
17
+ name : & str ,
18
+ is_call : bool ,
19
+ expr_span : Span ,
20
+ ) -> ( Box < TraceableError > , Box < ConstDependency > ) {
21
+ (
22
+ Box :: new (
23
+ create_traceable_error (
24
+ "Unsupported feature" . into ( ) ,
25
+ format ! (
26
+ "{name}{} is not supported by Rspack." ,
27
+ if is_call { "()" } else { "" }
28
+ ) ,
29
+ file,
30
+ expr_span. into ( ) ,
31
+ )
32
+ . with_severity ( Severity :: Warn )
33
+ . with_hide_stack ( Some ( true ) ) ,
34
+ ) ,
35
+ Box :: new ( ConstDependency :: new (
36
+ expr_span. into ( ) ,
37
+ "(void 0)" . into ( ) ,
38
+ None ,
39
+ ) ) ,
40
+ )
41
+ }
42
+
14
43
const WEBPACK_HASH : & str = "__webpack_hash__" ;
15
44
const WEBPACK_LAYER : & str = "__webpack_layer__" ;
16
45
const WEBPACK_PUBLIC_PATH : & str = "__webpack_public_path__" ;
@@ -318,54 +347,42 @@ impl JavascriptParserPlugin for APIPlugin {
318
347
& self ,
319
348
parser : & mut JavascriptParser ,
320
349
member_expr : & swc_core:: ecma:: ast:: MemberExpr ,
321
- _name : & str ,
350
+ for_name : & str ,
322
351
) -> Option < bool > {
323
- macro_rules! not_supported_expr {
324
- ( $check: ident, $expr: ident, $name: literal) => {
325
- if expr_matcher:: $check( $expr) {
326
- let ( warning, dep) = expression_not_supported( & parser. source_file, $name, $expr) ;
327
- parser. warning_diagnostics. push( warning) ;
328
- parser. presentational_dependencies. push( dep) ;
329
- return Some ( true ) ;
330
- }
331
- } ;
332
- }
333
-
334
- let expr = & swc_core:: ecma:: ast:: Expr :: Member ( member_expr. to_owned ( ) ) ;
335
-
336
- if let Some ( root) = extract_member_root ( expr)
337
- && let s = root. sym . as_str ( )
338
- && parser. is_unresolved_ident ( s)
352
+ if for_name == "require.extensions"
353
+ || for_name == "require.config"
354
+ || for_name == "require.version"
355
+ || for_name == "require.include"
356
+ || for_name == "require.onError"
357
+ || for_name == "require.main.require"
358
+ || for_name == "module.parent.require"
339
359
{
340
- if s == "require" {
341
- not_supported_expr ! ( is_require_extensions, expr, "require.extensions" ) ;
342
- not_supported_expr ! ( is_require_config, expr, "require.config" ) ;
343
- not_supported_expr ! ( is_require_version, expr, "require.version" ) ;
344
- not_supported_expr ! ( is_require_include, expr, "require.include" ) ;
345
- not_supported_expr ! ( is_require_onerror, expr, "require.onError" ) ;
346
- not_supported_expr ! ( is_require_main_require, expr, "require.main.require" ) ;
347
- } else if s == "module" {
348
- not_supported_expr ! ( is_module_parent_require, expr, "module.parent.require" ) ;
349
- }
360
+ let ( warning, dep) =
361
+ expression_not_supported ( parser. source_file , for_name, false , member_expr. span ( ) ) ;
362
+ parser. warning_diagnostics . push ( warning) ;
363
+ parser. presentational_dependencies . push ( dep) ;
364
+ return Some ( true ) ;
350
365
}
351
366
352
- if expr_matcher :: is_require_cache ( expr ) {
367
+ if for_name == "require.cache" {
353
368
parser
354
369
. presentational_dependencies
355
370
. push ( Box :: new ( ConstDependency :: new (
356
- expr . span ( ) . into ( ) ,
371
+ member_expr . span ( ) . into ( ) ,
357
372
RuntimeGlobals :: MODULE_CACHE . name ( ) . into ( ) ,
358
373
Some ( RuntimeGlobals :: MODULE_CACHE ) ,
359
374
) ) ) ;
360
- Some ( true )
361
- } else if expr_matcher:: is_require_main ( expr) {
375
+ return Some ( true ) ;
376
+ }
377
+
378
+ if for_name == "require.main" {
362
379
let mut runtime_requirements = RuntimeGlobals :: default ( ) ;
363
380
runtime_requirements. insert ( RuntimeGlobals :: MODULE_CACHE ) ;
364
381
runtime_requirements. insert ( RuntimeGlobals :: ENTRY_MODULE_ID ) ;
365
382
parser
366
383
. presentational_dependencies
367
384
. push ( Box :: new ( ConstDependency :: new (
368
- expr . span ( ) . into ( ) ,
385
+ member_expr . span ( ) . into ( ) ,
369
386
format ! (
370
387
"{}[{}]" ,
371
388
RuntimeGlobals :: MODULE_CACHE ,
@@ -374,8 +391,10 @@ impl JavascriptParserPlugin for APIPlugin {
374
391
. into ( ) ,
375
392
Some ( runtime_requirements) ,
376
393
) ) ) ;
377
- Some ( true )
378
- } else if expr_matcher:: is_webpack_module_id ( expr) {
394
+ return Some ( true ) ;
395
+ }
396
+
397
+ if for_name == "__webpack_module__.id" {
379
398
parser
380
399
. presentational_dependencies
381
400
. push ( Box :: new ( RuntimeRequirementsDependency :: new (
@@ -385,51 +404,32 @@ impl JavascriptParserPlugin for APIPlugin {
385
404
. presentational_dependencies
386
405
. push ( Box :: new ( ModuleArgumentDependency :: new (
387
406
Some ( "id" . into ( ) ) ,
388
- expr . span ( ) . into ( ) ,
407
+ member_expr . span ( ) . into ( ) ,
389
408
Some ( parser. source_map . clone ( ) ) ,
390
409
) ) ) ;
391
- Some ( true )
392
- } else {
393
- None
394
- }
395
- }
396
-
397
- fn call ( & self , parser : & mut JavascriptParser , call_expr : & CallExpr , _name : & str ) -> Option < bool > {
398
- macro_rules! not_supported_call {
399
- ( $check: ident, $name: literal) => {
400
- if let Callee :: Expr ( expr_box) = & call_expr. callee
401
- && let Expr :: Member ( expr) = & * * expr_box
402
- && expr_matcher:: $check( & Expr :: Member ( expr. to_owned( ) ) )
403
- {
404
- let ( warning, dep) = expression_not_supported(
405
- & parser. source_file,
406
- $name,
407
- & Expr :: Call ( call_expr. to_owned( ) ) ,
408
- ) ;
409
- parser. warning_diagnostics. push( warning) ;
410
- parser. presentational_dependencies. push( dep) ;
411
- return Some ( true ) ;
412
- }
413
- } ;
410
+ return Some ( true ) ;
414
411
}
415
412
416
- let root = call_expr
417
- . callee
418
- . as_expr ( )
419
- . and_then ( |expr| extract_member_root ( expr) ) ;
413
+ None
414
+ }
420
415
421
- if let Some ( root) = root
422
- && let s = root. sym . as_str ( )
423
- && parser. is_unresolved_ident ( s)
416
+ fn call (
417
+ & self ,
418
+ parser : & mut JavascriptParser ,
419
+ call_expr : & CallExpr ,
420
+ for_name : & str ,
421
+ ) -> Option < bool > {
422
+ if for_name == "require.config"
423
+ || for_name == "require.include"
424
+ || for_name == "require.onError"
425
+ || for_name == "require.main.require"
426
+ || for_name == "module.parent.require"
424
427
{
425
- if s == "require" {
426
- not_supported_call ! ( is_require_config, "require.config()" ) ;
427
- not_supported_call ! ( is_require_include, "require.include()" ) ;
428
- not_supported_call ! ( is_require_onerror, "require.onError()" ) ;
429
- not_supported_call ! ( is_require_main_require, "require.main.require()" ) ;
430
- } else if s == "module" {
431
- not_supported_call ! ( is_module_parent_require, "module.parent.require()" ) ;
432
- }
428
+ let ( warning, dep) =
429
+ expression_not_supported ( parser. source_file , for_name, true , call_expr. span ( ) ) ;
430
+ parser. warning_diagnostics . push ( warning) ;
431
+ parser. presentational_dependencies . push ( dep) ;
432
+ return Some ( true ) ;
433
433
}
434
434
435
435
None
0 commit comments