@@ -67,9 +67,17 @@ const instantiateFromFile = async (
6767const instantiateFromFileCode = instantiateFromFile . toString ( )
6868
6969export 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