Skip to content

Commit

Permalink
fix(ssr): hoist re-exports with imports (#12530)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Mar 22, 2023
1 parent 63524ba commit 45549e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -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__);
"
`)
})

Expand All @@ -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__);
"
`)
})

Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -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}`,
)
Expand Down Expand Up @@ -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`)
}
}
}
Expand Down

0 comments on commit 45549e4

Please sign in to comment.