[wasm] build: fix build with node/browser

This commit is contained in:
鲁树人 2024-09-14 19:55:53 +01:00
parent 29c5be137a
commit d385630499
5 changed files with 36 additions and 5 deletions

View File

@ -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':

View File

@ -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,

View File

@ -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<Buffer>|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);
}
}

8
um_wasm_loader/test.cjs Normal file
View File

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

8
um_wasm_loader/test.mjs Normal file
View File

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