@@ -38,8 +38,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
3838
3939 const hoistIndex = 0
4040
41- let hasInjected = false
42-
4341 // this will transform import statements into dynamic ones, if there are imports
4442 // it will keep the import as is, if we don't need to mock anything
4543 // in browser environment it will wrap the module value with "vitest_wrap_module" function
@@ -76,7 +74,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
7674 }
7775
7876 function defineExport ( position : number , name : string , local = name ) {
79- hasInjected = true
8077 s . appendLeft (
8178 position ,
8279 `\nObject.defineProperty(${ viInjectedKey } , "${ name } ", `
@@ -170,7 +167,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
170167 // named hoistable/class exports
171168 // export default function foo() {}
172169 // export default class A {}
173- hasInjected = true
174170 const { name } = node . declaration . id
175171 s . remove ( node . start , node . start + 15 /* 'export default '.length */ )
176172 s . append (
@@ -180,7 +176,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
180176 }
181177 else {
182178 // anonymous default exports
183- hasInjected = true
184179 s . update (
185180 node . start ,
186181 node . start + 14 /* 'export default'.length */ ,
@@ -196,13 +191,10 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
196191 s . remove ( node . start , node . end )
197192 const importId = defineImportAll ( node . source . value as string )
198193 // hoist re-exports near the defined import so they are immediately exported
199- if ( node . exported ) {
194+ if ( node . exported )
200195 defineExport ( hoistIndex , node . exported . name , `${ importId } ` )
201- }
202- else {
203- hasInjected = true
196+ else
204197 s . appendLeft ( hoistIndex , `${ viExportAllHelper } (${ viInjectedKey } , ${ importId } );\n` )
205- }
206198 }
207199 }
208200
@@ -244,11 +236,10 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
244236 } ,
245237 } )
246238
247- if ( hasInjected ) {
248- // make sure "__vi_injected__" is declared as soon as possible
249- s . prepend ( `const ${ viInjectedKey } = { [Symbol.toStringTag]: "Module" };\n` )
250- s . append ( `\nexport { ${ viInjectedKey } }` )
251- }
239+ // make sure "__vi_injected__" is declared as soon as possible
240+ // prepend even if file doesn't export anything
241+ s . prepend ( `const ${ viInjectedKey } = { [Symbol.toStringTag]: "Module" };\n` )
242+ s . append ( `\nexport { ${ viInjectedKey } }` )
252243
253244 return {
254245 ast,
0 commit comments