@@ -17,7 +17,8 @@ import {
17
17
makeVariableInitDeclaration ,
18
18
makeExportListDeclaration ,
19
19
makeStatement ,
20
- makeAwaitExpression
20
+ makeAwaitExpression ,
21
+ makeImportDeclaration
21
22
} from "./utils/make-node" ;
22
23
import { RandomIdentifierGenerator } from "./utils/random-identifier" ;
23
24
import { resolveImport } from "./utils/resolve-import" ;
@@ -86,6 +87,12 @@ export function transformModule(
86
87
// Extract import declarations
87
88
const imports = ast . body . filter ( ( item ) : item is SWC . ImportDeclaration => item . type === "ImportDeclaration" ) ;
88
89
90
+ // Handle `export { named as renamed } from "module";`
91
+ const exportFroms = ast . body . filter (
92
+ ( item ) : item is SWC . ExportNamedDeclaration => item . type === "ExportNamedDeclaration" && ! ! item . source
93
+ ) ;
94
+ const newImportsByExportFroms : SWC . ImportDeclaration [ ] = [ ] ;
95
+
89
96
const exportMap : Record < string , string > = { } ;
90
97
91
98
// Extract export declarations
@@ -130,15 +137,18 @@ export function transformModule(
130
137
}
131
138
132
139
return false ;
140
+ // Handle `export { named as renamed };` without "from"
133
141
case "ExportNamedDeclaration" :
134
- item . specifiers . forEach ( specifier => {
135
- /* istanbul ignore if */
136
- if ( specifier . type !== "ExportSpecifier" ) {
137
- raiseUnexpectedNode ( "export specifier" , specifier . type ) ;
138
- }
139
-
140
- exportMap [ ( specifier . exported || specifier . orig ) . value ] = specifier . orig . value ;
141
- } ) ;
142
+ if ( ! item . source ) {
143
+ item . specifiers . forEach ( specifier => {
144
+ /* istanbul ignore if */
145
+ if ( specifier . type !== "ExportSpecifier" ) {
146
+ raiseUnexpectedNode ( "export specifier" , specifier . type ) ;
147
+ }
148
+
149
+ exportMap [ ( specifier . exported || specifier . orig ) . value ] = specifier . orig . value ;
150
+ } ) ;
151
+ }
142
152
143
153
return true ;
144
154
}
@@ -181,7 +191,20 @@ export function transformModule(
181
191
const importedNames = new Set (
182
192
imports . flatMap ( importStmt => importStmt . specifiers . map ( specifier => specifier . local . value ) )
183
193
) ;
184
- const exportedNamesDeclaration = makeVariablesDeclaration ( exportedNames . filter ( name => ! importedNames . has ( name ) ) ) ;
194
+ const exportFromedNames = new Set (
195
+ exportFroms . flatMap ( exportStmt => exportStmt . specifiers . map ( specifier => {
196
+ if ( specifier . type === "ExportNamespaceSpecifier" ) {
197
+ return specifier . name . value ;
198
+ } else if ( specifier . type === "ExportDefaultSpecifier" ) {
199
+ // When will this happen?
200
+ return specifier . exported . value ;
201
+ } else {
202
+ return ( specifier . exported || specifier . orig ) . value ;
203
+ }
204
+ } ) )
205
+ ) ;
206
+ const exportedNamesDeclaration = makeVariablesDeclaration ( exportedNames . filter ( name => ! importedNames . has ( name ) && ! exportFromedNames . has ( name ) ) ) ;
207
+
185
208
const warppedStatements = topLevelStatements . flatMap < SWC . Statement > ( stmt => {
186
209
if ( stmt . type === "VariableDeclaration" ) {
187
210
const declaredNames = stmt . declarations . flatMap ( decl => resolvePattern ( decl . id ) ) ;
@@ -283,12 +306,19 @@ export function transformModule(
283
306
284
307
// Add import of TLA promises from imported modules
285
308
let importedPromiseCount = 0 ;
286
- for ( const importDeclaration of imports ) {
287
- const importedModuleName = resolveImport ( moduleName , importDeclaration . source . value ) ;
309
+ for ( const declaration of [ ... imports , ... exportFroms ] ) {
310
+ const importedModuleName = resolveImport ( moduleName , declaration . source . value ) ;
288
311
if ( ! importedModuleName || ! bundleInfo [ importedModuleName ] ) continue ;
289
312
290
313
if ( bundleInfo [ importedModuleName ] . transformNeeded ) {
291
- importDeclaration . specifiers . push (
314
+ let targetImportDeclaration : SWC . ImportDeclaration ;
315
+ if ( declaration . type === "ImportDeclaration" ) {
316
+ targetImportDeclaration = declaration ;
317
+ } else {
318
+ targetImportDeclaration = makeImportDeclaration ( declaration . source ) ;
319
+ newImportsByExportFroms . push ( targetImportDeclaration ) ;
320
+ }
321
+ targetImportDeclaration . specifiers . push (
292
322
makeImportSpecifier ( options . promiseExportName , options . promiseImportName ( importedPromiseCount ) )
293
323
) ;
294
324
importedPromiseCount ++ ;
@@ -338,7 +368,7 @@ export function transformModule(
338
368
* export { ..., __tla };
339
369
*/
340
370
341
- const newTopLevel : SWC . ModuleItem [ ] = [ ...imports , exportedNamesDeclaration ] ;
371
+ const newTopLevel : SWC . ModuleItem [ ] = [ ...imports , ... newImportsByExportFroms , ... exportFroms , exportedNamesDeclaration ] ;
342
372
343
373
if ( exportedNames . length > 0 || bundleInfo [ moduleName ] ?. importedBy ?. length > 0 ) {
344
374
// If the chunk is being imported, append export of the TLA promise to export list
0 commit comments