diff --git a/src/lexer.js b/src/lexer.js index d47f499..45e4820 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -90,13 +90,28 @@ function copyLE (src, outBuf16) { outBuf16[i] = src.charCodeAt(i++); } +const loadWasm = (typeof EXTERNAL_PATH === "string" && (async () => { + const { readFile } = await import('node:fs/promises'); + const { fileURLToPath } = await import('node:url'); + return readFile(fileURLToPath( + import.meta.resolve("undefined/dist/lexer.wasm") + )); +})) || (async () => { + const binary = WASM_BINARY + if (typeof window !== "undefined" && typeof atob === "function") { + return Uint8Array.from(atob(binary), (x) => x.charCodeAt(0)); + } else { + return Buffer.from(binary, "base64"); + } +}); + let initPromise; export function init () { if (initPromise) return initPromise; return initPromise = (async () => { const compiled = await WebAssembly.compile( - (await import('./loadWasm')).default + await loadWasm() ) const { exports } = await WebAssembly.instantiate(compiled); wasm = exports; diff --git a/src/loadWasm.js b/src/loadWasm.js deleted file mode 100644 index dea7ef3..0000000 --- a/src/loadWasm.js +++ /dev/null @@ -1,16 +0,0 @@ -export default (EXTERNAL_PATH - && await (await import('node:fs/promises')) - .readFile( - (await import('node:url')) - .fileURLToPath( - import.meta.resolve(EXTERNAL_PATH + '/dist/lexer.wasm') - ) - ) -) - || (WASM_BINARY - && (binary => - typeof window !== 'undefined' && typeof atob === 'function' - ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) - : Buffer.from(binary, 'base64') - )(WASM_BINARY) - )