Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): properly transform export default with expressions #7705

Merged
merged 2 commits into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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