@@ -2,8 +2,12 @@ import path from 'node:path'
2
2
import MagicString from 'magic-string'
3
3
import type { SourceMap } from 'rollup'
4
4
import type {
5
+ ExportAllDeclaration ,
6
+ ExportDefaultDeclaration ,
7
+ ExportNamedDeclaration ,
5
8
Function as FunctionNode ,
6
9
Identifier ,
10
+ ImportDeclaration ,
7
11
Pattern ,
8
12
Property ,
9
13
VariableDeclaration ,
@@ -130,53 +134,70 @@ async function ssrTransformScript(
130
134
)
131
135
}
132
136
133
- // 1. check all import statements and record id -> importName map
137
+ const imports : ( ImportDeclaration & { start : number ; end : number } ) [ ] = [ ]
138
+ const exports : ( (
139
+ | ExportNamedDeclaration
140
+ | ExportDefaultDeclaration
141
+ | ExportAllDeclaration
142
+ ) & { start : number ; end : number } ) [ ] = [ ]
143
+
134
144
for ( const node of ast . body as Node [ ] ) {
145
+ if ( node . type === 'ImportDeclaration' ) {
146
+ imports . push ( node )
147
+ } else if (
148
+ node . type === 'ExportNamedDeclaration' ||
149
+ node . type === 'ExportDefaultDeclaration' ||
150
+ node . type === 'ExportAllDeclaration'
151
+ ) {
152
+ exports . push ( node )
153
+ }
154
+ }
155
+
156
+ // 1. check all import statements and record id -> importName map
157
+ for ( const node of imports ) {
135
158
// import foo from 'foo' --> foo -> __import_foo__.default
136
159
// import { baz } from 'foo' --> baz -> __import_foo__.baz
137
160
// import * as ok from 'foo' --> ok -> __import_foo__
138
- if ( node . type === 'ImportDeclaration' ) {
139
- const importId = defineImport ( hoistIndex , node . source . value as string , {
140
- importedNames : node . specifiers
141
- . map ( ( s ) => {
142
- if ( s . type === 'ImportSpecifier' )
143
- return s . imported . type === 'Identifier'
144
- ? s . imported . name
145
- : // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
146
- s . imported . value
147
- else if ( s . type === 'ImportDefaultSpecifier' ) return 'default'
148
- } )
149
- . filter ( isDefined ) ,
150
- } )
151
- s . remove ( node . start , node . end )
152
- for ( const spec of node . specifiers ) {
153
- if ( spec . type === 'ImportSpecifier' ) {
154
- if ( spec . imported . type === 'Identifier' ) {
155
- idToImportMap . set (
156
- spec . local . name ,
157
- `${ importId } .${ spec . imported . name } ` ,
158
- )
159
- } else {
160
- idToImportMap . set (
161
- spec . local . name ,
162
- `${ importId } [${
163
- // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
164
- JSON . stringify ( spec . imported . value )
165
- } ]`,
166
- )
167
- }
168
- } else if ( spec . type === 'ImportDefaultSpecifier' ) {
169
- idToImportMap . set ( spec . local . name , `${ importId } .default` )
161
+ const importId = defineImport ( hoistIndex , node . source . value as string , {
162
+ importedNames : node . specifiers
163
+ . map ( ( s ) => {
164
+ if ( s . type === 'ImportSpecifier' )
165
+ return s . imported . type === 'Identifier'
166
+ ? s . imported . name
167
+ : // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
168
+ s . imported . value
169
+ else if ( s . type === 'ImportDefaultSpecifier' ) return 'default'
170
+ } )
171
+ . filter ( isDefined ) ,
172
+ } )
173
+ s . remove ( node . start , node . end )
174
+ for ( const spec of node . specifiers ) {
175
+ if ( spec . type === 'ImportSpecifier' ) {
176
+ if ( spec . imported . type === 'Identifier' ) {
177
+ idToImportMap . set (
178
+ spec . local . name ,
179
+ `${ importId } .${ spec . imported . name } ` ,
180
+ )
170
181
} else {
171
- // namespace specifier
172
- idToImportMap . set ( spec . local . name , importId )
182
+ idToImportMap . set (
183
+ spec . local . name ,
184
+ `${ importId } [${
185
+ // @ts -expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
186
+ JSON . stringify ( spec . imported . value )
187
+ } ]`,
188
+ )
173
189
}
190
+ } else if ( spec . type === 'ImportDefaultSpecifier' ) {
191
+ idToImportMap . set ( spec . local . name , `${ importId } .default` )
192
+ } else {
193
+ // namespace specifier
194
+ idToImportMap . set ( spec . local . name , importId )
174
195
}
175
196
}
176
197
}
177
198
178
199
// 2. check all export statements and define exports
179
- for ( const node of ast . body as Node [ ] ) {
200
+ for ( const node of exports ) {
180
201
// named exports
181
202
if ( node . type === 'ExportNamedDeclaration' ) {
182
203
if ( node . declaration ) {
0 commit comments