diff --git a/packages/browser/src/node/esmInjector.ts b/packages/browser/src/node/esmInjector.ts index 4aa977c089a7..a8e0514b1987 100644 --- a/packages/browser/src/node/esmInjector.ts +++ b/packages/browser/src/node/esmInjector.ts @@ -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 @@ -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`) } } diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 174408d8e299..10e5caeeea5f 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -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) }, }, ] diff --git a/packages/ui/client/auto-imports.d.ts b/packages/ui/client/auto-imports.d.ts index 3facfb050412..d6b718892a7d 100644 --- a/packages/ui/client/auto-imports.d.ts +++ b/packages/ui/client/auto-imports.d.ts @@ -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'] diff --git a/test/core/test/injector-esm.test.ts b/test/core/test/injector-esm.test.ts index e08ac7cac236..af034297940f 100644 --- a/test/core/test/injector-esm.test.ts +++ b/test/core/test/injector-esm.test.ts @@ -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 () => { @@ -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__ }" `) }) @@ -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 @@ -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 @@ -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__ }" `) @@ -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__ }" `) })