Inline tree-sitter wasm and add runtime fallback (#11157)

This commit is contained in:
cornmander
2025-10-16 17:25:30 -07:00
committed by GitHub
parent 22f725eb08
commit dcf362bcf9
18 changed files with 966 additions and 282 deletions

View File

@@ -8,6 +8,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import { writeFileSync } from 'node:fs';
import { wasmLoader } from 'esbuild-plugin-wasm';
let esbuild;
try {
@@ -22,6 +23,37 @@ const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
const pkg = require(path.resolve(__dirname, 'package.json'));
function createWasmPlugins() {
const wasmBinaryPlugin = {
name: 'wasm-binary',
setup(build) {
build.onResolve({ filter: /\.wasm\?binary$/ }, (args) => {
const specifier = args.path.replace(/\?binary$/, '');
const resolveDir = args.resolveDir || '';
const isBareSpecifier =
!path.isAbsolute(specifier) &&
!specifier.startsWith('./') &&
!specifier.startsWith('../');
let resolvedPath;
if (isBareSpecifier) {
resolvedPath = require.resolve(specifier, {
paths: resolveDir ? [resolveDir, __dirname] : [__dirname],
});
} else {
resolvedPath = path.isAbsolute(specifier)
? specifier
: path.join(resolveDir, specifier);
}
return { path: resolvedPath, namespace: 'wasm-embedded' };
});
},
};
return [wasmBinaryPlugin, wasmLoader({ mode: 'embedded' })];
}
const external = [
'@lydell/node-pty',
'node-pty',
@@ -51,6 +83,7 @@ const cliConfig = {
define: {
'process.env.CLI_VERSION': JSON.stringify(pkg.version),
},
plugins: createWasmPlugins(),
alias: {
'is-in-ci': path.resolve(__dirname, 'packages/cli/src/patches/is-in-ci.ts'),
},
@@ -67,6 +100,7 @@ const a2aServerConfig = {
define: {
'process.env.CLI_VERSION': JSON.stringify(pkg.version),
},
plugins: createWasmPlugins(),
};
Promise.allSettled([