You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Users often need to run extra steps around a pkg build — bundling with esbuild/webpack beforehand, minifying/obfuscating the files pkg is about to embed, or smoke-testing the produced binary afterwards. Today this lives in external shell scripts, and there's no way to transform walker output without mutating the user's source tree.
Blocked on #242 — the per-file transform hook is inherently a JS function, so it needs the typed Node.js API options object to land first. Shipping the whole hooks surface together (rather than shell-only first, functions later) avoids an awkward intermediate config shape.
Proposal
Split hooks into two kinds with clearly different roles.
1. Shell hooks — preBuild, postBuild
For setup/teardown and smoke tests. Config-file friendly (strings).
Spawned via shell, stdio inherited, non-zero exit fails the build
postBuild runs per produced binary; path exposed as PKG_OUTPUT env var
JS API also accepts function form (() => Promise<void>)
2. Transformer hook — transform(filePath, contents) (JS API only)
For per-file content transformation after the walker collects files, before they're written into the VFS / SEA archive. This is the hook for minification / obfuscation.
Why a dedicated transformer rather than a shell hook:
Operates on the exact set of files pkg is embedding (walker output), not the whole source tree
Never touches the user's source files on disk
Per-file — users apply their own matching rules
Runs before bytecode compilation and compression, so it composes cleanly with --public-packages and Brotli/GZip
Minification / Obfuscation
Deliberately not a built-in pkg feature — keeps runtime deps minimal. Instead, document recipes using the transform hook with the user's tool of choice (terser, javascript-obfuscator, swc, esbuild, etc.). Users pick the tool that fits their project; pkg stays out of the transform business.
Users often need to run extra steps around a pkg build — bundling with esbuild/webpack beforehand, minifying/obfuscating the files pkg is about to embed, or smoke-testing the produced binary afterwards. Today this lives in external shell scripts, and there's no way to transform walker output without mutating the user's source tree.
Proposal
Split hooks into two kinds with clearly different roles.
1. Shell hooks —
preBuild,postBuildFor setup/teardown and smoke tests. Config-file friendly (strings).
{ "pkg": { "hooks": { "preBuild": "esbuild src/index.js --bundle --outfile=dist/bundle.js", "postBuild": "./dist/my-app-linux --version" } } }postBuildruns per produced binary; path exposed asPKG_OUTPUTenv var() => Promise<void>)2. Transformer hook —
transform(filePath, contents)(JS API only)For per-file content transformation after the walker collects files, before they're written into the VFS / SEA archive. This is the hook for minification / obfuscation.
Why a dedicated transformer rather than a shell hook:
--public-packagesand Brotli/GZipMinification / Obfuscation
Deliberately not a built-in pkg feature — keeps runtime deps minimal. Instead, document recipes using the
transformhook with the user's tool of choice (terser, javascript-obfuscator, swc, esbuild, etc.). Users pick the tool that fits their project; pkg stays out of the transform business.Scope
preBuild→ walk →transform(per file) → bytecode/compression → write →postBuild(per binary)package.json#pkg/.pkgrc(feat: support a .pkgrc configuration file #238) for shell hooks, and via the typed Node.js API (feat: typed options object for the Node.js API #242) for thetransformfunctiontransform, and pre-bundling viapreBuildPart of #235