@@ -7,19 +7,22 @@ export function wrapCode(magicString, uses, moduleName, exportsName, indentExclu
77 }
88 if ( uses . exports ) {
99 args . push ( 'exports' ) ;
10- passedArgs . push ( exportsName ) ;
10+ passedArgs . push ( uses . module ? ` ${ moduleName } .exports` : exportsName ) ;
1111 }
1212 magicString
1313 . trim ( )
1414 . indent ( '\t' , { exclude : indentExclusionRanges } )
1515 . 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 ( ', ' ) } ));` ) ;
1719}
1820
1921export function rewriteExportsAndGetExportsBlock (
2022 magicString ,
2123 moduleName ,
2224 exportsName ,
25+ exportedExportsName ,
2326 wrapped ,
2427 moduleExportsAssignments ,
2528 firstTopLevelModuleExportsAssignment ,
@@ -30,7 +33,6 @@ export function rewriteExportsAndGetExportsBlock(
3033 code ,
3134 HELPERS_NAME ,
3235 exportMode ,
33- detectWrappedDefault ,
3436 defaultIsModuleExports ,
3537 usesRequireWrapper ,
3638 requireName
@@ -58,17 +60,20 @@ export function rewriteExportsAndGetExportsBlock(
5860 exportDeclarations ,
5961 moduleExportsAssignments ,
6062 firstTopLevelModuleExportsAssignment ,
61- exportsName
63+ exportsName ,
64+ defaultIsModuleExports ,
65+ HELPERS_NAME
6266 ) ;
6367 } 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+ }
6574 if ( wrapped ) {
66- getExportsWhenWrapping (
67- exportDeclarations ,
68- exportsName ,
69- detectWrappedDefault ,
70- HELPERS_NAME ,
71- defaultIsModuleExports
75+ exportDeclarations . push (
76+ getDefaultExportDeclaration ( exportedExportsName , defaultIsModuleExports , HELPERS_NAME )
7277 ) ;
7378 } else {
7479 getExports (
@@ -81,17 +86,19 @@ export function rewriteExportsAndGetExportsBlock(
8186 topLevelAssignments ,
8287 moduleName ,
8388 exportsName ,
89+ exportedExportsName ,
8490 defineCompiledEsmExpressions ,
8591 HELPERS_NAME ,
86- defaultIsModuleExports
92+ defaultIsModuleExports ,
93+ exportMode
8794 ) ;
8895 }
8996 }
9097 if ( exports . length ) {
91- exportDeclarations . push ( `export { ${ exports . join ( ', ' ) } }; ` ) ;
98+ exportDeclarations . push ( `export { ${ exports . join ( ', ' ) } }` ) ;
9299 }
93100
94- return `\n\n${ exportDeclarations . join ( '\n' ) } ` ;
101+ return `\n\n${ exportDeclarations . join ( '; \n' ) } ; ` ;
95102}
96103
97104function getExportsWhenUsingRequireWrapper (
@@ -106,35 +113,32 @@ function getExportsWhenUsingRequireWrapper(
106113 requireName ,
107114 defineCompiledEsmExpressions
108115) {
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 ) {
129125 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 } `
133131 ) ;
134132 }
135133 }
134+ replaceDefineCompiledEsmExpressionsAndGetIfRestorable (
135+ defineCompiledEsmExpressions ,
136+ magicString ,
137+ exportMode ,
138+ moduleName ,
139+ exportsName
140+ ) ;
136141 }
137- exports . push ( `${ requireName } as __require` ) ;
138142}
139143
140144function getExportsForReplacedModuleExports (
@@ -143,34 +147,30 @@ function getExportsForReplacedModuleExports(
143147 exportDeclarations ,
144148 moduleExportsAssignments ,
145149 firstTopLevelModuleExportsAssignment ,
146- exportsName
150+ exportsName ,
151+ defaultIsModuleExports ,
152+ HELPERS_NAME
147153) {
148154 for ( const { left } of moduleExportsAssignments ) {
149155 magicString . overwrite ( left . start , left . end , exportsName ) ;
150156 }
151157 magicString . prependRight ( firstTopLevelModuleExportsAssignment . left . start , 'var ' ) ;
152158 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- ) {
163159 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 )
171161 ) ;
172162}
173163
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+
174174function getExports (
175175 magicString ,
176176 exports ,
@@ -181,9 +181,11 @@ function getExports(
181181 topLevelAssignments ,
182182 moduleName ,
183183 exportsName ,
184+ exportedExportsName ,
184185 defineCompiledEsmExpressions ,
185186 HELPERS_NAME ,
186- defaultIsModuleExports
187+ defaultIsModuleExports ,
188+ exportMode
187189) {
188190 let deconflictedDefaultExportName ;
189191 // Collect and rewrite module.exports assignments
@@ -195,8 +197,10 @@ function getExports(
195197 for ( const [ exportName , { nodes } ] of exportsAssignmentsByName ) {
196198 const deconflicted = deconflictedExportNames [ exportName ] ;
197199 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 } `;
200204 if ( needsDeclaration && topLevelAssignments . has ( node ) ) {
201205 replacement = `var ${ replacement } ` ;
202206 needsDeclaration = false ;
@@ -214,22 +218,58 @@ function getExports(
214218 }
215219 }
216220
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+ ) ;
225228
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` ) ;
230243 } else {
231244 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
233272 ) ;
234273 }
274+ return isRestorableCompiledEsm ;
235275}
0 commit comments