Skip to content

Commit

Permalink
fix(wasm): support inlined WASM in Node < v16 (fix #8620) (#8622)
Browse files Browse the repository at this point in the history
  • Loading branch information
pastelmind committed Jun 17, 2022
1 parent e10530b commit f586b14
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/vite/src/node/plugins/wasm.ts
Expand Up @@ -7,11 +7,21 @@ const wasmHelperId = '/__vite-wasm-helper'
const wasmHelper = async (opts = {}, url: string) => {
let result
if (url.startsWith('data:')) {
// @ts-ignore
const binaryString = atob(url.replace(/^data:.*?base64,/, ''))
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
const urlContent = url.replace(/^data:.*?base64,/, '')
let bytes
if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
bytes = Buffer.from(urlContent, 'base64')
} else if (typeof atob === 'function') {
// @ts-ignore
const binaryString = atob(urlContent)
bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
} else {
throw new Error(
'Failed to decode base64-encoded data URL, Buffer and atob are not supported'
)
}
// @ts-ignore
result = await WebAssembly.instantiate(bytes, opts)
Expand Down

0 comments on commit f586b14

Please sign in to comment.