@@ -26,21 +26,32 @@ const wasmHelper = async (opts = {}, url: string) => {
2626 }
2727 result = await WebAssembly . instantiate ( bytes , opts )
2828 } else {
29- // https://github.com/mdn/webassembly-examples/issues/5
30- // WebAssembly.instantiateStreaming requires the server to provide the
31- // correct MIME type for .wasm files, which unfortunately doesn't work for
32- // a lot of static file servers, so we just work around it by getting the
33- // raw buffer.
34- const response = await fetch ( url )
35- const contentType = response . headers . get ( 'Content-Type' ) || ''
36- if (
37- 'instantiateStreaming' in WebAssembly &&
38- contentType . startsWith ( 'application/wasm' )
39- ) {
40- result = await WebAssembly . instantiateStreaming ( response , opts )
41- } else {
42- const buffer = await response . arrayBuffer ( )
29+ if ( typeof process !== 'undefined' && process . versions ?. node ) {
30+ const fs = await import ( 'node:fs/promises' )
31+ if ( url . startsWith ( '/@fs/' ) ) {
32+ url = url . slice ( 4 )
33+ } else if ( url . startsWith ( '/' ) ) {
34+ url = url . slice ( 1 )
35+ }
36+ const buffer = await fs . readFile ( url )
4337 result = await WebAssembly . instantiate ( buffer , opts )
38+ } else {
39+ // https://github.com/mdn/webassembly-examples/issues/5
40+ // WebAssembly.instantiateStreaming requires the server to provide the
41+ // correct MIME type for .wasm files, which unfortunately doesn't work for
42+ // a lot of static file servers, so we just work around it by getting the
43+ // raw buffer.
44+ const response = await fetch ( url )
45+ const contentType = response . headers . get ( 'Content-Type' ) || ''
46+ if (
47+ 'instantiateStreaming' in WebAssembly &&
48+ contentType . startsWith ( 'application/wasm' )
49+ ) {
50+ result = await WebAssembly . instantiateStreaming ( response , opts )
51+ } else {
52+ const buffer = await response . arrayBuffer ( )
53+ result = await WebAssembly . instantiate ( buffer , opts )
54+ }
4455 }
4556 }
4657 return result . instance
@@ -66,7 +77,7 @@ export const wasmHelperPlugin = (): Plugin => {
6677 return `export default ${ wasmHelperCode } `
6778 }
6879
69- const url = await fileToUrl ( this , id )
80+ const url = ( await fileToUrl ( this , id ) ) . split ( '?' ) [ 0 ]
7081
7182 return `
7283 import initWasm from "${ wasmHelperId } "
0 commit comments