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'); +});