@@ -8,6 +8,7 @@ import type {
8
8
Function as FunctionNode ,
9
9
Identifier ,
10
10
ImportDeclaration ,
11
+ Literal ,
11
12
Pattern ,
12
13
Property ,
13
14
VariableDeclaration ,
@@ -130,7 +131,7 @@ async function ssrTransformScript(
130
131
function defineExport ( position : number , name : string , local = name ) {
131
132
s . appendLeft (
132
133
position ,
133
- `\nObject.defineProperty(${ ssrModuleExportsKey } , " ${ name } " , ` +
134
+ `\nObject.defineProperty(${ ssrModuleExportsKey } , ${ JSON . stringify ( name ) } , ` +
134
135
`{ enumerable: true, configurable: true, get(){ return ${ local } }});` ,
135
136
)
136
137
}
@@ -163,10 +164,7 @@ async function ssrTransformScript(
163
164
importedNames : node . specifiers
164
165
. map ( ( s ) => {
165
166
if ( s . type === 'ImportSpecifier' )
166
- return s . imported . type === 'Identifier'
167
- ? s . imported . name
168
- : // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
169
- s . imported . value
167
+ return getIdentifierNameOrLiteralValue ( s . imported ) as string
170
168
else if ( s . type === 'ImportDefaultSpecifier' ) return 'default'
171
169
} )
172
170
. filter ( isDefined ) ,
@@ -182,10 +180,7 @@ async function ssrTransformScript(
182
180
} else {
183
181
idToImportMap . set (
184
182
spec . local . name ,
185
- `${ importId } [${
186
- // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
187
- JSON . stringify ( spec . imported . value )
188
- } ]`,
183
+ `${ importId } [${ JSON . stringify ( spec . imported . value as string ) } ]` ,
189
184
)
190
185
}
191
186
} else if ( spec . type === 'ImportDefaultSpecifier' ) {
@@ -226,33 +221,39 @@ async function ssrTransformScript(
226
221
node . start ,
227
222
node . source . value as string ,
228
223
{
229
- importedNames : node . specifiers . map ( ( s ) => s . local . name ) ,
224
+ importedNames : node . specifiers . map (
225
+ ( s ) => getIdentifierNameOrLiteralValue ( s . local ) as string ,
226
+ ) ,
230
227
} ,
231
228
)
232
229
for ( const spec of node . specifiers ) {
233
- const exportedAs =
234
- spec . exported . type === 'Identifier'
235
- ? spec . exported . name
236
- : // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
237
- spec . exported . value
238
-
239
- defineExport (
240
- node . start ,
241
- exportedAs ,
242
- `${ importId } .${ spec . local . name } ` ,
243
- )
230
+ const exportedAs = getIdentifierNameOrLiteralValue (
231
+ spec . exported ,
232
+ ) as string
233
+
234
+ if ( spec . local . type === 'Identifier' ) {
235
+ defineExport (
236
+ node . start ,
237
+ exportedAs ,
238
+ `${ importId } .${ spec . local . name } ` ,
239
+ )
240
+ } else {
241
+ defineExport (
242
+ node . start ,
243
+ exportedAs ,
244
+ `${ importId } [${ JSON . stringify ( spec . local . value as string ) } ]` ,
245
+ )
246
+ }
244
247
}
245
248
} else {
246
249
// export { foo, bar }
247
250
for ( const spec of node . specifiers ) {
248
- const local = spec . local . name
251
+ // spec.local can be Literal only when it has "from 'something'"
252
+ const local = ( spec . local as Identifier ) . name
249
253
const binding = idToImportMap . get ( local )
250
-
251
- const exportedAs =
252
- spec . exported . type === 'Identifier'
253
- ? spec . exported . name
254
- : // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
255
- spec . exported . value
254
+ const exportedAs = getIdentifierNameOrLiteralValue (
255
+ spec . exported ,
256
+ ) as string
256
257
257
258
defineExport ( node . end , exportedAs , binding || local )
258
259
}
@@ -292,7 +293,10 @@ async function ssrTransformScript(
292
293
s . remove ( node . start , node . end )
293
294
const importId = defineImport ( node . start , node . source . value as string )
294
295
if ( node . exported ) {
295
- defineExport ( node . start , node . exported . name , `${ importId } ` )
296
+ const exportedAs = getIdentifierNameOrLiteralValue (
297
+ node . exported ,
298
+ ) as string
299
+ defineExport ( node . start , exportedAs , `${ importId } ` )
296
300
} else {
297
301
s . appendLeft ( node . start , `${ ssrExportAllKey } (${ importId } );\n` )
298
302
}
@@ -377,6 +381,10 @@ async function ssrTransformScript(
377
381
}
378
382
}
379
383
384
+ function getIdentifierNameOrLiteralValue ( node : Identifier | Literal ) {
385
+ return node . type === 'Identifier' ? node . name : node . value
386
+ }
387
+
380
388
interface Visitors {
381
389
onIdentifier : (
382
390
node : Identifier & {
0 commit comments