Skip to content

Commit f586b14

Browse files
authored
fix(wasm): support inlined WASM in Node < v16 (fix #8620) (#8622)
1 parent e10530b commit f586b14

File tree

1 file changed

+15
-5
lines changed
  • packages/vite/src/node/plugins

1 file changed

+15
-5
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ const wasmHelperId = '/__vite-wasm-helper'
77
const wasmHelper = async (opts = {}, url: string) => {
88
let result
99
if (url.startsWith('data:')) {
10-
// @ts-ignore
11-
const binaryString = atob(url.replace(/^data:.*?base64,/, ''))
12-
const bytes = new Uint8Array(binaryString.length)
13-
for (let i = 0; i < binaryString.length; i++) {
14-
bytes[i] = binaryString.charCodeAt(i)
10+
const urlContent = url.replace(/^data:.*?base64,/, '')
11+
let bytes
12+
if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
13+
bytes = Buffer.from(urlContent, 'base64')
14+
} else if (typeof atob === 'function') {
15+
// @ts-ignore
16+
const binaryString = atob(urlContent)
17+
bytes = new Uint8Array(binaryString.length)
18+
for (let i = 0; i < binaryString.length; i++) {
19+
bytes[i] = binaryString.charCodeAt(i)
20+
}
21+
} else {
22+
throw new Error(
23+
'Failed to decode base64-encoded data URL, Buffer and atob are not supported'
24+
)
1525
}
1626
// @ts-ignore
1727
result = await WebAssembly.instantiate(bytes, opts)

0 commit comments

Comments
 (0)