Skip to content

Commit 3c6a771

Browse files
committed
feat: add outdir option
$build.outdir and --outdir (-o) name the output folder, so a site that merges bongo's output with other tools' files can keep them apart. The config now loads before the output folder is touched. A config that fails to load empties only the --outdir folder or the folder of the last good build, never a guessed one. dev --write follows the config.
1 parent bbec3e1 commit 3c6a771

20 files changed

Lines changed: 357 additions & 70 deletions

‎docs/content/cli.md‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ group: Start
55
order: 4
66
---
77

8-
The `bongo` command has two subcommands. `bongo build` writes the site to `public/`. `bongo dev` serves the site and rebuilds it when a file changes.
8+
The `bongo` command has two subcommands. `bongo build` writes the site to the [output folder](#output-folder), `public/` by default. `bongo dev` serves the site and rebuilds it when a file changes.
99

1010
## Commands
1111

@@ -16,30 +16,32 @@ bongo dev
1616

1717
| Command | Description |
1818
| --- | --- |
19-
| `bongo build` | Build the site into `public/`. |
19+
| `bongo build` | Build the site into the output folder. |
2020
| `bongo dev` | Start the dev server, build the site behind it, and rebuild on each change. See [Dev server](/dev-server/). |
2121

22-
When it is done, `bongo build` prints the size of each file in `public/`, and its gzip size. Above 50 files, it prints the total size of each kind of file instead, such as `.html` or `.png`, and lists only the scripts and stylesheets, with their gzip sizes.
22+
When it is done, `bongo build` prints the size of each file in the output folder, and its gzip size. Above 50 files, it prints the total size of each kind of file instead, such as `.html` or `.png`, and lists only the scripts and stylesheets, with their gzip sizes.
2323

2424
## Options
2525

2626
```sh
2727
bongo build -C site --minify
2828
bongo dev -C site --host --port 3000
2929
bongo dev --write
30+
bongo build --outdir web/public
3031
bongo build --debug
3132
bongo build --fresh
3233
bongo build --drafts
3334
```
3435

3536
| Option | Default | Description |
3637
| --- | --- | --- |
37-
| `-C`, `--cwd` | `.` | The project folder. bongo finds the config, the source folders, and `public/` in it. |
38+
| `-C`, `--cwd` | `.` | The project folder. bongo finds the config and the source folders in it. |
3839
| `-H`, `--host` | `localhost` | The host that `bongo dev` binds. A bare `--host`, with no value, exposes the server on your network. |
3940
| `-p`, `--port` | `3000` | The port that `bongo dev` binds. |
41+
| `-o`, `--outdir` | `public` | The folder to write the site to, from the project folder. It wins over [`$build.outdir`](/config/#output-folder). See [Output folder](#output-folder). |
4042
| `-m`, `--minify` | off | Minify the output. See [Minify](#minify). |
4143
| `-d`, `--debug` | off | Log each build stage and its time. See [Debug](#debug). |
42-
| `-w`, `--write` | off | `bongo dev` only. Also write the output to `public/`. See [Output folder](#output-folder). |
44+
| `-w`, `--write` | off | `bongo dev` only. Also write the output to the output folder. See [Output folder](#output-folder). |
4345
| `-f`, `--fresh` | off | Render every page, not only the pages that a change reaches. See [Incremental builds](#incremental-builds). |
4446
| `--drafts` | off; on for `bongo dev` | Build drafts: pages with `draft: true`, or a `date` after the build starts. `--no-drafts` leaves them out. See [Drafts](/content/#drafts). |
4547
| `-v`, `--version` | | Show the version. |
@@ -79,13 +81,15 @@ Without `--debug`, a stage that is still running after 15 seconds prints `still
7981

8082
## Output folder
8183

82-
`bongo build` writes over the files that an earlier run left in `public/`, which is much faster than making them again. When it is done, it deletes each file of the last build that it did not write, so no file from an earlier run stays. The record of the last build lists those files, so `bongo build` does not list `public/`. Thus a file that something else put in `public/` stays, until a build without the record, such as `bongo build --fresh`, lists the folder and deletes it. When the build fails, it empties `public/` and deletes the record, so no part of the failed build stays.
84+
The output folder is `public/` in the project folder. To use another folder, set [`$build.outdir`](/config/#output-folder) in the config, or pass `--outdir`. The flag wins over the config. bongo owns this folder, so do not let another tool write its files there. When the site also needs files from other tools, such as maps or API docs, build to a folder of its own, such as `web/public`, and copy the folders together in your deploy step.
8385

84-
`bongo dev` does not use `public/`. It keeps its output in memory and serves it from there. Thus a `bongo build` that runs at the same time never changes what dev serves, and dev never changes what the build writes.
86+
`bongo build` writes over the files that an earlier run left in the output folder, which is much faster than making them again. When it is done, it deletes each file of the last build that it did not write, so no file from an earlier run stays. The record of the last build lists those files, so `bongo build` does not list the folder. Thus a file that something else put in the folder stays, until a build without the record, such as `bongo build --fresh`, lists the folder and deletes it. When the build fails, it empties the output folder and deletes the record, so no part of the failed build stays. A config that fails to load cannot name `$build.outdir`. Then bongo empties the `--outdir` folder, or else the folder of the last good build, and no other folder.
8587

86-
With `--write`, `bongo dev` also writes its output to `public/`. It empties the folder before the first build. After that, each rebuild deletes only the files that the new build no longer writes.
88+
`bongo dev` does not use the output folder. It keeps its output in memory and serves it from there. Thus a `bongo build` that runs at the same time never changes what dev serves, and dev never changes what the build writes.
8789

88-
bongo never follows a symlink in `public/`. It deletes a symlink that a write or a deletion goes through, and a build that lists the folder deletes each symlink in it. It never deletes the file or folder that a symlink points to. bongo refuses to empty or write over `public/`, and the build fails, when the folder resolves through symlinks to:
90+
With `--write`, `bongo dev` also writes its output to the output folder. It empties the folder before the first build. After that, each rebuild deletes only the files that the new build no longer writes. When the config names another `$build.outdir`, dev empties the new folder and writes all of its output there.
91+
92+
bongo never follows a symlink in the output folder. It deletes a symlink that a write or a deletion goes through, and a build that lists the folder deletes each symlink in it. It never deletes the file or folder that a symlink points to. bongo refuses to empty or write over the output folder, and the build fails, when the folder resolves through symlinks to:
8993

9094
- the project root, or a folder above it,
9195
- a folder outside the project,
@@ -106,7 +110,7 @@ Each build compares the size and the mtime of each source file with the record.
106110
- a script, a stylesheet, an image, or another asset that it links changes, or a module or stylesheet that one of them imports, since the name of the output file changes. `bongo dev` names each script and stylesheet by its entry, not by its content. There, an edit to one renders only the pages that inline it or use its CSS module classes. The other pages reload and get the new file,
107111
- the config has `$hooks.transform`, and an output file is added, deleted, or changes its frontmatter, since the hook gets the list of output files,
108112
- it was a draft by its `date`, and that moment has passed,
109-
- its file in `public/` changed or is gone since the last build.
113+
- its file in the output folder changed or is gone since the last build.
110114

111115
Every page renders again when the config changes, or a local file that it imports, a file in `data/`, `layouts/`, or `partials/`, the lockfile, the version of bongo, the runtime, `NODE_ENV`, `--minify`, or `--drafts`. [Output files](/output-files/) and [data modules](/data-modules/) render on every build, since they can read every page.
112116

@@ -116,7 +120,7 @@ bongo cannot see a change that keeps the size and the mtime of a source file, as
116120
bongo build --fresh
117121
```
118122

119-
A build that fails empties `public/`, so the next build renders every page. To start over, delete `node_modules/.cache/bongo/memo.json`, or use `--fresh`.
123+
A build that fails empties the output folder, so the next build renders every page. To start over, delete `node_modules/.cache/bongo/memo.json`, or use `--fresh`.
120124

121125
## Minify
122126

@@ -137,12 +141,13 @@ await build({ cwd: 'site', minify: true });
137141
```
138142

139143
- `cwd` — the project folder. Required.
144+
- `outdir` — the same as `--outdir`.
140145
- `minify` — the same as `--minify`.
141146
- `debug` — the same as `--debug`.
142147
- `fresh` — the same as `--fresh`.
143148
- `drafts` — the same as `--drafts`.
144149

145-
`dev(options)` starts the dev server, as `bongo dev` does. It resolves after the first build, and the server keeps running. It takes `cwd`, `debug`, `drafts`, `fresh`, `port`, `host`, and `write`, the same as the `bongo dev` options:
150+
`dev(options)` starts the dev server, as `bongo dev` does. It resolves after the first build, and the server keeps running. It takes `cwd`, `debug`, `drafts`, `fresh`, `outdir`, `port`, `host`, and `write`, the same as the `bongo dev` options:
146151

147152
```ts
148153
import { dev } from 'bongo';

‎docs/content/config.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default {
9292
| `$utils` | Helper functions that templates can call. See [`$utils`](#utils). |
9393
| `$partials` | Programmatic partial functions. See [Config partials](/partials/#config-partials). |
9494
| `$images` | Image defaults. See [Image config](/images/#image-config). |
95-
| `$build` | Build options. See [Build targets](#build-targets) and [Script values](#script-values). |
95+
| `$build` | Build options. See [Build targets](#build-targets), [Script values](#script-values), and [Output folder](#output-folder). |
9696
| `$hooks` | Functions that change the build output. See [Hooks](/hooks/). |
9797
| `plugins` | An array of partial config objects, merged in order. See [Plugins](#plugins). |
9898

@@ -254,6 +254,21 @@ export default {
254254

255255
Your values replace the built-in ones of the same name. The `define` maps of plugins and the config merge, and the config's values win.
256256

257+
## Output folder
258+
259+
`$build.outdir` names the folder that `bongo build` writes the site to. The path is from the project folder, and the default is `public`:
260+
261+
```ts [bongo.config.ts]
262+
export default {
263+
$site: { url: 'https://example.com/', title: 'My site', lang: 'en' },
264+
$build: {
265+
outdir: 'web/public',
266+
},
267+
};
268+
```
269+
270+
The `--outdir` flag wins over it. bongo deletes each file in this folder that the build does not write, so keep the files of other tools in another folder. See [Output folder](/cli/#output-folder) for the rules.
271+
257272
## Plugins
258273

259274
A plugin is a partial config object. Plugins usually come from a function, such as `markdown()` from [@bongo/markdown](/markdown/) or `feed()` from [@bongo/feed](/feed/):

‎docs/content/dev-server.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ If the config or a data file fails to load, the error overlay shows the error, a
3636

3737
## Output in memory
3838

39-
The dev server keeps each build in memory and serves it from there, as Vite does. It does not read or write `public/`. Thus you can run `bongo build` while `bongo dev` runs: neither one changes the output of the other.
39+
The dev server keeps each build in memory and serves it from there, as Vite does. It does not read or write the output folder. Thus you can run `bongo build` while `bongo dev` runs: neither one changes the output of the other.
4040

41-
To see the dev output as files, run `bongo dev --write`. Then dev also writes its output to `public/`. See [Output folder](/cli/#output-folder).
41+
To see the dev output as files, run `bongo dev --write`. Then dev also writes its output to the output folder, `public/` by default. See [Output folder](/cli/#output-folder).
4242

4343
The dev server serves a file that bongo only copies from `content/`, such as a PDF, from its source. Thus an edit to it shows at once.
4444

‎docs/content/dynamic-images.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bongo keeps each drawing in `node_modules/.cache/bongo/render`. The key is a has
9393
The build fails with a message that names the cause when:
9494

9595
- two pages draw different images to the same `output`;
96-
- `output` is a file of the site, or a path outside `public/`;
96+
- `output` is a file of the site, or a path outside the output folder;
9797
- the template has no root element, or more than one;
9898
- the template has text, but declares no fonts;
9999
- a font is WOFF2, or a file is not in `assets/`;

‎docs/content/project.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ my-site/
2727
| `data/` | Holds JSON, YAML, and module files. Templates get them as `$data`. |
2828
| `layouts/` | Holds the `.hbs` layouts that wrap pages. |
2929
| `partials/` | Holds the `.hbs`, JavaScript, and TypeScript partials. |
30-
| `public/` | Receives the output of `bongo build`. Each build writes over it, then deletes the files of the last build that it did not write. `bongo dev` keeps its output in memory. |
30+
| `public/` | Receives the output of `bongo build`, unless [`$build.outdir`](/config/#output-folder) names another folder. Each build writes over it, then deletes the files of the last build that it did not write. `bongo dev` keeps its output in memory. |
3131
| `bongo.config.ts` | The config. |
3232

3333
### `assets/`
@@ -50,7 +50,7 @@ A partial is a template that layouts, `.hbs` content, and Markdown can call by n
5050

5151
### `public/`
5252

53-
`bongo build` writes over `public/`, then deletes each file of the last build that it did not write. `bongo dev` keeps its output in memory and does not touch `public/`, unless you pass `--write`. bongo refuses to use the folder when it is not a safe folder. See [Output folder](/cli/#output-folder).
53+
The output folder. [`$build.outdir`](/config/#output-folder) or `--outdir` can name another folder. `bongo build` writes over it, then deletes each file of the last build that it did not write. `bongo dev` keeps its output in memory and does not touch the folder, unless you pass `--write`. bongo refuses to use the folder when it is not a safe folder. See [Output folder](/cli/#output-folder).
5454

5555
### `bongo.config.ts`
5656

@@ -60,10 +60,10 @@ The config can also be `bongo.config.mjs`, `bongo.config.cjs`, or `bongo.config.
6060

6161
When you run `bongo build`, bongo does these steps:
6262

63-
1. `bongo build` keeps the files of the last build in `public/` to write over them. The [record of the last build](/cli/#incremental-builds) lists them; without it, bongo lists the folder and deletes each symlink in it. Then bongo loads the config, `bongo.config.{ts,mjs,cjs,js}`. Rolldown bundles the config and the local files that it imports into one temporary `.mjs` module. bongo imports that module, then deletes it. This works the same in Bun, Deno, and Node. Packages stay external and resolve as usual. A package linked into `node_modules` from outside it, such as a workspace package, is local code: bongo bundles it too, and the packages it imports resolve from its own folder. `import.meta.url`, `import.meta.dirname`, and `__dirname` keep pointing at each source file. TypeScript partials, `data/` modules, and [data modules](/data-modules/) load the same way.
64-
2. It loads the files in `data/` as `$data`. It collects layouts from `layouts/` and partials from `partials/`. It compiles the Handlebars files (`.hbs`) with tempura, and loads the JavaScript and TypeScript partials as modules.
63+
1. bongo loads the config, `bongo.config.{ts,mjs,cjs,js}`. Rolldown bundles the config and the local files that it imports into one temporary `.mjs` module. bongo imports that module, then deletes it. This works the same in Bun, Deno, and Node. Packages stay external and resolve as usual. A package linked into `node_modules` from outside it, such as a workspace package, is local code: bongo bundles it too, and the packages it imports resolve from its own folder. `import.meta.url`, `import.meta.dirname`, and `__dirname` keep pointing at each source file. TypeScript partials, `data/` modules, and [data modules](/data-modules/) load the same way. It also loads the files in `data/` as `$data`.
64+
2. `bongo build` keeps the files of the last build in the output folder, which the config can name, to write over them. The [record of the last build](/cli/#incremental-builds) lists them; without it, bongo lists the folder and deletes each symlink in it. It collects layouts from `layouts/` and partials from `partials/`. It compiles the Handlebars files (`.hbs`) with tempura, and loads the JavaScript and TypeScript partials as modules.
6565
3. It passes the content files through the registered [loaders](/loaders/), then renders each page through its matching [layout chain](/templates/#layouts). A page that no change reached since the last build keeps its file and skips its loaders. See [Incremental builds](/cli/#incremental-builds). [Output files](/output-files/), such as `sitemap.xml.hbs`, get no layout. [Data modules](/data-modules/), such as `search.json.ts`, run when every page is known, and write JSON.
6666
4. It scans the rendered HTML for `<bongo:*>` elements, and the content of each page for `<img>` elements with a relative `src`. It registers each asset and replaces it with a placeholder token.
6767
5. It flushes the asset groups. JavaScript goes through Rolldown, CSS goes through LightningCSS, and images go through `Bun.Image`. Output file names include content hashes.
6868
6. It replaces the placeholders with external asset URLs or with inline asset contents.
69-
7. It passes each page and output file to [`$hooks.transform`](/hooks/), and writes the pages to `public/`. `bongo dev` keeps them in memory. So that a large site does not fill the memory, `bongo build` writes each page to `public/` as soon as it renders, then reads it again for steps 6 and 7. Last, it deletes each file of the last build that this build did not write. With `--minify`, it then minifies the pages when the `minify` CLI is available. See [Minify](/cli/#minify).
69+
7. It passes each page and output file to [`$hooks.transform`](/hooks/), and writes the pages to the output folder. `bongo dev` keeps them in memory. So that a large site does not fill the memory, `bongo build` writes each page to `public/` as soon as it renders, then reads it again for steps 6 and 7. Last, it deletes each file of the last build that this build did not write. With `--minify`, it then minifies the pages when the `minify` CLI is available. See [Minify](/cli/#minify).

‎packages/bongo/bin.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ try {
1414
cwd: { type: 'string', short: 'C', default: '.' },
1515
host: { type: 'string', short: 'H' },
1616
port: { type: 'string', short: 'p' },
17+
outdir: { type: 'string', short: 'o' },
1718
minify: { type: 'boolean', short: 'm', default: false },
1819
debug: { type: 'boolean', short: 'd', default: false },
1920
write: { type: 'boolean', short: 'w', default: false },
@@ -51,9 +52,10 @@ if (values.help) {
5152
output += '\n -C, --cwd Directory to resolve from (default .)';
5253
output += '\n -H, --host Host to bind; bare flag exposes network (default localhost)';
5354
output += '\n -p, --port Port to bind (default 3000)';
55+
output += '\n -o, --outdir Folder to write the site to (default $build.outdir or public)';
5456
output += '\n -m, --minify Compress built assets (default false)';
5557
output += '\n -d, --debug Log each build stage and its time (default false)';
56-
output += '\n -w, --write Dev only: also write the output to public/ (default false)';
58+
output += '\n -w, --write Dev only: also write the output to the outdir (default false)';
5759
output += '\n -f, --fresh Render every page, not only what changed (default false)';
5860
output += '\n --drafts Build drafts; --no-drafts leaves them out (default false; dev true)';
5961
output += '\n -v, --version Displays current version';
@@ -72,8 +74,8 @@ if (values.help) {
7274
let command = require('.')[name];
7375
if (!command) throw new Error(`Unknown "${name}" command`);
7476

75-
let { cwd, host, minify, debug, write, fresh, drafts } = values;
76-
await command({ cwd, host, port, minify, debug, write, fresh, drafts });
77+
let { cwd, host, outdir, minify, debug, write, fresh, drafts } = values;
78+
await command({ cwd, host, port, outdir, minify, debug, write, fresh, drafts });
7779
} catch (err) {
7880
console.error('ERROR', err.stack);
7981
process.exit(1);

0 commit comments

Comments
 (0)