Skip to content

Commit

Permalink
fix(ssr): properly transform export default with expressions (#7705)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 13, 2022
1 parent 48e038c commit d6830e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -657,3 +657,26 @@ export function fn1() {
"
`)
})

// https://github.com/vitest-dev/vitest/issues/1141
test('export default expression', async () => {
// esbuild transform result of following TS code
// export default <MyFn> function getRandom() {
// return Math.random()
// }
const code = `
export default (function getRandom() {
return Math.random();
});
`.trim()

expect((await ssrTransform(code, null, null)).code).toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = (function getRandom() {
return Math.random();
});"
`)

expect(
(await ssrTransform(`export default (class A {});`, null, null)).code
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = (class A {});"`)
})
7 changes: 6 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -143,7 +143,12 @@ export async function ssrTransform(

// default export
if (node.type === 'ExportDefaultDeclaration') {
if ('id' in node.declaration && node.declaration.id) {
const expressionTypes = ['FunctionExpression', 'ClassExpression']
if (
'id' in node.declaration &&
node.declaration.id &&
!expressionTypes.includes(node.declaration.type)
) {
// named hoistable/class exports
// export default function foo() {}
// export default class A {}
Expand Down

0 comments on commit d6830e3

Please sign in to comment.