From 59fafa9e2a9af098c3d6666707f7afbdcbb45d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sat, 14 Sep 2024 20:09:38 +0100 Subject: [PATCH] [wasm] build: fix detection for environment --- um_wasm_loader/build.js | 3 --- um_wasm_loader/src/loader.mjs | 17 +++++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/um_wasm_loader/build.js b/um_wasm_loader/build.js index 62e6e4d..d689266 100644 --- a/um_wasm_loader/build.js +++ b/um_wasm_loader/build.js @@ -92,9 +92,6 @@ async function main() { // Ask rollup to build bundles. await run(['pnpm', 'build:bundle']); await run(['pnpm', 'exec', 'prettier', '--ignore-path', '', '-w', 'dist/loader.d.ts']); - await replaceFileByRegex(path.join(wasmDistDir, 'loader.mjs'), [ - ['fetch(filepath)', 'fetch(new URL(filepath, import.meta.url))'], - ]); } main() diff --git a/um_wasm_loader/src/loader.mjs b/um_wasm_loader/src/loader.mjs index 6d8c15b..d4f6de3 100644 --- a/um_wasm_loader/src/loader.mjs +++ b/um_wasm_loader/src/loader.mjs @@ -8,14 +8,15 @@ function loader() { initSync({ module: umWasm() }); return Promise.resolve(true); } else { - /** @type {Promise|undefined} */ - let wasm = undefined; - if (typeof document === 'undefined') { - // node - wasm = import('node:f' + 's/promises').then((fs) => - fs.readFile(new URL('../pkg/um_wasm_bg.wasm', import.meta.url)), - ); - } + const url = new URL('um_wasm_bg.wasm', import.meta.url); + const wasm = + url.protocol === 'file:' + ? import('node:f' + 's/promises') + .then((fs) => fs.readFile(url)) + .catch((err) => { + console.log('read wasm failed', err); + }) + : undefined; return __wbg_init({ module_or_path: wasm }).then(() => true); } }