Merge pull request #18036 from NousResearch/fix/bundle-size

ui-tui: bundle with esbuild, drop runtime node_modules
This commit is contained in:
ethernet 2026-05-11 17:46:19 -04:00 committed by GitHub
commit 825bd50e6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 122 additions and 136 deletions

View file

@ -41,7 +41,7 @@ From the repo root, the normal path is:
hermes --tui
```
The CLI expects `ui-tui/node_modules` to exist. If the TUI deps are missing:
The CLI expects `ui-tui/dist/entry.js` to exist, or the whole source code available in which to run `npm install` and `npm run dev`.
```bash
cd ui-tui

View file

@ -26,6 +26,7 @@
"@typescript-eslint/eslint-plugin": "^8",
"@typescript-eslint/parser": "^8",
"babel-plugin-react-compiler": "^1.0.0",
"esbuild": "~0.27.0",
"eslint": "^9",
"eslint-plugin-perfectionist": "^5",
"eslint-plugin-react": "^7",

View file

@ -6,8 +6,7 @@
"scripts": {
"dev": "npm run build --prefix packages/hermes-ink && tsx --watch src/entry.tsx",
"start": "tsx src/entry.tsx",
"build": "npm run build --prefix packages/hermes-ink && tsc -p tsconfig.build.json && npm run build:compile && chmod +x dist/entry.js",
"build:compile": "babel dist --out-dir dist --config-file ./babel.compiler.config.cjs --extensions .js --keep-file-extension",
"build": "node scripts/build.mjs",
"type-check": "tsc --noEmit -p tsconfig.json",
"lint": "eslint src/ packages/",
"lint:fix": "eslint src/ packages/ --fix",
@ -35,6 +34,7 @@
"@typescript-eslint/eslint-plugin": "^8",
"@typescript-eslint/parser": "^8",
"babel-plugin-react-compiler": "^1.0.0",
"esbuild": "~0.27.0",
"eslint": "^9",
"eslint-plugin-perfectionist": "^5",
"eslint-plugin-react": "^7",

61
ui-tui/scripts/build.mjs Normal file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env node
// Bundles src/entry.tsx into a single self-contained dist/entry.js.
// No runtime node_modules needed.
import { build } from 'esbuild'
import { readFileSync, writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
const here = dirname(fileURLToPath(import.meta.url))
const root = resolve(here, '..')
const out = resolve(root, 'dist/entry.js')
// `react-devtools-core` is only imported when DEV=true at runtime (Ink dev
// mode). Stub it out so the bundle doesn't carry the dep.
const stubDevtools = {
name: 'stub-react-devtools-core',
setup(b) {
b.onResolve({ filter: /^react-devtools-core$/ }, args => ({
path: args.path,
namespace: 'stub-devtools'
}))
b.onLoad({ filter: /.*/, namespace: 'stub-devtools' }, () => ({
contents: 'export default { initialize() {}, connectToDevTools() {} }',
loader: 'js'
}))
}
}
await build({
entryPoints: [resolve(root, 'src/entry.tsx')],
bundle: true,
platform: 'node',
format: 'esm',
target: 'node20',
outfile: out,
jsx: 'automatic',
jsxImportSource: 'react',
// Skip the prebuilt @hermes/ink bundle — esbuild's __esm helper doesn't
// await nested async init, which breaks lazy-initialized exports like
// `render`. Bundling from source sidesteps that.
alias: { '@hermes/ink': resolve(root, 'packages/hermes-ink/src/entry-exports.ts') },
plugins: [stubDevtools],
// Some transitive deps use CommonJS `require(...)` at runtime. ESM bundles
// don't get a `require` binding automatically, so we inject one.
banner: {
js: "import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);"
},
logLevel: 'info'
})
// esbuild preserves the shebang from src/entry.tsx into the bundle, but Nix's
// patchShebangs phase mangles `/usr/bin/env -S node --foo --bar` (it strips
// the `node` token, leaving a broken interpreter). The hermes_cli launcher
// always invokes this file as `node dist/entry.js` anyway, so the shebang is
// redundant — strip it.
const body = readFileSync(out, 'utf8')
if (body.startsWith('#!')) {
writeFileSync(out, body.slice(body.indexOf('\n') + 1))
}
console.log(`built ${out}`)