Skip to content

Commit

Permalink
fix(ssr): Transform named default exports without altering scope (#4053)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
GrygrFlzr and antfu committed Jul 2, 2021
1 parent a0a80f8 commit 5211aaf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
50 changes: 49 additions & 1 deletion packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -211,8 +211,56 @@ test('should declare variable for imported super class', async () => {
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = __vite_ssr_import__(\\"./dependency\\")
const Foo = __vite_ssr_import_0__.Foo;
__vite_ssr_exports__.default = class A extends Foo {}
class A extends Foo {}
class B extends Foo {}
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A })
Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }})"
`)
})

// #4049
test('should handle default export variants', async () => {
// default anonymous functions
expect(
(await ssrTransform(`export default function() {}\n`, null, null)).code
).toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = function() {}
"
`)
// default anonymous class
expect((await ssrTransform(`export default class {}\n`, null, null)).code)
.toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = class {}
"
`)
// default named functions
expect(
(
await ssrTransform(
`export default function foo() {}\n` +
`foo.prototype = Object.prototype;`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"function foo() {}
foo.prototype = Object.prototype;
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: foo })"
`)
// default named classes
expect(
(
await ssrTransform(
`export default class A {}\n` + `export class B extends A {}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"class A {}
class B extends A {}
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A })
Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }})"
`)
})
Expand Down
23 changes: 18 additions & 5 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -124,11 +124,24 @@ export async function ssrTransform(

// default export
if (node.type === 'ExportDefaultDeclaration') {
s.overwrite(
node.start,
node.start + 14,
`${ssrModuleExportsKey}.default =`
)
if ('id' in node.declaration && node.declaration.id) {
// named hoistable/class exports
// export default function foo() {}
// export default class A {}
const { name } = node.declaration.id
s.remove(node.start, node.start + 15 /* 'export default '.length */)
s.append(
`\nObject.defineProperty(${ssrModuleExportsKey}, "default", ` +
`{ enumerable: true, value: ${name} })`
)
} else {
// anonymous default exports
s.overwrite(
node.start,
node.start + 14 /* 'export default'.length */,
`${ssrModuleExportsKey}.default =`
)
}
}

// export * from './foo'
Expand Down

0 comments on commit 5211aaf

Please sign in to comment.