Skip to content

Commit a8e91df

Browse files
robertsLandoclaude
andauthored
feat(config): accept CLI-only build flags in pkg config (#263)
* refactor(config): unify flag registry, phase-split resolveConfig Consolidate CLI (string[]) and programmatic (PkgExecOptions) entry points through a single canonical ParsedInput shape, dropping the options->argv->parseArgs round trip. Split resolveConfig into named phase helpers (resolveInput, resolveConfigFile, resolveOutput, resolveTargetList, assignTargetOutputs). Fully resolve flags in the config layer: compress becomes CompressType enum, targets becomes NodeTarget[] with per-target output paths and input-overwrite guards applied. Expose effective merged pkg on ResolvedConfig so index.ts no longer duplicates precedence logic. Drop unused minimist dep; util.parseArgs drives CLI parsing. * test(config): add pure-unit regression guard for parseInput/resolveFlags Integration-only coverage (test-50-config-flags) exercises a handful of end-to-end paths via real binaries and takes seconds. This test requires lib-es5/config directly and runs 78 assertions in ~200ms covering: - CLI parseInput: positionals, short-circuits, all flag kinds (bool/string/ list), short aliases, alias collapsing (target/targets, out-path/outdir/ out-dir), every FLAG_SPECS bool negation, positional/negation last-wins, unknown-option rejection - Programmatic parseInput: type guards, input validation, bakeOptions/ publicPackages/noDictionary/targets array-join, empty-array behavior - resolveFlags: CLI>config>default merge for every flag, three-state override (CLI false beats config true), list empty-clear, whitespace trimming, csv parsing, '*' preservation, all compress aliases - validatePkgConfig: unknown-key warn, known-key silence, type mismatches for bool/string/list, mixed-array rejection * fix(config): address PR review feedback - Validate `pkg` config field is a plain object (reject string/array/null) with a clear error message instead of silently falling back to defaults - types.ts: `PkgCompressType` now excludes numeric reverse-mapping keys from the enum (`Exclude<keyof typeof CompressType, number>`) - help.ts: add `--no-signature` to the options section; list `native-build` and `sea` in the config-overridable flags note - docs: correct `targets` type (`string | string[]`) and rename `deployAssets` row to `deployFiles` (matches actual code/types) - test-80-compression fixture: swap `minimist` require for `picomatch` (direct dep — minimist was removed from package.json) - test-50-config-flags: new `pkg.not-object.json` fixture + assertion for the non-object pkg rejection * refactor(config): enhance type safety for CLI argument parsing * refactor(config): improve type safety and clarity in CLI argument handling * fix(config): address Copilot review feedback - fix "give" → "given" typo in --sea help text - preserve last-wins order for --target/--targets aliases via token walk - narrow PkgOptions.deployFiles to `[from, to]` / `[from, to, 'directory']` tuples - force-require picomatch/picocolors in compression fixture for real VFS payload Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * refactor(config): drop resolveTargetAlias helper Both --target and --targets are niche aliases; last-wins token walk is overkill. Revert to simple nullish collapse. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(config): address deep-review feedback - Merge resolved flag values back into pkg via applyResolvedFlags so pkgOptions consumers observe CLI overrides (not just the raw config). - Reject `--target` + `--targets` together instead of silently preferring one alias regardless of order. - Move validatePkgConfig out of resolveFlags into resolveConfig so resolveFlags is a pure merge. - Simplify resolveOutput: read outputPath from the resolved pkg instead of re-walking configJson.pkg / inputJson.pkg. - Throw on bad-type exec() options (boolean/string/list) instead of silently dropping them at the programmatic boundary. - Drop redundant String() in resolveList's trim. - Clean up test-80 compression fixture: remove dead loremIpsum and the /* eslint-disable no-unused-vars */ it required. - Add test-50-config-flags coverage for the --target/--targets conflict. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(config): address Copilot review feedback - Set log.debugMode unconditionally from flags.debug so a prior exec() call with debug:true does not leak debug logging into a later debug:false invocation (the programmatic API may run exec multiple times per process). - Document the positive --signature flag in help; it's already accepted by the CLI parser and is needed to override a config value of signature:false. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * docs(config): add JSDoc coverage across lib/config.ts Convert existing inline prose comments into JSDoc and add short doc blocks to exported types, internal helpers, and module-level constants. Content is unchanged; this just makes each symbol self-describe for hover/autocomplete and for future readers. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(config): address Copilot review feedback - Trim each entry in resolveTargetList's comma-split paths (both CLI and pkg.targets string forms). Inputs like "node22-linux-x64, node22-win-x64" previously produced a leading-space token and failed in parseTargets with "Unknown token ' node22'". Matches the trim pattern already used by resolveList for other list-shaped flags. - Correct the deployFiles schema row in docs-site/guide/configuration.md: it is a tuple array ([from, to] or [from, to, "directory"]), not a plain string[]. Matches lib/types.ts and lib/walker.ts. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent e59f2e8 commit a8e91df

15 files changed

Lines changed: 1875 additions & 598 deletions

File tree

docs-site/guide/configuration.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,29 @@ When both a pkgrc and a `pkg` field in `package.json` are present, the pkgrc win
8989

9090
## Full schema
9191

92-
| Key | Type | Description |
93-
| -------------- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------- |
94-
| `scripts` | glob \| string[] | JS files compiled to V8 bytecode and embedded without source — see [Scripts](#scripts) |
95-
| `assets` | glob \| string[] | Files embedded as raw content, accessible under `/snapshot/` — see [Assets](#assets) |
96-
| `ignore` | string[] | Globs excluded from the final executable — see [Ignore files](#ignore-files) |
97-
| `targets` | string[] | Target triples, e.g. `node22-linux-x64` — see [Targets](/guide/targets) |
98-
| `outputPath` | string | Directory for output binaries (equivalent to CLI `--out-path`) |
99-
| `patches` | object | Patch modules that can't be packaged as-is — see [pkg source](https://github.com/yao-pkg/pkg/blob/main/dictionary/) for examples |
100-
| `sea` | boolean | Opt into [SEA mode](/guide/sea-mode) without passing `--sea` |
101-
| `seaConfig` | object | Forwarded to Node.js SEA config (`useCodeCache`, `disableExperimentalSEAWarning`, etc.) |
102-
| `deployAssets` | boolean | Copy `assets` next to the executable at runtime instead of keeping them in the snapshot |
92+
| Key | Type | Description |
93+
| ------------------ | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
94+
| `scripts` | glob \| string[] | JS files compiled to V8 bytecode and embedded without source — see [Scripts](#scripts) |
95+
| `assets` | glob \| string[] | Files embedded as raw content, accessible under `/snapshot/` — see [Assets](#assets) |
96+
| `ignore` | string[] | Globs excluded from the final executable — see [Ignore files](#ignore-files) |
97+
| `targets` | string \| string[] | Target triples, e.g. `node22-linux-x64`; accepts a single target, an array, or a comma-separated string — see [Targets](/guide/targets) |
98+
| `outputPath` | string | Directory for output binaries (equivalent to CLI `--out-path`) |
99+
| `patches` | object | Patch modules that can't be packaged as-is — see [pkg source](https://github.com/yao-pkg/pkg/blob/main/dictionary/) for examples |
100+
| `sea` | boolean | Opt into [SEA mode](/guide/sea-mode) without passing `--sea` |
101+
| `seaConfig` | object | Forwarded to Node.js SEA config (`useCodeCache`, `disableExperimentalSEAWarning`, etc.) |
102+
| `deployFiles` | tuple[] | Files that cannot be bundled; each entry is `[from, to]` or `[from, to, "directory"]`. pkg logs a reminder to ship each one next to the output at runtime |
103+
| `compress` | string | VFS compression algorithm — `None` (default), `Brotli`, `GZip`, or `Zstd`. Equivalent to CLI `--compress` |
104+
| `fallbackToSource` | boolean | Ship source when bytecode generation fails for a file. Equivalent to CLI `--fallback-to-source` |
105+
| `public` | boolean | Speed up packaging and disclose top-level project sources. Equivalent to CLI `--public` |
106+
| `publicPackages` | string \| string[] | Package names treated as public. Use `"*"` for all. Equivalent to CLI `--public-packages` |
107+
| `options` | string \| string[] | V8 / Node options baked into the executable, e.g. `["expose-gc"]`. Equivalent to CLI `--options` |
108+
| `bytecode` | boolean | Compile bytecode (default `true`). Set to `false` for source-only builds. Equivalent to CLI `--no-bytecode` |
109+
| `nativeBuild` | boolean | Build native addons (default `true`). Equivalent to CLI `--no-native-build` (set `false`) |
110+
| `noDictionary` | string \| string[] | Package names whose dictionary handling is skipped. Use `"*"` for all. Equivalent to CLI `--no-dict` |
111+
| `debug` | boolean | Verbose packaging logs. Equivalent to CLI `--debug` |
112+
| `signature` | boolean | Sign macOS binaries when applicable (default `true`). Equivalent to CLI `--signature` / `--no-signature` |
113+
114+
CLI flags always override config values. Unknown keys under `pkg` produce a warning.
103115

104116
## Scripts
105117

0 commit comments

Comments
 (0)