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(browser): keep default export when rewriting exports #3389

Merged
merged 2 commits into from May 17, 2023
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
12 changes: 3 additions & 9 deletions packages/browser/src/node/esmInjector.ts
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
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
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
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