Repository navigation
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
103 lines (92 loc) · 10.3 KB
/
Copy pathesbuild.config.mjs
File metadata and controls
103 lines (92 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as esbuild from 'esbuild'
import { copyFileSync, readFileSync, writeFileSync } from 'node:fs'
import { clearBuildInfo, computeBuildStamp, writeBuildInfo } from './scripts/build-stamp.mjs'
import { buildDefines, CJS_CLIENT, ENTRY_POINTS, EXTERNAL_NATIVE_DEPS, ORT_WEB_GLUE } from './scripts/build-options.mjs'
import { clearBundleStamp, sourceDigest, writeBundleStamp } from './scripts/source-digest.mjs'
import { sweepStaleChunks } from './scripts/sweep-chunks.mjs'
// Every ES module entry point shares this prefix because they share the chunks themselves -- see the build below.
const CHUNK_PREFIX = 'token-goat-chunk-'
// The hook entry used to be built separately and owned its own prefix, so dist/ carried a second, byte-for-byte copy of every shared chunk. Nothing emits these any more; sweeping them with an empty keep-list clears whatever an older build left behind in a working dist/.
const LEGACY_HOOK_CHUNK_PREFIX = 'token-goat-hook-chunk-'
// Taken before the build reads anything, so a source edited while the build runs leaves a stamp that no longer matches and the next test run builds again. tests/setup/build-bundle.ts compares it; see scripts/source-digest.mjs for why it is a digest and not an mtime.
const sourcesBuilt = sourceDigest('.')
clearBundleStamp('.')
const pkg = JSON.parse(readFileSync('./package.json', 'utf8'))
// One identity for this build, inlined into every bundle below and written beside them as dist/build-info.json (see scripts/build-stamp.mjs). Cleared first so a build that dies half way leaves no file vouching for it.
clearBuildInfo('.')
const build = computeBuildStamp('.')
// The entry points and the external list live in scripts/build-options.mjs: the notices generator and its guard have to bundle the same graph this build does, and a second copy of either would drift silently. See that file's own comment for why each package stays external.
// One build for the ES module entry points, and the reason the CLI and the hook library are one build rather than two:
//
// `token-goat.core` is the CLI. dist/token-goat.mjs (the launcher written at the bottom of this file) imports it by name, so every command pays for whatever it loads at startup.
//
// `token-goat-hook` is a separate library entry for in-process hook invocation: bridges that either already run inside a long-lived Node host (OpenClaw, opencode, pi) or spawn their own shim process (Codex, Claude Code, Copilot CLI) `import()` this sibling file directly, instead of `spawnSync`-ing a second `token-goat hook <event>` process on top of dist/token-goat.mjs. It must stay a plain library with zero load-time side effects, unlike dist/token-goat.mjs (whose banner-less src/main.ts entry calls `run()` at import time to parse `process.argv` as CLI args) -- see src/hook_lib.ts. Splitting preserves that: src/main.ts is reachable from the core entry only, so its top-level `run()` stays inside token-goat.core.mjs and never lands in a shared chunk. tests/guards/dist_chunks_deduped.test.ts imports the built hook entry to prove it.
//
// These were two separate esbuild.build() calls with disjoint chunk prefixes, which meant every module both entries reach -- almost all of them -- was emitted twice, once per prefix. dist/ was 6.75 MB across 31 files, of which 3.37 MB was a verbatim second copy, and the whole of it ships, because package.json's `files` takes the directory. esbuild's code splitting shares chunks across the entry points of a *single* build, so declaring both here is what deduplicates them: dist/ 6.75 MB -> 3.37 MB over 18 files, and the published tarball 1.60 MB -> 0.84 MB packed. Both eager sets came through the merge unchanged (core 1.890 MB, hook 1.837 MB). entryNames is '[name]', which resolves to the keys below, so both entry filenames are unchanged; outExtension keeps them `.mjs`.
//
// splitting:true is load-bearing for both entries independently. V8 compiles a module in full before running any of it, so code sitting behind a dynamic import is still parsed on every single invocation when esbuild inlines it into one file -- only its *execution* is deferred. Splitting moves it into sibling chunks that are read only if that import actually fires. For the CLI that took the eagerly loaded set from 3.61 MB to 2.83 MB (measured on the built launcher with the compile cache warm, over 15 interleaved A/B pairs of `symbol`: 149 ms -> 139 ms, the split build faster in 13 of the 15 pairs). For the hook -- which a bridge `import()`s on nearly every tool call -- it took the eagerly parsed set from 3.61 MB to 2.27 MB and import+eval from ~57 ms to ~39 ms. Sharing the chunks does not undo either: a module the core reaches statically and the hook only dynamically still lands behind the hook's dynamic edge, and tests/guards/core_bundle_stays_split.test.ts and dist_chunks_deduped.test.ts pin both eager sets.
const result = await esbuild.build({
metafile: true,
entryPoints: ENTRY_POINTS,
bundle: true,
splitting: true,
platform: 'node',
target: 'node22',
format: 'esm',
outdir: 'dist',
entryNames: '[name]',
chunkNames: `${CHUNK_PREFIX}[hash]`,
outExtension: { '.js': '.mjs' },
// Native addons cannot be bundled, and every package here is declared optionalDependencies in package.json — bundling one anyway (as sharp, puppeteer-core, pdfjs-dist and fflate previously were, via their `await import(...)` call sites) defeats "optional": esbuild statically resolves and inlines even a dynamic `import('literal')`, so the feature only worked at runtime because a matching platform package happened to be present in node_modules, not because the graceful-degradation fallback ever ran.
external: EXTERNAL_NATIVE_DEPS,
banner: {
// The require polyfill makes esbuild's CJS-interop stub (__require2) work on Node.js 24 ESM: the stub checks `typeof require !== "undefined"` and delegates to this createRequire-backed implementation when found. The shebang lives on the dist/token-goat.mjs launcher written below, which is the file package.json's `bin` points at, not on these bundles.
js: [
"import { createRequire as __cjsRequire } from 'node:module';",
'const require = __cjsRequire(import.meta.url);',
].join('\n'),
},
define: buildDefines(pkg, build.stamp),
})
// The hook client again, as one CommonJS file. Every hook shim is a CommonJS script, and the first import() a CommonJS process makes starts Node's ESM loader: 3.5ms for an empty module, and 11.7ms to load the six-module ESM client against 6.3ms for this single file. A shim pays that on every hook call a resident server answers, where it is a sixth of the whole call. The ESM client above stays for the launcher, which is ESM already, and for shims installed before this file existed. `import.meta.url` has no CommonJS equivalent, so the banner derives it from the file's own path, which is what src/hook_ipc.ts reads to find the launcher beside it.
await esbuild.build({
entryPoints: { [CJS_CLIENT]: ENTRY_POINTS[CJS_CLIENT] },
bundle: true,
platform: 'node',
target: 'node22',
format: 'cjs',
outdir: 'dist',
entryNames: '[name]',
outExtension: { '.js': '.cjs' },
banner: { js: "const __tgModuleUrl = require('node:url').pathToFileURL(__filename).href;" },
define: { ...buildDefines(pkg, build.stamp), 'import.meta.url': '__tgModuleUrl' },
// Any other import.meta the client grows would compile to an empty object and fail only at run time, inside a shim that swallows the error and quietly stops using the server.
logOverride: { 'empty-import-meta': 'error' },
})
// The WebAssembly runtime's glue module is the one piece of onnxruntime-web the bundle cannot inline: ONNX Runtime loads it by URL (env.wasm.wasmPaths) as a module of its own, so it has to be a real file beside the chunks. src/embed_runtime.ts checks it against a pinned digest before handing its URL over, so a glue from any other release is refused rather than paired with the wrong binary.
copyFileSync(`node_modules/onnxruntime-web/dist/${ORT_WEB_GLUE}`, `dist/${ORT_WEB_GLUE}`)
sweepStaleChunks('dist', CHUNK_PREFIX, Object.keys(result.metafile.outputs))
sweepStaleChunks('dist', LEGACY_HOOK_CHUNK_PREFIX, [])
// Also clears an entry file orphaned by a since-renamed ENTRY_POINTS key (e.g. dist/_m.mjs from before an entry was called 'token-goat.core'): unlike a chunk, nothing re-emits it under a new name, and package.json's `files: ["dist/"]` would otherwise ship it in every tarball forever. The launcher below is the one file this build writes outside esbuild's own outputs.
sweepStaleChunks('dist', '', [...Object.keys(result.metafile.outputs), 'dist/token-goat.mjs', `dist/${ORT_WEB_GLUE}`])
// The `bin` entry point is a launcher, not the bundle itself, purely so that module.enableCompileCache() can run BEFORE the ~3.5MB core bundle is compiled. V8 compiles a module in full before executing any of it, so the same call placed in the core bundle's own banner runs too late to cache that bundle -- measured as no change at all, versus ~22ms (about 17% of a bare invocation) when it precedes the import from here. Every CLI call and every spawned hook pays that compile, so the launcher stays tiny: anything added to it is compiled uncached on every single run.
const LAUNCHER = [
'#!/usr/bin/env node',
'// Namespace import, not a named one: enableCompileCache landed in Node 22.1 and package.json still supports 22.0, where importing it by name is a link-time failure the try/catch below never gets to run against.',
"import * as nodeModule from 'node:module'",
'// Older Node has no compile cache, and a read-only or full cache dir throws; neither is a reason to fail the command.',
'try {',
' nodeModule.enableCompileCache?.()',
'} catch {}',
'// Deliberately not awaited: a top-level await here makes this file an async ESM graph, and this file is the package main, so require("token-goat") would fail with ERR_REQUIRE_ASYNC_MODULE.',
"import('./token-goat.core.mjs').catch((err) => {",
' console.error(err)',
' process.exitCode = 1',
'})',
'',
].join('\n')
writeFileSync('dist/token-goat.mjs', LAUNCHER)
// Two builds that overlap with a source edited between their starts can interleave their outputs and leave the stamp of whichever finished last. Accepted rather than locked: it needs a hand-run build racing the test setup's, and the next edit to any source makes the stamp miss again.
writeBundleStamp('.', sourcesBuilt)
writeBuildInfo('.', pkg.version, build)
console.log(`Built dist/token-goat.mjs (v${pkg.version})`)