Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): use appendRight for import #9554

Merged
merged 1 commit into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
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
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