Make the dist directory configurable

Set it with `dist_dir` in bunext.config.ts or `--dist-dir` on any CLI
command (precedence: --dist-dir > BUNEXT_DIST_DIR > dist_dir > .bunext).
setupDistDir() resolves it in the CLI's preAction hook and at the start
of bunextInit(), so custom servers get the config value too.

- Read dist-derived paths from grabDirNames() inside functions instead of
  at import time; a guard test enforces this.
- Derive the client asset URL prefix from the dist dir instead of
  hard-coding /.bunext.
- Make the dev watcher ignore rules and probe follow the dist dir.
- Validate the dir: inside the project root, outside src/, public/ and
  node_modules/, URL-safe characters only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-09 03:55:54 +01:00
co-authored by Claude Opus 5.5
parent 4d4a79a6dd
commit b410b92f9f
35 changed files with 601 additions and 80 deletions
+3 -1
View File
@@ -65,4 +65,6 @@ API routes live in `src/pages/api/` and follow standard Bun `Request → Respons
`bunext build` also updates `.bunext/page-lastmod.json` (`src/utils/page-lastmod-record.ts`): per page, a fingerprint of the bundle hash + CSS path and the build date it last changed. The sitemap uses these dates as `lastmod`.
### Config
Consumer projects define `bunext.config.ts` at their project root. The `BunextConfig` type fields: `distDir`, `assetsPrefix`, `origin`, `globalVars`, `port`, `development`.
Consumer projects define `bunext.config.ts` at their project root. The `BunextConfig` type fields include `dist_dir`, `assets_prefix`, `origin`, `global_vars`, `port`, `development`.
The dist directory (default `.bunext`) is resolved by `setupDistDir()` (`src/functions/setup-dist-dir.ts`): `--dist-dir` > `BUNEXT_DIST_DIR` env > config `dist_dir`. It runs in the CLI's `preAction` hook and at the start of `bunextInit()` (so custom servers get it too), and stores the result in `BUNEXT_DIST_DIR`, which `grabDirNames()` reads and child processes inherit. Because it runs after modules are imported, **never read a dist-derived path from `grabDirNames()` at module level** — call it inside the function that uses it. Only root-derived paths (`ROOT_DIR`, `PAGES_DIR`, `PUBLIC_DIR`, ...) may be read at module level; `src/__tests__/utils/grab-dist-dir.test.ts` enforces this. Client bundle URLs are their project-relative paths (`grabBunextPublicUrlPath()`), so never hard-code `/.bunext` in URLs or paths.