Skip to content

Commit

Permalink
fix(ssr): use appendRight for import (#9554)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Aug 8, 2022
1 parent a6b12f8 commit dfec6ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 12 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ test('export default', async () => {
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
})

test('export then import minified', async () => {
expect(
await ssrTransformSimpleCode(
`export * from 'vue';import {createApp} from 'vue';`
)
).toMatchInlineSnapshot(`
"const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\");
__vite_ssr_exportAll__(__vite_ssr_import_1__);const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
"
`)
})

test('import.meta', async () => {
expect(
await ssrTransformSimpleCode(`console.log(import.meta.url)`)
Expand Down
10 changes: 4 additions & 6 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function ssrTransformScript(
function defineImport(node: Node, source: string) {
deps.add(source)
const importId = `__vite_ssr_import_${uid++}__`
s.appendLeft(
s.appendRight(
node.start,
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`
)
Expand All @@ -115,6 +115,7 @@ async function ssrTransformScript(
// import { baz } from 'foo' --> baz -> __import_foo__.baz
// import * as ok from 'foo' --> ok -> __import_foo__
if (node.type === 'ImportDeclaration') {
s.remove(node.start, node.end)
const importId = defineImport(node, node.source.value as string)
for (const spec of node.specifiers) {
if (spec.type === 'ImportSpecifier') {
Expand All @@ -129,7 +130,6 @@ async function ssrTransformScript(
idToImportMap.set(spec.local.name, importId)
}
}
s.remove(node.start, node.end)
}
}

Expand Down Expand Up @@ -207,13 +207,11 @@ async function ssrTransformScript(

// export * from './foo'
if (node.type === 'ExportAllDeclaration') {
s.remove(node.start, node.end)
const importId = defineImport(node, node.source.value as string)
if (node.exported) {
const importId = defineImport(node, node.source.value as string)
s.remove(node.start, node.end)
defineExport(node.end, node.exported.name, `${importId}`)
} else {
const importId = defineImport(node, node.source.value as string)
s.remove(node.start, node.end)
s.appendLeft(node.end, `${ssrExportAllKey}(${importId});`)
}
}
Expand Down

0 comments on commit dfec6ca

Please sign in to comment.