From d385630499480f12e8d19a196ffeb34d2a0d9db3 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 19:55:53 +0100 Subject: [PATCH] [wasm] build: fix build with node/browser --- um_wasm_loader/pnpm-lock.yaml | 3 +++ um_wasm_loader/rollup.config.mjs | 7 ++++++- um_wasm_loader/src/loader.mjs | 15 +++++++++++---- um_wasm_loader/test.cjs | 8 ++++++++ um_wasm_loader/test.mjs | 8 ++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 um_wasm_loader/test.cjs create mode 100644 um_wasm_loader/test.mjs diff --git a/um_wasm_loader/pnpm-lock.yaml b/um_wasm_loader/pnpm-lock.yaml index f0f5b1c..111cd2f 100644 --- a/um_wasm_loader/pnpm-lock.yaml +++ b/um_wasm_loader/pnpm-lock.yaml @@ -257,11 +257,13 @@ snapshots: dependencies: '@rollup/pluginutils': 5.1.0(rollup@4.21.2) magic-string: 0.30.11 + optionalDependencies: rollup: 4.21.2 '@rollup/plugin-wasm@6.2.2(rollup@4.21.2)': dependencies: '@rollup/pluginutils': 5.1.0(rollup@4.21.2) + optionalDependencies: rollup: 4.21.2 '@rollup/pluginutils@5.1.0(rollup@4.21.2)': @@ -269,6 +271,7 @@ snapshots: '@types/estree': 1.0.5 estree-walker: 2.0.2 picomatch: 2.3.1 + optionalDependencies: rollup: 4.21.2 '@rollup/rollup-android-arm-eabi@4.21.2': diff --git a/um_wasm_loader/rollup.config.mjs b/um_wasm_loader/rollup.config.mjs index 3d0d46a..db00b8f 100644 --- a/um_wasm_loader/rollup.config.mjs +++ b/um_wasm_loader/rollup.config.mjs @@ -5,7 +5,12 @@ import { dts } from 'rollup-plugin-dts'; function makePlugins({ sync }) { const plugins = []; - plugins.push(wasm({ sync: sync ? ['pkg/um_wasm_bg.wasm'] : [] })); + plugins.push( + wasm({ + sync: sync ? ['pkg/um_wasm_bg.wasm'] : [], + fileName: 'um_wasm_bg.wasm', + }), + ); plugins.push( replace({ preventAssignment: true, diff --git a/um_wasm_loader/src/loader.mjs b/um_wasm_loader/src/loader.mjs index 52c204e..6d8c15b 100644 --- a/um_wasm_loader/src/loader.mjs +++ b/um_wasm_loader/src/loader.mjs @@ -1,15 +1,22 @@ import umWasm from '../pkg/um_wasm_bg.wasm'; import { __wbg_init, initSync } from '../pkg/um_wasm.js'; + export * from '../pkg/um_wasm.js'; function loader() { if (process.env.UMC_INLINE_BUILD === '1') { initSync({ module: umWasm() }); - return Promise.resolve(undefined); + return Promise.resolve(true); } else { - return umWasm().then(async (wasm) => { - await __wbg_init({ module_or_path: wasm }); - }); + /** @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)), + ); + } + return __wbg_init({ module_or_path: wasm }).then(() => true); } } diff --git a/um_wasm_loader/test.cjs b/um_wasm_loader/test.cjs new file mode 100644 index 0000000..5e4c422 --- /dev/null +++ b/um_wasm_loader/test.cjs @@ -0,0 +1,8 @@ +const { NCMFile, ready } = require('./dist/loader.js'); + +ready.then(() => { + const ncm = new NCMFile(); + let n = ncm.open(new Uint8Array([])); + console.assert(n !== 0, 'n should not be 0', { n }); + console.log('cjs test ok'); +}); diff --git a/um_wasm_loader/test.mjs b/um_wasm_loader/test.mjs new file mode 100644 index 0000000..24b46f4 --- /dev/null +++ b/um_wasm_loader/test.mjs @@ -0,0 +1,8 @@ +import { NCMFile, ready } from './dist/loader.mjs'; + +ready.then(() => { + const ncm = new NCMFile(); + let n = ncm.open(new Uint8Array([])); + console.assert(n !== 0, 'n should not be 0', { n }); + console.log('mjs test ok'); +});