Skip to content

Commit 6501d2e

Browse files
authored
fix(browser): keep default export when rewriting exports (#3389)
1 parent b354bc1 commit 6501d2e

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

packages/browser/src/node/esmInjector.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ const skipHijack = [
1515
/vite\/dist\/client/,
1616
]
1717

18-
interface Options {
19-
cacheDir: string
20-
}
21-
2218
// this is basically copypaste from Vite SSR
2319
// this method transforms all import and export statements into `__vi_injected__` variable
2420
// to allow spying on them. this can be disabled by setting `slowHijackESM` to `false`
25-
export function injectVitestModule(code: string, id: string, parse: (code: string, options: any) => AcornNode, options: Options) {
21+
export function injectVitestModule(code: string, id: string, parse: (code: string, options: any) => AcornNode) {
2622
if (skipHijack.some(skip => id.match(skip)))
2723
return
2824

@@ -195,10 +191,8 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin
195191
node.start + 14 /* 'export default'.length */,
196192
`${viInjectedKey}.default =`,
197193
)
198-
if (id.startsWith(options.cacheDir)) {
199-
// keep export default for optimized dependencies
200-
s.append(`\nexport default { ${viInjectedKey}: ${viInjectedKey}.default };\n`)
201-
}
194+
// keep export default for optimized dependencies
195+
s.append(`\nexport default { ${viInjectedKey}: ${viInjectedKey}.default };\n`)
202196
}
203197
}
204198

packages/browser/src/node/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ export default (project: any, base = '/'): Plugin[] => {
8585
const hijackESM = project.config.browser.slowHijackESM ?? false
8686
if (!hijackESM)
8787
return
88-
return injectVitestModule(source, id, this.parse, {
89-
cacheDir: project.server.config.cacheDir,
90-
})
88+
return injectVitestModule(source, id, this.parse)
9189
},
9290
},
9391
]

packages/ui/client/auto-imports.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ declare global {
9191
const throttledWatch: typeof import('@vueuse/core')['throttledWatch']
9292
const toRaw: typeof import('vue')['toRaw']
9393
const toReactive: typeof import('@vueuse/core')['toReactive']
94-
const toRef: typeof import('@vueuse/core')['toRef']
94+
const toRef: typeof import('vue')['toRef']
9595
const toRefs: typeof import('vue')['toRefs']
9696
const toValue: typeof import('@vueuse/core')['toValue']
9797
const triggerRef: typeof import('vue')['triggerRef']

test/core/test/injector-esm.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ function parse(code: string, options: any) {
88
}
99

1010
function injectSimpleCode(code: string) {
11-
return injectVitestModule(code, '/test.js', parse, {
12-
cacheDir: '/tmp',
13-
})?.code
11+
return injectVitestModule(code, '/test.js', parse)?.code
1412
}
1513

1614
test('default import', async () => {
@@ -148,6 +146,8 @@ test('export default', async () => {
148146
).toMatchInlineSnapshot(`
149147
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
150148
__vi_inject__.default = {}
149+
export default { __vi_inject__: __vi_inject__.default };
150+
151151
export { __vi_inject__ }"
152152
`)
153153
})
@@ -320,6 +320,8 @@ test('should handle default export variants', async () => {
320320
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
321321
__vi_inject__.default = function() {}
322322
323+
export default { __vi_inject__: __vi_inject__.default };
324+
323325
export { __vi_inject__ }"
324326
`)
325327
// default anonymous class
@@ -328,6 +330,8 @@ test('should handle default export variants', async () => {
328330
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
329331
__vi_inject__.default = class {}
330332
333+
export default { __vi_inject__: __vi_inject__.default };
334+
331335
export { __vi_inject__ }"
332336
`)
333337
// default named functions
@@ -743,6 +747,8 @@ export default (function getRandom() {
743747
__vi_inject__.default = (function getRandom() {
744748
return Math.random();
745749
});
750+
export default { __vi_inject__: __vi_inject__.default };
751+
746752
export { __vi_inject__ }"
747753
`)
748754

@@ -751,6 +757,8 @@ export default (function getRandom() {
751757
).toMatchInlineSnapshot(`
752758
"const __vi_inject__ = { [Symbol.toStringTag]: \\"Module\\" };
753759
__vi_inject__.default = (class A {});
760+
export default { __vi_inject__: __vi_inject__.default };
761+
754762
export { __vi_inject__ }"
755763
`)
756764
})

0 commit comments

Comments
 (0)