@@ -36,11 +36,12 @@ impl ModuleLexer {
36
36
self . reexports . clear ( ) ;
37
37
}
38
38
39
- fn reset ( & mut self , expr : & Expr ) {
39
+ fn replace_exports_from_expr ( & mut self , expr : & Expr ) {
40
40
if let Expr :: Paren ( ParenExpr { expr, .. } ) = expr {
41
- self . reset ( expr) ;
41
+ self . replace_exports_from_expr ( expr) ;
42
42
return ;
43
43
}
44
+
44
45
if let Some ( reexport) = self . as_reexport ( expr) {
45
46
self . clear ( ) ;
46
47
self . reexports . insert ( reexport) ;
@@ -72,6 +73,8 @@ impl ModuleLexer {
72
73
}
73
74
}
74
75
}
76
+ } else if let Expr :: Assign ( assign) = expr {
77
+ self . replace_exports_from_expr ( & assign. right ) ;
75
78
}
76
79
}
77
80
@@ -101,7 +104,7 @@ impl ModuleLexer {
101
104
}
102
105
}
103
106
Expr :: Call ( call) => {
104
- if let Some ( file) = is_require_call ( & call) {
107
+ if let Some ( file) = with_require_call ( & call) {
105
108
self . idents . insert ( name. into ( ) , IdentKind :: Reexport ( file) ) ;
106
109
}
107
110
}
@@ -244,7 +247,7 @@ impl ModuleLexer {
244
247
fn as_reexport ( & self , expr : & Expr ) -> Option < String > {
245
248
match expr {
246
249
Expr :: Paren ( ParenExpr { expr, .. } ) => return self . as_reexport ( expr) ,
247
- Expr :: Call ( call) => is_require_call ( & call) ,
250
+ Expr :: Call ( call) => with_require_call ( & call) ,
248
251
Expr :: Ident ( id) => {
249
252
if let Some ( value) = self . idents . get ( id. sym . as_ref ( ) ) {
250
253
match value {
@@ -328,7 +331,7 @@ impl ModuleLexer {
328
331
}
329
332
}
330
333
Expr :: Call ( call) => {
331
- if let Some ( reexport) = is_require_call ( call) {
334
+ if let Some ( reexport) = with_require_call ( call) {
332
335
self . reexports . insert ( reexport) ;
333
336
}
334
337
}
@@ -487,46 +490,50 @@ impl ModuleLexer {
487
490
488
491
fn get_exports_from_assign ( & mut self , assign : & AssignExpr ) {
489
492
if assign. op == AssignOp :: Assign {
490
- let member = if let AssignTarget :: Simple ( simple) = & assign. left {
491
- if let SimpleAssignTarget :: Member ( member) = & simple {
492
- Some ( member)
493
- } else {
494
- None
495
- }
496
- } else {
497
- None
498
- } ;
499
- if let Some ( MemberExpr { obj, prop, .. } ) = member {
500
- let prop = get_prop_name ( & prop) ;
501
- if let Some ( prop) = prop {
502
- match obj. as_ref ( ) {
503
- Expr :: Ident ( obj) => {
504
- let obj_name = obj. sym . as_ref ( ) ;
505
- if self . is_exports_ident ( obj_name) {
506
- // exports.foo = 'bar'
507
- self . named_exports . insert ( prop) ;
508
- if let Expr :: Assign ( dep_assign) = assign. right . as_ref ( ) {
509
- self . get_exports_from_assign ( dep_assign) ;
493
+ if let AssignTarget :: Simple ( simple) = & assign. left {
494
+ if let SimpleAssignTarget :: Member ( MemberExpr { obj, prop, .. } ) = & simple {
495
+ let prop = get_prop_name ( & prop) ;
496
+ if let Some ( prop) = prop {
497
+ match obj. as_ref ( ) {
498
+ Expr :: Ident ( obj) => {
499
+ let obj_name = obj. sym . as_ref ( ) ;
500
+ if self . is_exports_ident ( obj_name) {
501
+ // exports.foo = 'bar'
502
+ self . named_exports . insert ( prop) ;
503
+ if let Expr :: Assign ( right_as_assign) = assign. right . as_ref ( ) {
504
+ self . get_exports_from_assign ( right_as_assign) ;
505
+ }
506
+ return ;
507
+ } else if obj_name. eq ( "module" ) && self . is_exports_ident ( & prop) {
508
+ // module.exports = ??
509
+ self . replace_exports_from_expr ( assign. right . as_ref ( ) ) ;
510
+ return ;
510
511
}
511
- } else if obj_name. eq ( "module" ) && self . is_exports_ident ( & prop) {
512
- // module.exports = ??
513
- let right_expr = assign. right . as_ref ( ) ;
514
- self . reset ( right_expr)
515
512
}
516
- }
517
- Expr :: Member ( _) => {
518
- if is_member ( obj, "module" , "exports" ) {
519
- self . named_exports . insert ( prop) ;
513
+ // module.exports.foo = 'bar'
514
+ Expr :: Member ( _) => {
515
+ if is_member ( obj, "module" , "exports" ) {
516
+ self . named_exports . insert ( prop) ;
517
+ if let Expr :: Assign ( right_as_assign) = assign. right . as_ref ( ) {
518
+ self . get_exports_from_assign ( right_as_assign) ;
519
+ }
520
+ return ;
521
+ }
520
522
}
523
+ _ => { }
521
524
}
522
- _ => { }
523
525
}
524
- }
525
- } else {
526
- if let Some ( name) = self . get_export_name_of_call_arg ( assign. right . as_ref ( ) ) {
527
- self . named_exports . insert ( name) ;
526
+ } else if let SimpleAssignTarget :: Ident ( id) = & simple {
527
+ if self . is_exports_ident ( id. sym . as_ref ( ) ) {
528
+ // exports = ??
529
+ self . replace_exports_from_expr ( assign. right . as_ref ( ) ) ;
530
+ return
531
+ }
528
532
}
529
533
}
534
+ if let Some ( name) = self . get_export_name_of_call_arg ( assign. right . as_ref ( ) ) {
535
+ self . named_exports . insert ( name) ;
536
+ }
530
537
}
531
538
}
532
539
@@ -712,7 +719,7 @@ impl ModuleLexer {
712
719
return 0 ;
713
720
}
714
721
Expr :: Call ( call) => {
715
- if let Some ( body) = is_iife_call ( call) {
722
+ if let Some ( body) = get_iife_body ( call) {
716
723
return self . get_webpack_require_props_from_stmts ( & body, webpack_require_sym) ;
717
724
}
718
725
return 0 ;
@@ -969,6 +976,7 @@ impl ModuleLexer {
969
976
// module.exports = { foo: 'bar' }
970
977
// module.exports = { ...require('a'), ...require('b') }
971
978
// module.exports = require('lib')
979
+ // exports = module.exports = { foo: 'bar' }
972
980
// foo = exports.foo || (exports.foo = {})
973
981
Expr :: Assign ( assign) => {
974
982
self . get_exports_from_assign ( & assign) ;
@@ -982,6 +990,7 @@ impl ModuleLexer {
982
990
// Object.assign(module, { exports: { foo: 'bar' } })
983
991
// Object.assign(module, { exports: require('lib') })
984
992
// (function() { ... })()
993
+ // (function() { ... }).call(this)
985
994
// require("tslib").__exportStar(..., exports)
986
995
// tslib.__exportStar(..., exports)
987
996
// __exportStar(..., exports)
@@ -1028,7 +1037,7 @@ impl ModuleLexer {
1028
1037
}
1029
1038
if is_module {
1030
1039
if let Some ( expr) = with_value {
1031
- self . reset ( & expr) ;
1040
+ self . replace_exports_from_expr ( & expr) ;
1032
1041
}
1033
1042
}
1034
1043
} else if is_object_static_mothod_call ( & call, "assign" ) && call. args . len ( ) >= 2 {
@@ -1052,7 +1061,7 @@ impl ModuleLexer {
1052
1061
}
1053
1062
}
1054
1063
if let Some ( exports_expr) = with_exports {
1055
- self . reset ( & exports_expr) ;
1064
+ self . replace_exports_from_expr ( & exports_expr) ;
1056
1065
}
1057
1066
} else if is_exports {
1058
1067
self . use_object_as_exports ( props) ;
@@ -1080,7 +1089,7 @@ impl ModuleLexer {
1080
1089
}
1081
1090
} else if let Some ( body) = self . is_umd_iife_call ( & call) {
1082
1091
self . walk_body ( body, false ) ;
1083
- } else if let Some ( body) = is_iife_call ( & call) {
1092
+ } else if let Some ( body) = get_iife_body ( & call) {
1084
1093
for arg in & call. args {
1085
1094
if arg. spread . is_none ( ) {
1086
1095
// (function() { ... })(exports.foo || (exports.foo = {}))
@@ -1099,7 +1108,7 @@ impl ModuleLexer {
1099
1108
if let Expr :: Call ( call) = arg. as_ref ( ) {
1100
1109
if let Some ( body) = self . is_umd_iife_call ( & call) {
1101
1110
self . walk_body ( body, false ) ;
1102
- } else if let Some ( body) = is_iife_call ( & call) {
1111
+ } else if let Some ( body) = get_iife_body ( & call) {
1103
1112
// (function() { ... })(exports.foo || (exports.foo = {}))
1104
1113
for arg in & call. args {
1105
1114
if arg. spread . is_none ( ) {
@@ -1128,7 +1137,7 @@ impl ModuleLexer {
1128
1137
Expr :: Bin ( BinExpr { left, op, right, .. } ) => {
1129
1138
if matches ! ( op, BinaryOp :: LogicalAnd ) {
1130
1139
if let Expr :: Call ( call) = right. as_ref ( ) {
1131
- if let Some ( body) = is_iife_call ( & call) {
1140
+ if let Some ( body) = get_iife_body ( & call) {
1132
1141
if self . is_true ( left) {
1133
1142
for arg in & call. args {
1134
1143
if arg. spread . is_none ( ) {
@@ -1177,7 +1186,7 @@ impl ModuleLexer {
1177
1186
Stmt :: Return ( ReturnStmt { arg, .. } ) => {
1178
1187
self . fn_returned = true ;
1179
1188
if let Some ( arg) = arg {
1180
- self . reset ( arg) ;
1189
+ self . replace_exports_from_expr ( arg) ;
1181
1190
}
1182
1191
}
1183
1192
_ => { }
@@ -1404,7 +1413,7 @@ impl ModuleLexer {
1404
1413
if let Expr :: Seq ( SeqExpr { exprs, .. } ) = & * * arg {
1405
1414
if let Some ( expr) = exprs. get ( 0 ) {
1406
1415
if let Expr :: Call ( call) = & * * expr {
1407
- if let Some ( stmts) = is_iife_call ( call) {
1416
+ if let Some ( stmts) = get_iife_body ( call) {
1408
1417
self . get_webpack_exports ( & stmts, & webpack_require_sym, & 0 ) ;
1409
1418
}
1410
1419
}
@@ -1421,7 +1430,7 @@ impl ModuleLexer {
1421
1430
if let Some ( module_exports_expr) = exprs. get ( exprs. len ( ) - 1 ) {
1422
1431
if let Some ( module_iife_expr) = exprs. get ( 0 ) {
1423
1432
if let Expr :: Call ( module_iife_call_expr) = & * * module_iife_expr {
1424
- if let Some ( stmts) = is_iife_call ( module_iife_call_expr) {
1433
+ if let Some ( stmts) = get_iife_body ( module_iife_call_expr) {
1425
1434
if let Expr :: Ident ( Ident {
1426
1435
sym : module_exports_sym,
1427
1436
..
@@ -1564,7 +1573,7 @@ fn with_expr_callee(call: &CallExpr) -> Option<&Expr> {
1564
1573
}
1565
1574
1566
1575
// require('lib')
1567
- fn is_require_call ( call : & CallExpr ) -> Option < String > {
1576
+ fn with_require_call ( call : & CallExpr ) -> Option < String > {
1568
1577
if let Some ( Expr :: Ident ( id) ) = with_expr_callee ( call) {
1569
1578
if id. sym . as_ref ( ) . eq ( "require" ) && call. args . len ( ) > 0 {
1570
1579
return match call. args [ 0 ] . expr . as_ref ( ) {
@@ -1758,10 +1767,25 @@ fn is_umd_checks(stmts: &Vec<Stmt>) -> bool {
1758
1767
}
1759
1768
}
1760
1769
1761
- fn is_iife_call ( call : & CallExpr ) -> Option < Vec < Stmt > > {
1770
+ fn get_iife_body ( call : & CallExpr ) -> Option < Vec < Stmt > > {
1762
1771
let expr = if let Some ( callee) = with_expr_callee ( call) {
1763
1772
match callee {
1764
1773
Expr :: Paren ( ParenExpr { expr, .. } ) => expr. as_ref ( ) ,
1774
+ // (function() { ... }).call(this)
1775
+ Expr :: Member ( MemberExpr { obj, prop, .. } ) => {
1776
+ if let Some ( prop_name) = get_prop_name ( prop) {
1777
+ if !prop_name. eq ( "call" ) {
1778
+ return None ;
1779
+ }
1780
+ } else {
1781
+ return None ;
1782
+ }
1783
+ if let Expr :: Paren ( ParenExpr { expr, .. } ) = obj. as_ref ( ) {
1784
+ expr. as_ref ( )
1785
+ } else {
1786
+ return None ;
1787
+ }
1788
+ }
1765
1789
_ => callee,
1766
1790
}
1767
1791
} else {
0 commit comments