@@ -7,19 +7,22 @@ export function wrapCode(magicString, uses, moduleName, exportsName, indentExclu
7
7
}
8
8
if ( uses . exports ) {
9
9
args . push ( 'exports' ) ;
10
- passedArgs . push ( exportsName ) ;
10
+ passedArgs . push ( uses . module ? ` ${ moduleName } .exports` : exportsName ) ;
11
11
}
12
12
magicString
13
13
. trim ( )
14
14
. indent ( '\t' , { exclude : indentExclusionRanges } )
15
15
. prepend ( `(function (${ args . join ( ', ' ) } ) {\n` )
16
- . append ( `\n} (${ passedArgs . join ( ', ' ) } ));` ) ;
16
+ // For some reason, this line is only indented correctly when using a
17
+ // require-wrapper if we have this leading space
18
+ . append ( ` \n} (${ passedArgs . join ( ', ' ) } ));` ) ;
17
19
}
18
20
19
21
export function rewriteExportsAndGetExportsBlock (
20
22
magicString ,
21
23
moduleName ,
22
24
exportsName ,
25
+ exportedExportsName ,
23
26
wrapped ,
24
27
moduleExportsAssignments ,
25
28
firstTopLevelModuleExportsAssignment ,
@@ -30,7 +33,6 @@ export function rewriteExportsAndGetExportsBlock(
30
33
code ,
31
34
HELPERS_NAME ,
32
35
exportMode ,
33
- detectWrappedDefault ,
34
36
defaultIsModuleExports ,
35
37
usesRequireWrapper ,
36
38
requireName
@@ -58,17 +60,20 @@ export function rewriteExportsAndGetExportsBlock(
58
60
exportDeclarations ,
59
61
moduleExportsAssignments ,
60
62
firstTopLevelModuleExportsAssignment ,
61
- exportsName
63
+ exportsName ,
64
+ defaultIsModuleExports ,
65
+ HELPERS_NAME
62
66
) ;
63
67
} else {
64
- exports . push ( `${ exportsName } as __moduleExports` ) ;
68
+ if ( exportMode === 'module' ) {
69
+ exportDeclarations . push ( `var ${ exportedExportsName } = ${ moduleName } .exports` ) ;
70
+ exports . push ( `${ exportedExportsName } as __moduleExports` ) ;
71
+ } else {
72
+ exports . push ( `${ exportsName } as __moduleExports` ) ;
73
+ }
65
74
if ( wrapped ) {
66
- getExportsWhenWrapping (
67
- exportDeclarations ,
68
- exportsName ,
69
- detectWrappedDefault ,
70
- HELPERS_NAME ,
71
- defaultIsModuleExports
75
+ exportDeclarations . push (
76
+ getDefaultExportDeclaration ( exportedExportsName , defaultIsModuleExports , HELPERS_NAME )
72
77
) ;
73
78
} else {
74
79
getExports (
@@ -81,17 +86,19 @@ export function rewriteExportsAndGetExportsBlock(
81
86
topLevelAssignments ,
82
87
moduleName ,
83
88
exportsName ,
89
+ exportedExportsName ,
84
90
defineCompiledEsmExpressions ,
85
91
HELPERS_NAME ,
86
- defaultIsModuleExports
92
+ defaultIsModuleExports ,
93
+ exportMode
87
94
) ;
88
95
}
89
96
}
90
97
if ( exports . length ) {
91
- exportDeclarations . push ( `export { ${ exports . join ( ', ' ) } }; ` ) ;
98
+ exportDeclarations . push ( `export { ${ exports . join ( ', ' ) } }` ) ;
92
99
}
93
100
94
- return `\n\n${ exportDeclarations . join ( '\n' ) } ` ;
101
+ return `\n\n${ exportDeclarations . join ( '; \n' ) } ; ` ;
95
102
}
96
103
97
104
function getExportsWhenUsingRequireWrapper (
@@ -106,35 +113,32 @@ function getExportsWhenUsingRequireWrapper(
106
113
requireName ,
107
114
defineCompiledEsmExpressions
108
115
) {
109
- if ( ! wrapped ) {
110
- if ( exportMode === 'replace' ) {
111
- for ( const { left } of moduleExportsAssignments ) {
112
- magicString . overwrite ( left . start , left . end , exportsName ) ;
113
- }
114
- } else {
115
- // Collect and rewrite module.exports assignments
116
- for ( const { left } of moduleExportsAssignments ) {
117
- magicString . overwrite ( left . start , left . end , `${ moduleName } .exports` ) ;
118
- }
119
- // Collect and rewrite named exports
120
- for ( const [ exportName , { nodes } ] of exportsAssignmentsByName ) {
121
- for ( const node of nodes ) {
122
- magicString . overwrite ( node . start , node . left . end , `${ exportsName } .${ exportName } ` ) ;
123
- }
124
- }
125
- // Collect and rewrite exports.__esModule assignments
126
- for ( const expression of defineCompiledEsmExpressions ) {
127
- const moduleExportsExpression =
128
- expression . type === 'CallExpression' ? expression . arguments [ 0 ] : expression . left . object ;
116
+ exports . push ( `${ requireName } as __require` ) ;
117
+ if ( wrapped ) return ;
118
+ if ( exportMode === 'replace' ) {
119
+ rewriteModuleExportsAssignments ( magicString , moduleExportsAssignments , exportsName ) ;
120
+ } else {
121
+ rewriteModuleExportsAssignments ( magicString , moduleExportsAssignments , `${ moduleName } .exports` ) ;
122
+ // Collect and rewrite named exports
123
+ for ( const [ exportName , { nodes } ] of exportsAssignmentsByName ) {
124
+ for ( const { node, type } of nodes ) {
129
125
magicString . overwrite (
130
- moduleExportsExpression . start ,
131
- moduleExportsExpression . end ,
132
- exportsName
126
+ node . start ,
127
+ node . left . end ,
128
+ `${
129
+ exportMode === 'module' && type === 'module' ? `${ moduleName } .exports` : exportsName
130
+ } .${ exportName } `
133
131
) ;
134
132
}
135
133
}
134
+ replaceDefineCompiledEsmExpressionsAndGetIfRestorable (
135
+ defineCompiledEsmExpressions ,
136
+ magicString ,
137
+ exportMode ,
138
+ moduleName ,
139
+ exportsName
140
+ ) ;
136
141
}
137
- exports . push ( `${ requireName } as __require` ) ;
138
142
}
139
143
140
144
function getExportsForReplacedModuleExports (
@@ -143,34 +147,30 @@ function getExportsForReplacedModuleExports(
143
147
exportDeclarations ,
144
148
moduleExportsAssignments ,
145
149
firstTopLevelModuleExportsAssignment ,
146
- exportsName
150
+ exportsName ,
151
+ defaultIsModuleExports ,
152
+ HELPERS_NAME
147
153
) {
148
154
for ( const { left } of moduleExportsAssignments ) {
149
155
magicString . overwrite ( left . start , left . end , exportsName ) ;
150
156
}
151
157
magicString . prependRight ( firstTopLevelModuleExportsAssignment . left . start , 'var ' ) ;
152
158
exports . push ( `${ exportsName } as __moduleExports` ) ;
153
- exportDeclarations . push ( `export default ${ exportsName } ;` ) ;
154
- }
155
-
156
- function getExportsWhenWrapping (
157
- exportDeclarations ,
158
- exportsName ,
159
- detectWrappedDefault ,
160
- HELPERS_NAME ,
161
- defaultIsModuleExports
162
- ) {
163
159
exportDeclarations . push (
164
- `export default ${
165
- detectWrappedDefault && defaultIsModuleExports === 'auto'
166
- ? `/*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ exportsName } )`
167
- : defaultIsModuleExports === false
168
- ? `${ exportsName } .default`
169
- : exportsName
170
- } ;`
160
+ getDefaultExportDeclaration ( exportsName , defaultIsModuleExports , HELPERS_NAME )
171
161
) ;
172
162
}
173
163
164
+ function getDefaultExportDeclaration ( exportedExportsName , defaultIsModuleExports , HELPERS_NAME ) {
165
+ return `export default ${
166
+ defaultIsModuleExports === true
167
+ ? exportedExportsName
168
+ : defaultIsModuleExports === false
169
+ ? `${ exportedExportsName } .default`
170
+ : `/*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ exportedExportsName } )`
171
+ } `;
172
+ }
173
+
174
174
function getExports (
175
175
magicString ,
176
176
exports ,
@@ -181,9 +181,11 @@ function getExports(
181
181
topLevelAssignments ,
182
182
moduleName ,
183
183
exportsName ,
184
+ exportedExportsName ,
184
185
defineCompiledEsmExpressions ,
185
186
HELPERS_NAME ,
186
- defaultIsModuleExports
187
+ defaultIsModuleExports ,
188
+ exportMode
187
189
) {
188
190
let deconflictedDefaultExportName ;
189
191
// Collect and rewrite module.exports assignments
@@ -195,8 +197,10 @@ function getExports(
195
197
for ( const [ exportName , { nodes } ] of exportsAssignmentsByName ) {
196
198
const deconflicted = deconflictedExportNames [ exportName ] ;
197
199
let needsDeclaration = true ;
198
- for ( const node of nodes ) {
199
- let replacement = `${ deconflicted } = ${ exportsName } .${ exportName } ` ;
200
+ for ( const { node, type } of nodes ) {
201
+ let replacement = `${ deconflicted } = ${
202
+ exportMode === 'module' && type === 'module' ? `${ moduleName } .exports` : exportsName
203
+ } .${ exportName } `;
200
204
if ( needsDeclaration && topLevelAssignments . has ( node ) ) {
201
205
replacement = `var ${ replacement } ` ;
202
206
needsDeclaration = false ;
@@ -214,22 +218,58 @@ function getExports(
214
218
}
215
219
}
216
220
217
- // Collect and rewrite exports.__esModule assignments
218
- let isRestorableCompiledEsm = false ;
219
- for ( const expression of defineCompiledEsmExpressions ) {
220
- isRestorableCompiledEsm = true ;
221
- const moduleExportsExpression =
222
- expression . type === 'CallExpression' ? expression . arguments [ 0 ] : expression . left . object ;
223
- magicString . overwrite ( moduleExportsExpression . start , moduleExportsExpression . end , exportsName ) ;
224
- }
221
+ const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable (
222
+ defineCompiledEsmExpressions ,
223
+ magicString ,
224
+ exportMode ,
225
+ moduleName ,
226
+ exportsName
227
+ ) ;
225
228
226
- if ( ! isRestorableCompiledEsm || defaultIsModuleExports === true ) {
227
- exports . push ( `${ exportsName } as default` ) ;
228
- } else if ( moduleExportsAssignments . length === 0 || defaultIsModuleExports === false ) {
229
- exports . push ( `${ deconflictedDefaultExportName || exportsName } as default` ) ;
229
+ if (
230
+ defaultIsModuleExports === false ||
231
+ ( defaultIsModuleExports === 'auto' &&
232
+ isRestorableCompiledEsm &&
233
+ moduleExportsAssignments . length === 0 )
234
+ ) {
235
+ // If there is no deconflictedDefaultExportName, then we use the namespace as
236
+ // fallback because there can be no "default" property on the namespace
237
+ exports . push ( `${ deconflictedDefaultExportName || exportedExportsName } as default` ) ;
238
+ } else if (
239
+ defaultIsModuleExports === true ||
240
+ ( ! isRestorableCompiledEsm && moduleExportsAssignments . length === 0 )
241
+ ) {
242
+ exports . push ( `${ exportedExportsName } as default` ) ;
230
243
} else {
231
244
exportDeclarations . push (
232
- `export default /*@__PURE__*/${ HELPERS_NAME } .getDefaultExportFromCjs(${ exportsName } );`
245
+ getDefaultExportDeclaration ( exportedExportsName , defaultIsModuleExports , HELPERS_NAME )
246
+ ) ;
247
+ }
248
+ }
249
+
250
+ function rewriteModuleExportsAssignments ( magicString , moduleExportsAssignments , exportsName ) {
251
+ for ( const { left } of moduleExportsAssignments ) {
252
+ magicString . overwrite ( left . start , left . end , exportsName ) ;
253
+ }
254
+ }
255
+
256
+ function replaceDefineCompiledEsmExpressionsAndGetIfRestorable (
257
+ defineCompiledEsmExpressions ,
258
+ magicString ,
259
+ exportMode ,
260
+ moduleName ,
261
+ exportsName
262
+ ) {
263
+ let isRestorableCompiledEsm = false ;
264
+ for ( const { node, type } of defineCompiledEsmExpressions ) {
265
+ isRestorableCompiledEsm = true ;
266
+ const moduleExportsExpression =
267
+ node . type === 'CallExpression' ? node . arguments [ 0 ] : node . left . object ;
268
+ magicString . overwrite (
269
+ moduleExportsExpression . start ,
270
+ moduleExportsExpression . end ,
271
+ exportMode === 'module' && type === 'module' ? `${ moduleName } .exports` : exportsName
233
272
) ;
234
273
}
274
+ return isRestorableCompiledEsm ;
235
275
}
0 commit comments