From cca348161dcecb80ecde7cd82eb366aff69e93ce Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 22 Mar 2023 15:51:46 +0800 Subject: [PATCH] fix(ssr): hoist re-exports with imports --- .../vite/src/node/ssr/__tests__/ssrTransform.spec.ts | 9 ++++++--- packages/vite/src/node/ssr/ssrTransform.ts | 8 +++++--- 2 files changed, 11 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 56e7310602b320..b71b6262e1f009 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -103,9 +103,11 @@ test('export * from', async () => { ), ).toMatchInlineSnapshot(` "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\"); - const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\"); __vite_ssr_exportAll__(__vite_ssr_import_0__); - __vite_ssr_exportAll__(__vite_ssr_import_1__);" + const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\"); + __vite_ssr_exportAll__(__vite_ssr_import_1__); + + " `) }) @@ -132,7 +134,8 @@ test('export then import minified', async () => { ).toMatchInlineSnapshot(` "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\"); const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"vue\\"); - __vite_ssr_exportAll__(__vite_ssr_import_1__);" + __vite_ssr_exportAll__(__vite_ssr_import_1__); + " `) }) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index c5ebdd9ac80f52..b7d050f6479dd4 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -164,9 +164,10 @@ async function ssrTransformScript( if (node.source) { // export { foo, bar } from './foo' const importId = defineImport(node.source.value as string) + // hoist re-exports near the defined import so they are immediately exported for (const spec of node.specifiers) { defineExport( - node.end, + 0, spec.exported.name, `${importId}.${spec.local.name}`, ) @@ -213,10 +214,11 @@ async function ssrTransformScript( if (node.type === 'ExportAllDeclaration') { s.remove(node.start, node.end) const importId = defineImport(node.source.value as string) + // hoist re-exports near the defined import so they are immediately exported if (node.exported) { - defineExport(node.end, node.exported.name, `${importId}`) + defineExport(0, node.exported.name, `${importId}`) } else { - s.appendLeft(node.end, `${ssrExportAllKey}(${importId});`) + s.appendLeft(0, `${ssrExportAllKey}(${importId});\n`) } } }