fix(ssr): use appendRight for import (#9554)

This commit is contained in:
Bjorn Lu 2022-08-09 03:27:46 +08:00 committed by GitHub
parent a6b12f8c46
commit dfec6ca71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -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)`)

View File

@ -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`
)
@ -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') {
@ -129,7 +130,6 @@ async function ssrTransformScript(
idToImportMap.set(spec.local.name, importId)
}
}
s.remove(node.start, node.end)
}
}
@ -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});`)
}
}