feat: add wasm build config (#2985)

This commit is contained in:
顾盼 2026-04-09 14:21:00 +08:00 committed by GitHub
parent 32e7b632b8
commit 44c596cd14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 131 additions and 218 deletions

View file

@ -7,7 +7,8 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import { writeFileSync, rmSync } from 'node:fs';
import { writeFileSync, rmSync, readFileSync } from 'node:fs';
import { wasmLoader } from 'esbuild-plugin-wasm';
let esbuild;
try {
@ -25,6 +26,35 @@ const pkg = require(path.resolve(__dirname, 'package.json'));
// Clean dist directory (cross-platform)
rmSync(path.resolve(__dirname, 'dist'), { recursive: true, force: true });
/**
* Resolve `import X from '*.wasm?binary'` imports to an inline Uint8Array.
*
* The `?binary` suffix is a build-time hint: at bundle time (esbuild) the WASM
* bytes are embedded as base64 and exported as a default Uint8Array, so no
* external vendor files are needed at runtime. In source / transpiled mode
* the dynamic import throws and the caller falls back to reading from
* node_modules via `require.resolve`.
*/
const wasmBinaryPlugin = {
name: 'wasm-binary',
setup(build) {
build.onResolve({ filter: /\.wasm\?binary$/ }, (args) => {
const specifier = args.path.replace(/\?binary$/, '');
const localRequire = createRequire(
path.resolve(args.resolveDir || __dirname, '_dummy_.js'),
);
return {
path: localRequire.resolve(specifier),
namespace: 'wasm-binary',
};
});
build.onLoad({ filter: /.*/, namespace: 'wasm-binary' }, (args) => {
const contents = readFileSync(args.path);
return { contents, loader: 'binary' };
});
},
};
const external = [
'@lydell/node-pty',
'node-pty',
@ -75,6 +105,7 @@ esbuild
global: 'globalThis',
},
loader: { '.node': 'file' },
plugins: [wasmBinaryPlugin, wasmLoader({ mode: 'embedded' })],
metafile: true,
write: true,
keepNames: true,