Commit 3667383
feat: build hooks (preBuild, postBuild, per-file transform) (#273)
* feat: build hooks (preBuild, postBuild, transform)
Add three first-class build hooks to replace the shell-script wrappers
that previously had to surround pkg invocations:
- preBuild — shell command or JS function run once before the walker
- postBuild — shell command or JS function run once per produced binary;
shell form receives the output path via PKG_OUTPUT
- transform — JS-only per-file content rewrite, applied between the
walker and bytecode/compression. Enables minify/obfuscate recipes
without bundling them into pkg's runtime deps.
Lifecycle: preBuild → walk → transform (per file) → bytecode/compression
→ write → postBuild (per binary). Hooks run identically in traditional
and enhanced SEA pipelines; simple SEA mode supports preBuild/postBuild
but skips transform (no walker output).
Configurable via the typed Node.js API (all three hooks, function form
included), package.json#pkg / .pkgrc (shell form for preBuild/postBuild)
and pkg.config.{js,cjs,mjs} (function form for any hook).
Closes #252.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* refactor(types): extract PkgBaseOptions shared by config + API shapes
PkgOptions and PkgExecOptions had drifted into a 12-field overlap that
duplicated identical names, types, and JSDoc — most visibly across the
new build hooks. Pull those shared fields into a `PkgBaseOptions` base
interface and have both shapes extend it.
Listy fields where typing intentionally differs (`targets`,
`publicPackages`, `noDictionary` are lenient `string | string[]` in
config files and strict `string[]` at the API boundary) and the
`options`/`bakeOptions` rename stay on the leaf interfaces — pulling
those up would need generic gymnastics for no net win.
Public surface: PkgBaseOptions is exported from the package entry so
downstream tooling can type-derive shared build-shaping options without
enumerating fields by hand.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: address review feedback on build hooks
Copilot review:
- docs-site/guide/api.md: tighten the type strings for preBuild/postBuild
(parens around the function arms so `string | (() => ...)` parses as
intended), and reflect that `transform` returns may be Promise-wrapped.
- test/test-46-hooks/main.js: add `assert(!module.parent)` to match the
e2e convention used by sibling tests.
- test/unit/hooks.test.ts: replace `'true'` / `'exit 7'` with portable
`node -e` invocations — `true` is a POSIX shell builtin and Windows
cmd.exe doesn't recognize it, so the unit suite would fail on win32 CI.
Self-review:
- lib/config.ts: switch from `Object.assign(rawPkg, parsed.apiPkg)` to a
spread, so the source `configJson.pkg` / `inputJson.pkg` objects are not
mutated by API-injected hooks bleeding back into the parsed config.
- lib/hooks.ts: drop the redundant `as PreBuildHook` / `PostBuildHook` /
`TransformHook` casts — TypeScript already narrows the union after the
`typeof === 'string'` checks. Trim `void | undefined` on the transform
result to plain `void` (`void` covers `undefined` for return types).
- lib/hooks.ts + lib/sea.ts: extract a `runPostBuildForTargets` helper to
dedupe the per-target loop the two SEA paths shared, and to give the
loop direct unit-test coverage (closes the lib/sea.ts coverage gap
flagged by codecov on the previous push).
- lib/sea.ts: cache `pkgOptions.get()` once in `seaEnhanced` instead of
fetching it twice.
- docs-site/guide/api.md: warn that `transform` receives every embedded
file (including binaries / `.node` addons) — users must filter by
extension before rewriting, since returning a string for binary content
would corrupt it. Also document the SEA-vs-traditional postBuild
timing difference (parallel-bake-then-sequential-postBuild in SEA
vs. interleaved per-target in the traditional pipeline).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* refactor(config): extract shared hook validator; test transform disk path
- Deduplicate the preBuild/postBuild shell-or-fn validation shared by
parseOptionsInput and validatePkgConfig into validateShellOrFnHook, so
the non-empty-string rule lives in one place and can't drift.
- Add unit coverage for runTransform's disk-read fallback (record.body
undefined -> readFile, then cache) and its read-failure error path,
which underpin the eager-load correctness rationale.
* test(hooks): add enhanced-SEA e2e coverage for the transform hook
Closes #287. The transform build hook ran through the traditional pipeline
e2e (test-46-hooks) but had no end-to-end coverage in the SEA pipeline,
even though seaEnhanced() calls runTransform() before generating the SEA
archive. Add test-88-sea-hooks: it drives the programmatic API (transform
is function-only, not reachable from the CLI) with `sea: true` and a
package.json input, builds the host SEA binary, and asserts the mutated
marker is printed by the produced executable — proving the transform flows
into the SEA archive bytes.
Also fix an api.md inconsistency: `transform` is reachable from
pkg.config.{js,cjs,mjs}, not "API only" (it's just function-only, so
unavailable from JSON config) — matching configuration.md and the
Build hooks note already in api.md.
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 3520d26 commit 3667383
16 files changed
Lines changed: 1126 additions & 81 deletions
File tree
- docs-site/guide
- docs
- lib
- test
- test-46-hooks
- test-88-sea-hooks
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
| |||
126 | 129 | | |
127 | 130 | | |
128 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
129 | 223 | | |
130 | 224 | | |
131 | 225 | | |
| |||
0 commit comments