From 80a3239d326f914371109f66970ff764b1297ca8 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 5 Aug 2022 20:21:47 +0800 Subject: [PATCH] fix(ssr): use appendRight for import --- .../vite/src/node/ssr/__tests__/ssrTransform.spec.ts | 12 ++++++++++++ packages/vite/src/node/ssr/ssrTransform.ts | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index cc5b19522ef9a5..a4e74390d3d3c2 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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)`) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 2c38c53e74714e..bf1633395ae992 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -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});`) } }