Skip to content

Commit dfec6ca

Browse files
authored
fix(ssr): use appendRight for import (#9554)
1 parent a6b12f8 commit dfec6ca

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ test('export default', async () => {
125125
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
126126
})
127127

128+
test('export then import minified', async () => {
129+
expect(
130+
await ssrTransformSimpleCode(
131+
`export * from 'vue';import {createApp} from 'vue';`
132+
)
133+
).toMatchInlineSnapshot(`
134+
"const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
135+
__vite_ssr_exportAll__(__vite_ssr_import_1__);const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
136+
"
137+
`)
138+
})
139+
128140
test('import.meta', async () => {
129141
expect(
130142
await ssrTransformSimpleCode(`console.log(import.meta.url)`)

packages/vite/src/node/ssr/ssrTransform.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async function ssrTransformScript(
9494
function defineImport(node: Node, source: string) {
9595
deps.add(source)
9696
const importId = `__vite_ssr_import_${uid++}__`
97-
s.appendLeft(
97+
s.appendRight(
9898
node.start,
9999
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`
100100
)
@@ -115,6 +115,7 @@ async function ssrTransformScript(
115115
// import { baz } from 'foo' --> baz -> __import_foo__.baz
116116
// import * as ok from 'foo' --> ok -> __import_foo__
117117
if (node.type === 'ImportDeclaration') {
118+
s.remove(node.start, node.end)
118119
const importId = defineImport(node, node.source.value as string)
119120
for (const spec of node.specifiers) {
120121
if (spec.type === 'ImportSpecifier') {
@@ -129,7 +130,6 @@ async function ssrTransformScript(
129130
idToImportMap.set(spec.local.name, importId)
130131
}
131132
}
132-
s.remove(node.start, node.end)
133133
}
134134
}
135135

@@ -207,13 +207,11 @@ async function ssrTransformScript(
207207

208208
// export * from './foo'
209209
if (node.type === 'ExportAllDeclaration') {
210+
s.remove(node.start, node.end)
211+
const importId = defineImport(node, node.source.value as string)
210212
if (node.exported) {
211-
const importId = defineImport(node, node.source.value as string)
212-
s.remove(node.start, node.end)
213213
defineExport(node.end, node.exported.name, `${importId}`)
214214
} else {
215-
const importId = defineImport(node, node.source.value as string)
216-
s.remove(node.start, node.end)
217215
s.appendLeft(node.end, `${ssrExportAllKey}(${importId});`)
218216
}
219217
}

0 commit comments

Comments
 (0)