@@ -21,81 +21,104 @@ export function rewriteExportsAndGetExportsBlock(
21
21
code ,
22
22
uses ,
23
23
HELPERS_NAME ,
24
- id
24
+ id ,
25
+ replacesModuleExports
25
26
) {
26
- const exportDeclarations = [
27
- `export { exports as __moduleExports } from ${ JSON . stringify ( wrapId ( id , MODULE_SUFFIX ) ) } `
28
- ] ;
29
- const moduleExportsPropertyAssignments = [ ] ;
27
+ const exportDeclarations = [ ] ;
30
28
31
- if ( wrapped ) {
32
- if ( defineCompiledEsmExpressions . length > 0 || code . indexOf ( '__esModule' ) >= 0 ) {
33
- // eslint-disable-next-line no-param-reassign
34
- uses . commonjsHelpers = true ;
35
- exportDeclarations . push (
36
- `export default /*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ moduleName } .exports);`
37
- ) ;
29
+ // TODO Lukas maybe introduce an export mode with
30
+ // - 'replace'
31
+ // - 'wrapped'
32
+ // - 'module'
33
+ // - 'exports'
34
+ // then consider extracting parts that "getExportDeclarations"
35
+ if ( replacesModuleExports ) {
36
+ if ( topLevelModuleExportsAssignments . length > 0 ) {
37
+ for ( const { left } of topLevelModuleExportsAssignments ) {
38
+ magicString . overwrite ( left . start , left . end , `var ${ moduleName } ` ) ;
39
+ }
38
40
} else {
39
- exportDeclarations . push ( `export default ${ moduleName } .exports ;`) ;
41
+ exportDeclarations . unshift ( `var ${ moduleName } = {} ;`) ;
40
42
}
43
+ exportDeclarations . push (
44
+ `export { ${ moduleName } as __moduleExports };` ,
45
+ `export { ${ moduleName } as default };`
46
+ ) ;
41
47
} else {
42
- let deconflictedDefaultExportName ;
48
+ exportDeclarations . push (
49
+ `export { exports as __moduleExports } from ${ JSON . stringify ( wrapId ( id , MODULE_SUFFIX ) ) } `
50
+ ) ;
51
+ if ( wrapped ) {
52
+ if ( defineCompiledEsmExpressions . length > 0 || code . indexOf ( '__esModule' ) >= 0 ) {
53
+ // eslint-disable-next-line no-param-reassign
54
+ uses . commonjsHelpers = true ;
55
+ exportDeclarations . push (
56
+ `export default /*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ moduleName } .exports);`
57
+ ) ;
58
+ } else {
59
+ exportDeclarations . push ( `export default ${ moduleName } .exports;` ) ;
60
+ }
61
+ } else {
62
+ let deconflictedDefaultExportName ;
43
63
44
- // Collect and rewrite module.exports assignments
45
- for ( const { left } of topLevelModuleExportsAssignments ) {
46
- magicString . overwrite ( left . start , left . end , `${ moduleName } .exports` ) ;
47
- }
64
+ // TODO Lukas if we have an assignment to module.exports and assignments to exports.foo or usages of exports we need to wrap
65
+ // TODO Lukas remove?
66
+ // Collect and rewrite module.exports assignments
67
+ for ( const { left } of topLevelModuleExportsAssignments ) {
68
+ magicString . overwrite ( left . start , left . end , `${ moduleName } .exports` ) ;
69
+ }
48
70
49
- // Collect and rewrite named exports
50
- for ( const [ exportName , node ] of topLevelExportsAssignmentsByName ) {
51
- const deconflicted = deconflict ( exportName ) ;
52
- magicString . overwrite ( node . start , node . left . end , `var ${ deconflicted } ` ) ;
71
+ // Collect and rewrite named exports
72
+ for ( const [ exportName , node ] of topLevelExportsAssignmentsByName ) {
73
+ const deconflicted = deconflict ( exportName ) ;
74
+ magicString . overwrite ( node . start , node . left . end , `var ${ deconflicted } ` ) ;
53
75
54
- if ( exportName === 'default' ) {
55
- deconflictedDefaultExportName = deconflicted ;
56
- } else {
57
- exportDeclarations . push (
58
- exportName === deconflicted
59
- ? `export { ${ exportName } };`
60
- : `export { ${ deconflicted } as ${ exportName } };`
76
+ if ( exportName === 'default' ) {
77
+ deconflictedDefaultExportName = deconflicted ;
78
+ } else {
79
+ exportDeclarations . push (
80
+ exportName === deconflicted
81
+ ? `export { ${ exportName } };`
82
+ : `export { ${ deconflicted } as ${ exportName } };`
83
+ ) ;
84
+ }
85
+
86
+ magicString . appendLeft (
87
+ code [ node . end ] === ';' ? node . end + 1 : node . end ,
88
+ `\n${ moduleName } .exports.${ exportName } = ${ deconflicted } ;`
61
89
) ;
62
90
}
63
91
64
- magicString . appendLeft (
65
- code [ node . end ] === ';' ? node . end + 1 : node . end ,
66
- `\n${ moduleName } .exports.${ exportName } = ${ deconflicted } ;`
67
- ) ;
68
- }
69
-
70
- // Collect and rewrite exports.__esModule assignments
71
- let isRestorableCompiledEsm = false ;
72
- for ( const expression of defineCompiledEsmExpressions ) {
73
- isRestorableCompiledEsm = true ;
74
- const moduleExportsExpression =
75
- expression . type === 'CallExpression' ? expression . arguments [ 0 ] : expression . left . object ;
76
- magicString . overwrite (
77
- moduleExportsExpression . start ,
78
- moduleExportsExpression . end ,
79
- `${ moduleName } .exports`
80
- ) ;
81
- }
92
+ // Collect and rewrite exports.__esModule assignments
93
+ let isRestorableCompiledEsm = false ;
94
+ for ( const expression of defineCompiledEsmExpressions ) {
95
+ isRestorableCompiledEsm = true ;
96
+ const moduleExportsExpression =
97
+ expression . type === 'CallExpression' ? expression . arguments [ 0 ] : expression . left . object ;
98
+ magicString . overwrite (
99
+ moduleExportsExpression . start ,
100
+ moduleExportsExpression . end ,
101
+ `${ moduleName } .exports`
102
+ ) ;
103
+ }
82
104
83
- if ( isRestorableCompiledEsm ) {
84
- exportDeclarations . push (
85
- deconflictedDefaultExportName
86
- ? `export {${ deconflictedDefaultExportName } as default};`
87
- : `export default ${ moduleName } .exports;`
88
- ) ;
89
- } else if ( deconflictedDefaultExportName && code . indexOf ( '__esModule' ) >= 0 ) {
90
- // eslint-disable-next-line no-param-reassign
91
- uses . commonjsHelpers = true ;
92
- exportDeclarations . push (
93
- `export default /*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ moduleName } .exports);`
94
- ) ;
95
- } else {
96
- exportDeclarations . push ( `export default ${ moduleName } .exports;` ) ;
105
+ if ( isRestorableCompiledEsm ) {
106
+ exportDeclarations . push (
107
+ deconflictedDefaultExportName
108
+ ? `export {${ deconflictedDefaultExportName } as default};`
109
+ : `export default ${ moduleName } .exports;`
110
+ ) ;
111
+ } else if ( deconflictedDefaultExportName && code . indexOf ( '__esModule' ) >= 0 ) {
112
+ // eslint-disable-next-line no-param-reassign
113
+ uses . commonjsHelpers = true ;
114
+ exportDeclarations . push (
115
+ `export default /*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ moduleName } .exports);`
116
+ ) ;
117
+ } else {
118
+ exportDeclarations . push ( `export default ${ moduleName } .exports;` ) ;
119
+ }
97
120
}
98
121
}
99
122
100
- return `\n\n${ exportDeclarations . concat ( moduleExportsPropertyAssignments ) . join ( '\n' ) } ` ;
123
+ return `\n\n${ exportDeclarations . join ( '\n' ) } ` ;
101
124
}
0 commit comments