Skip to content

Commit a84998d

Browse files
committed
detect vitest
1 parent 642cfce commit a84998d

File tree

1 file changed

+11
-3
lines changed
  • packages/vite/src/node/plugins

1 file changed

+11
-3
lines changed

packages/vite/src/node/plugins/wasm.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ const instantiateFromFile = async (
6767
const instantiateFromFileCode = instantiateFromFile.toString()
6868

6969
export const wasmHelperPlugin = (): Plugin => {
70+
// Vitest may use the client environment to run tests in Node.js.
71+
// Try to detect it explicitly.
72+
let isVitest = false
73+
7074
return {
7175
name: 'vite:wasm-helper',
7276

77+
configResolved(config) {
78+
isVitest = config.plugins.some((plugin) => plugin.name === 'vitest')
79+
},
80+
7381
resolveId: {
7482
filter: { id: exactRegex(wasmHelperId) },
7583
handler(id) {
@@ -80,10 +88,10 @@ export const wasmHelperPlugin = (): Plugin => {
8088
load: {
8189
filter: { id: [exactRegex(wasmHelperId), wasmInitRE] },
8290
async handler(id) {
83-
const isServer = this.environment.config.consumer === 'server'
91+
const isNode = this.environment.config.consumer === 'server' || isVitest
8492

8593
if (id === wasmHelperId) {
86-
const instantiateFromUrl = isServer
94+
const instantiateFromUrl = isNode
8795
? instantiateFromFileCode
8896
: instantiateFromUrlCode
8997
return `
@@ -93,7 +101,7 @@ export default ${wasmHelperCode}
93101
}
94102

95103
id = id.split('?')[0]
96-
const url = isServer ? fsPathFromId(id) : await fileToUrl(this, id)
104+
const url = isNode ? fsPathFromId(id) : await fileToUrl(this, id)
97105

98106
return `
99107
import initWasm from "${wasmHelperId}"

0 commit comments

Comments
 (0)