@@ -150,7 +150,7 @@ impl<'a> ModuleRunnerTransform<'a> {
150
150
}
151
151
Statement :: ExportAllDeclaration ( export) => {
152
152
self . transform_export_all_declaration (
153
- & mut new_stmts ,
153
+ & mut hoist_imports ,
154
154
& mut hoist_exports,
155
155
export,
156
156
ctx,
@@ -159,6 +159,7 @@ impl<'a> ModuleRunnerTransform<'a> {
159
159
Statement :: ExportNamedDeclaration ( export) => {
160
160
self . transform_export_named_declaration (
161
161
& mut new_stmts,
162
+ & mut hoist_imports,
162
163
& mut hoist_exports,
163
164
export,
164
165
ctx,
@@ -378,6 +379,7 @@ impl<'a> ModuleRunnerTransform<'a> {
378
379
fn transform_export_named_declaration (
379
380
& mut self ,
380
381
new_stmts : & mut ArenaVec < ' a , Statement < ' a > > ,
382
+ hoist_imports : & mut Vec < Statement < ' a > > ,
381
383
hoist_exports : & mut Vec < Statement < ' a > > ,
382
384
export : ArenaBox < ' a , ExportNamedDeclaration < ' a > > ,
383
385
ctx : & mut TraverseCtx < ' a > ,
@@ -433,7 +435,7 @@ impl<'a> ModuleRunnerTransform<'a> {
433
435
Argument :: from ( Expression :: StringLiteral ( ctx. ast . alloc ( source) ) ) ,
434
436
Self :: create_imported_names_object ( imported_names, ctx) ,
435
437
] ) ;
436
- new_stmts . push ( Self :: create_import ( SPAN , pattern, arguments, ctx) ) ;
438
+ hoist_imports . push ( Self :: create_import ( SPAN , pattern, arguments, ctx) ) ;
437
439
binding
438
440
} ) ;
439
441
@@ -479,7 +481,7 @@ impl<'a> ModuleRunnerTransform<'a> {
479
481
/// ```
480
482
fn transform_export_all_declaration (
481
483
& mut self ,
482
- new_stmts : & mut ArenaVec < ' a , Statement < ' a > > ,
484
+ hoist_imports : & mut Vec < Statement < ' a > > ,
483
485
hoist_exports : & mut Vec < Statement < ' a > > ,
484
486
export : ArenaBox < ' a , ExportAllDeclaration < ' a > > ,
485
487
ctx : & mut TraverseCtx < ' a > ,
@@ -498,16 +500,16 @@ impl<'a> ModuleRunnerTransform<'a> {
498
500
// `export * as foo from 'vue'` ->
499
501
// `Object.defineProperty(__vite_ssr_exports__, 'foo', { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ } });`
500
502
let export = Self :: create_export ( span, ident, exported. name ( ) , ctx) ;
501
- new_stmts . push ( import) ;
503
+ hoist_imports . push ( import) ;
502
504
hoist_exports. push ( export) ;
503
505
} else {
504
506
let callee = ctx. ast . expression_identifier ( SPAN , SSR_EXPORT_ALL_KEY ) ;
505
507
let arguments = ctx. ast . vec1 ( Argument :: from ( ident) ) ;
506
508
// `export * from 'vue'` -> `__vite_ssr_exportAll__(__vite_ssr_import_0__);`
507
509
let call = ctx. ast . expression_call ( SPAN , callee, NONE , arguments, false ) ;
508
510
let export = ctx. ast . statement_expression ( span, call) ;
509
- // we cannot hoist this case , so keep at the same position
510
- new_stmts . extend ( [ import, export] ) ;
511
+ // names from `export *` cannot be known , so add it right after the import.
512
+ hoist_imports . extend ( [ import, export] ) ;
511
513
}
512
514
}
513
515
@@ -1190,13 +1192,12 @@ const __vite_ssr_import_0__ = await __vite_ssr_import__('vue', { importedNames:
1190
1192
1191
1193
#[ test]
1192
1194
fn export_then_import_minified ( ) {
1193
- // `import` is hoisted but `export * from` is not. this is an expected behavior.
1194
1195
test_same (
1195
1196
"export * from 'vue';import {createApp} from 'vue'" ,
1196
1197
"
1197
- const __vite_ssr_import_1__ = await __vite_ssr_import__('vue', { importedNames: ['createApp'] });
1198
1198
const __vite_ssr_import_0__ = await __vite_ssr_import__('vue');
1199
1199
__vite_ssr_exportAll__(__vite_ssr_import_0__);
1200
+ const __vite_ssr_import_1__ = await __vite_ssr_import__('vue', { importedNames: ['createApp'] });
1200
1201
" ,
1201
1202
) ;
1202
1203
}
@@ -2064,10 +2065,12 @@ __vite_ssr_dynamic_import__('./bar.json', { with: { type: 'json' } });",
2064
2065
export * from './b'
2065
2066
console.log(foo + 2)" ,
2066
2067
"
2068
+ const __vite_ssr_import_0__ = await __vite_ssr_import__('./a');
2069
+ __vite_ssr_exportAll__(__vite_ssr_import_0__);
2067
2070
const __vite_ssr_import_1__ = await __vite_ssr_import__('./foo', { importedNames: ['foo']});
2071
+ const __vite_ssr_import_2__ = await __vite_ssr_import__('./b');
2072
+ __vite_ssr_exportAll__(__vite_ssr_import_2__);
2068
2073
console.log(__vite_ssr_import_1__.foo + 1);
2069
- const __vite_ssr_import_0__ = await __vite_ssr_import__('./a');__vite_ssr_exportAll__(__vite_ssr_import_0__);
2070
- const __vite_ssr_import_2__ = await __vite_ssr_import__('./b');__vite_ssr_exportAll__(__vite_ssr_import_2__);
2071
2074
console.log(__vite_ssr_import_1__.foo + 2);" ,
2072
2075
) ;
2073
2076
}
@@ -2090,8 +2093,8 @@ Object.defineProperty(__vite_ssr_exports__, 'bar', {
2090
2093
}
2091
2094
});
2092
2095
const __vite_ssr_import_0__ = await __vite_ssr_import__('./foo', { importedNames: ['foo'] });
2093
- __vite_ssr_exports__.default = (0, __vite_ssr_import_0__.foo)();
2094
2096
const __vite_ssr_import_1__ = await __vite_ssr_import__('./bar');
2097
+ __vite_ssr_exports__.default = (0, __vite_ssr_import_0__.foo)();
2095
2098
console.log(bar);
2096
2099
" ,
2097
2100
) ;
@@ -2295,8 +2298,8 @@ const c = () => {
2295
2298
const __vite_ssr_import_2__ = await __vite_ssr_import__('c');
2296
2299
__vite_ssr_exportAll__(__vite_ssr_import_2__);
2297
2300
const __vite_ssr_import_3__ = await __vite_ssr_import__('d');
2298
- __vite_ssr_dynamic_import__('e');
2299
2301
const __vite_ssr_import_4__ = await __vite_ssr_import__('a');
2302
+ __vite_ssr_dynamic_import__('e');
2300
2303
" ;
2301
2304
test_same_and_deps ( code, expected, & [ "a" , "b" , "c" , "d" ] , & [ "e" ] ) ;
2302
2305
}
0 commit comments