Skip to content

Commit

Permalink
fix(browser): keep default export when rewriting exports (#3389)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 17, 2023
1 parent b354bc1 commit 6501d2e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
12 changes: 3 additions & 9 deletions packages/browser/src/node/esmInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ const skipHijack = [
/vite\/dist\/client/,
]

interface Options {
cacheDir: string
}

// this is basically copypaste from Vite SSR
// this method transforms all import and export statements into `__vi_injected__` variable
// to allow spying on them. this can be disabled by setting `slowHijackESM` to `false`
export function injectVitestModule(code: string, id: string, parse: (code: string, options: any) => AcornNode, options: Options) {
export function injectVitestModule(code: string, id: string, parse: (code: string, options: any) => AcornNode) {
if (skipHijack.some(skip => id.match(skip)))
return

Expand Down Expand Up @@ -195,10 +191,8 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin
node.start + 14 /* 'export default'.length */,
`${viInjectedKey}.default =`,
)
if (id.startsWith(options.cacheDir)) {
// keep export default for optimized dependencies
s.append(`\nexport default { ${viInjectedKey}: ${viInjectedKey}.default };\n`)
}
// keep export default for optimized dependencies
s.append(`\nexport default { ${viInjectedKey}: ${viInjectedKey}.default };\n`)
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ export default (project: any, base = '/'): Plugin[] => {
const hijackESM = project.config.browser.slowHijackESM ?? false
if (!hijackESM)
return
return injectVitestModule(source, id, this.parse, {
cacheDir: project.server.config.cacheDir,
})
return injectVitestModule(source, id, this.parse)
},
},
]
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/client/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ declare global {
const throttledWatch: typeof import('@vueuse/core')['throttledWatch']
const toRaw: typeof import('vue')['toRaw']
const toReactive: typeof import('@vueuse/core')['toReactive']
const toRef: typeof import('@vueuse/core')['toRef']
const toRef: typeof import('vue')['toRef']
const toRefs: typeof import('vue')['toRefs']
const toValue: typeof import('@vueuse/core')['toValue']
const triggerRef: typeof import('vue')['triggerRef']
Expand Down
14 changes: 11 additions & 3 deletions test/core/test/injector-esm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ function parse(code: string, options: any) {
}

function injectSimpleCode(code: string) {
return injectVitestModule(code, '/test.js', parse, {
cacheDir: '/tmp',
})?.code
return injectVitestModule(code, '/test.js', parse)?.code
}

test('default import', async () => {
Expand Down Expand Up @@ -148,6 +146,8 @@ test('export default', async () => {
).toMatchInlineSnapshot(`
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
__vi_inject__.default = {}
export default { __vi_inject__: __vi_inject__.default };
export { __vi_inject__ }"
`)
})
Expand Down Expand Up @@ -320,6 +320,8 @@ test('should handle default export variants', async () => {
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
__vi_inject__.default = function() {}
export default { __vi_inject__: __vi_inject__.default };
export { __vi_inject__ }"
`)
// default anonymous class
Expand All @@ -328,6 +330,8 @@ test('should handle default export variants', async () => {
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
__vi_inject__.default = class {}
export default { __vi_inject__: __vi_inject__.default };
export { __vi_inject__ }"
`)
// default named functions
Expand Down Expand Up @@ -743,6 +747,8 @@ export default (function getRandom() {
__vi_inject__.default = (function getRandom() {
return Math.random();
});
export default { __vi_inject__: __vi_inject__.default };
export { __vi_inject__ }"
`)

Expand All @@ -751,6 +757,8 @@ export default (function getRandom() {
).toMatchInlineSnapshot(`
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
__vi_inject__.default = (class A {});
export default { __vi_inject__: __vi_inject__.default };
export { __vi_inject__ }"
`)
})
Expand Down

0 comments on commit 6501d2e

Please sign in to comment.