@@ -186,27 +186,63 @@ async function ssrTransformScript(
186
186
)
187
187
}
188
188
189
- const imports : RollupAstNode < ImportDeclaration > [ ] = [ ]
189
+ const imports : (
190
+ | RollupAstNode < ImportDeclaration >
191
+ | RollupAstNode < ExportNamedDeclaration >
192
+ | RollupAstNode < ExportAllDeclaration >
193
+ ) [ ] = [ ]
190
194
const exports : (
191
195
| RollupAstNode < ExportNamedDeclaration >
192
196
| RollupAstNode < ExportDefaultDeclaration >
193
197
| RollupAstNode < ExportAllDeclaration >
194
198
) [ ] = [ ]
199
+ const reExportImportIdMap = new Map <
200
+ RollupAstNode < ExportNamedDeclaration > | RollupAstNode < ExportAllDeclaration > ,
201
+ string
202
+ > ( )
195
203
196
204
for ( const node of ast . body as Node [ ] ) {
197
205
if ( node . type === 'ImportDeclaration' ) {
198
206
imports . push ( node )
207
+ } else if ( node . type === 'ExportDefaultDeclaration' ) {
208
+ exports . push ( node )
199
209
} else if (
200
210
node . type === 'ExportNamedDeclaration' ||
201
- node . type === 'ExportDefaultDeclaration' ||
202
211
node . type === 'ExportAllDeclaration'
203
212
) {
213
+ imports . push ( node )
204
214
exports . push ( node )
205
215
}
206
216
}
207
217
208
- // 1. check all import statements and record id -> importName map
218
+ // 1. check all import statements, hoist imports, and record id -> importName map
209
219
for ( const node of imports ) {
220
+ // hoist re-export's import at the same time as normal imports to preserve execution order
221
+ if ( node . type === 'ExportNamedDeclaration' ) {
222
+ if ( node . source ) {
223
+ // export { foo, bar } from './foo'
224
+ const importId = defineImport (
225
+ hoistIndex ,
226
+ node as RollupAstNode < ExportNamedDeclaration & { source : Literal } > ,
227
+ {
228
+ importedNames : node . specifiers . map (
229
+ ( s ) => getIdentifierNameOrLiteralValue ( s . local ) as string ,
230
+ ) ,
231
+ } ,
232
+ )
233
+ reExportImportIdMap . set ( node , importId )
234
+ }
235
+ continue
236
+ }
237
+ if ( node . type === 'ExportAllDeclaration' ) {
238
+ if ( node . source ) {
239
+ // export * from './foo'
240
+ const importId = defineImport ( hoistIndex , node )
241
+ reExportImportIdMap . set ( node , importId )
242
+ }
243
+ continue
244
+ }
245
+
210
246
// import foo from 'foo' --> foo -> __import_foo__.default
211
247
// import { baz } from 'foo' --> baz -> __import_foo__.baz
212
248
// import * as ok from 'foo' --> ok -> __import_foo__
@@ -263,18 +299,9 @@ async function ssrTransformScript(
263
299
}
264
300
s . remove ( node . start , ( node . declaration as Node ) . start )
265
301
} else {
266
- s . remove ( node . start , node . end )
267
302
if ( node . source ) {
268
303
// export { foo, bar } from './foo'
269
- const importId = defineImport (
270
- node . start ,
271
- node as RollupAstNode < ExportNamedDeclaration & { source : Literal } > ,
272
- {
273
- importedNames : node . specifiers . map (
274
- ( s ) => getIdentifierNameOrLiteralValue ( s . local ) as string ,
275
- ) ,
276
- } ,
277
- )
304
+ const importId = reExportImportIdMap . get ( node ) !
278
305
for ( const spec of node . specifiers ) {
279
306
const exportedAs = getIdentifierNameOrLiteralValue (
280
307
spec . exported ,
@@ -290,6 +317,7 @@ async function ssrTransformScript(
290
317
}
291
318
}
292
319
} else {
320
+ s . remove ( node . start , node . end )
293
321
// export { foo, bar }
294
322
for ( const spec of node . specifiers ) {
295
323
// spec.local can be Literal only when it has "from 'something'"
@@ -333,7 +361,7 @@ async function ssrTransformScript(
333
361
334
362
// export * from './foo'
335
363
if ( node . type === 'ExportAllDeclaration' ) {
336
- const importId = defineImport ( node . start , node )
364
+ const importId = reExportImportIdMap . get ( node ) !
337
365
if ( node . exported ) {
338
366
const exportedAs = getIdentifierNameOrLiteralValue (
339
367
node . exported ,
0 commit comments