@@ -38,8 +38,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
38
38
39
39
const hoistIndex = 0
40
40
41
- let hasInjected = false
42
-
43
41
// this will transform import statements into dynamic ones, if there are imports
44
42
// it will keep the import as is, if we don't need to mock anything
45
43
// 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
76
74
}
77
75
78
76
function defineExport ( position : number , name : string , local = name ) {
79
- hasInjected = true
80
77
s . appendLeft (
81
78
position ,
82
79
`\nObject.defineProperty(${ viInjectedKey } , "${ name } ", `
@@ -170,7 +167,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
170
167
// named hoistable/class exports
171
168
// export default function foo() {}
172
169
// export default class A {}
173
- hasInjected = true
174
170
const { name } = node . declaration . id
175
171
s . remove ( node . start , node . start + 15 /* 'export default '.length */ )
176
172
s . append (
@@ -180,7 +176,6 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
180
176
}
181
177
else {
182
178
// anonymous default exports
183
- hasInjected = true
184
179
s . update (
185
180
node . start ,
186
181
node . start + 14 /* 'export default'.length */ ,
@@ -196,13 +191,10 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
196
191
s . remove ( node . start , node . end )
197
192
const importId = defineImportAll ( node . source . value as string )
198
193
// hoist re-exports near the defined import so they are immediately exported
199
- if ( node . exported ) {
194
+ if ( node . exported )
200
195
defineExport ( hoistIndex , node . exported . name , `${ importId } ` )
201
- }
202
- else {
203
- hasInjected = true
196
+ else
204
197
s . appendLeft ( hoistIndex , `${ viExportAllHelper } (${ viInjectedKey } , ${ importId } );\n` )
205
- }
206
198
}
207
199
}
208
200
@@ -244,11 +236,10 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
244
236
} ,
245
237
} )
246
238
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 } }` )
252
243
253
244
return {
254
245
ast,
0 commit comments