Skip to content

Commit 5f5739c

Browse files
committed
refactor: rename to @angular-schule/prerender-format with option prerenderFormat
The option follows Astro's build.format: prerenderFormat 'directory' (default, foo/index.html) or 'file' (foo.html). 'flat' is already used by Angular for i18n output paths. Also adds a test for the JSON files that the builder and schematic registration point to.
1 parent 3f28e68 commit 5f5739c

17 files changed

Lines changed: 150 additions & 115 deletions

‎.github/scripts/test-angular-app.sh‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#
44
# Usage: test-angular-app.sh <ng-new-command> <package-tgz> <angular-version-range>
55
# ng-new-command: command that runs `ng new`, e.g. "npx @angular/cli@22"
6-
# package-tgz: packed @angular-schule/flat-prerender
6+
# package-tgz: packed @angular-schule/prerender-format
77
# angular-version-range: version range for all @angular/* packages, e.g. "^22.0.0" or "~22.0.0"
88
#
9-
# Checks: ng add refuses outputMode "server", ng add + ng build write flat files
9+
# Checks: ng add refuses outputMode "server", ng add + ng build write foo.html files
1010
# (also for a second locale), and a route "index" fails with a clear message.
1111
set -euo pipefail
1212

@@ -61,11 +61,11 @@ expect_failure() {
6161
echo "Failed as expected: $expected"
6262
}
6363

64-
# ng new --ssr uses outputMode "server", which flat output refuses
65-
expect_failure "ships an Angular SSR server" $NG add @angular-schule/flat-prerender --skip-confirmation
64+
# ng new --ssr uses outputMode "server", which the "file" format refuses
65+
expect_failure "ships an Angular SSR server" $NG add @angular-schule/prerender-format --skip-confirmation
6666

6767
set_build_option outputMode '"static"'
68-
$NG add @angular-schule/flat-prerender --skip-confirmation
68+
$NG add @angular-schule/prerender-format --skip-confirmation
6969

7070
cat > src/app/app.routes.ts <<'EOF'
7171
import { Routes } from '@angular/router';
@@ -125,4 +125,4 @@ node -e '
125125
'
126126
expect_failure "Route '/index' cannot be prerendered" $NG build
127127

128-
echo "Angular $RANGE: flat prerender output successful"
128+
echo "Angular $RANGE: prerender format 'file' successful"

‎.github/workflows/main.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
path: src/dist
3535
retention-days: 1
3636

37-
# Builds a fresh Angular app with SSG and checks the flat output end to end,
37+
# Builds a fresh Angular app with SSG and checks the "file" output end to end,
3838
# for the lowest and the latest supported Angular 22 version.
3939
# See .github/scripts/test-angular-app.sh for the checks.
4040
test-angular-versions:
@@ -63,7 +63,7 @@ jobs:
6363
name: dist
6464
path: src/dist
6565

66-
- name: Pack @angular-schule/flat-prerender
66+
- name: Pack @angular-schule/prerender-format
6767
working-directory: src/dist
6868
run: npm pack --pack-destination /tmp
6969

@@ -72,5 +72,5 @@ jobs:
7272
cd /tmp
7373
bash "$GITHUB_WORKSPACE/.github/scripts/test-angular-app.sh" \
7474
"npx --yes @angular/cli@${{ matrix.cli }}" \
75-
"$(ls /tmp/angular-schule-flat-prerender-*.tgz)" \
75+
"$(ls /tmp/angular-schule-prerender-format-*.tgz)" \
7676
"${{ matrix.range }}"

‎.github/workflows/npm-publish.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
working-directory: src/dist
4545
run: |
4646
VERSION=$(node -p "require('./package.json').version")
47-
echo "📦 Package: @angular-schule/flat-prerender"
47+
echo "📦 Package: @angular-schule/prerender-format"
4848
echo "📌 Version: $VERSION"
4949
echo "🏷️ Tag: ${{ inputs.tag }}"
5050
echo ""
@@ -76,4 +76,4 @@ jobs:
7676
echo "==============="
7777
echo ""
7878
echo "✅ Published successfully with provenance!"
79-
echo " https://www.npmjs.com/package/@angular-schule/flat-prerender"
79+
echo " https://www.npmjs.com/package/@angular-schule/prerender-format"

‎CLAUDE.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This file provides guidance when working with code in this repository.
44

55
## Overview
66

7-
`@angular-schule/flat-prerender` is an Angular CLI builder that wraps `@angular/build:application`. With `prerenderOutputStyle: "flat"`, prerendered routes are written as `foo.html` instead of `foo/index.html`, so static hosts serve `/foo` without a trailing slash redirect. It implements the option proposed in [angular/angular-cli#29173](https://github.com/angular/angular-cli/issues/29173). Structure and conventions follow [angular-cli-ghpages](https://github.com/angular-schule/angular-cli-ghpages).
7+
`@angular-schule/prerender-format` is an Angular CLI builder that wraps `@angular/build:application`. With `prerenderFormat: "file"`, prerendered routes are written as `foo.html` instead of `foo/index.html`, so static hosts serve `/foo` without a trailing slash redirect. It implements the option proposed in [angular/angular-cli#29173](https://github.com/angular/angular-cli/issues/29173). Structure and conventions follow [angular-cli-ghpages](https://github.com/angular-schule/angular-cli-ghpages).
88

99
## Development Commands
1010

@@ -26,7 +26,7 @@ Build process: `prebuild` (clean) → `build` (tsc) → `postbuild` (copy metada
2626
```bash
2727
npm run build:schema
2828
```
29-
`application/schema.json` is generated: the schema of `@angular/build:application` from the installed version plus `prerenderOutputStyle`. Regenerate it after updating `@angular/build` and commit the result. `npm test` fails if it is out of sync.
29+
`application/schema.json` is generated: the schema of `@angular/build:application` from the installed version plus `prerenderFormat`. Regenerate it after updating `@angular/build` and commit the result. `npm test` fails if it is out of sync.
3030

3131
### Test
3232
```bash
@@ -47,8 +47,8 @@ For testing changes locally with an Angular project:
4747

4848
2. In your Angular test project:
4949
```bash
50-
npm install --save-dev /path/to/angular-schule-flat-prerender-X.X.X.tgz
51-
ng add @angular-schule/flat-prerender
50+
npm install --save-dev /path/to/angular-schule-prerender-format-X.X.X.tgz
51+
ng add @angular-schule/prerender-format
5252
ng build
5353
```
5454

@@ -65,22 +65,22 @@ Publishes with provenance attestation for supply chain security.
6565

6666
For pre-release versions, after publishing:
6767
```bash
68-
npm dist-tag add @angular-schule/flat-prerender@X.X.X-rc.X next
68+
npm dist-tag add @angular-schule/prerender-format@X.X.X-rc.X next
6969
```
7070

7171
## Architecture
7272

7373
1. **Builder** (`src/application/`):
74-
- `builder.ts` - Angular builder entry point, called by `ng build`. Strips `prerenderOutputStyle`, refuses flat output when a server is shipped, delegates to `buildApplication` and fails a build whose prerendered pages did not go through the wrapper.
74+
- `builder.ts` - Angular builder entry point, called by `ng build`. Strips `prerenderFormat`, refuses the "file" format when a server is shipped, delegates to `buildApplication` and fails a build whose prerendered pages did not go through the wrapper.
7575
- `ships-server.ts` - The rule for "this build ships an SSR server", shared by builder and `ng add`.
76-
- `flat-output.ts` - Wraps the internal `prerenderPages()` of `@angular/build`. `execute-post-bundle.js` reads it from the module's exports object at call time, so replacing the export takes effect for regular and localized builds.
76+
- `file-format.ts` - Wraps the internal `prerenderPages()` of `@angular/build`. `execute-post-bundle.js` reads it from the module's exports object at call time, so replacing the export takes effect for regular and localized builds.
7777
- `schema.json` - Generated, see above.
7878

7979
2. **Schematic** (`src/ng-add.ts`):
80-
- Implements `ng add @angular-schule/flat-prerender`
81-
- Swaps the build target's builder and sets `prerenderOutputStyle: "flat"`
80+
- Implements `ng add @angular-schule/prerender-format`
81+
- Swaps the build target's builder and sets `prerenderFormat: "file"`
8282
- Stops if the build target or one of its configurations ships an SSR server
8383

8484
### Internal API
8585

86-
`prerenderPages()` in `@angular/build/src/utils/server-rendering/prerender.js` is not public. The builder fails loudly if the module or the export is missing, and `flat-output.spec.ts` checks the call site in `execute-post-bundle.js`. The package supports Angular 22 only. `.github/scripts/test-angular-app.sh` builds a fresh Angular app (lowest and latest 22.x in CI) and checks `ng add`, flat output for two locales and the `/index` error.
86+
`prerenderPages()` in `@angular/build/src/utils/server-rendering/prerender.js` is not public. The builder fails loudly if the module or the export is missing, and `file-format.spec.ts` checks the call site in `execute-post-bundle.js`. The package supports Angular 22 only. `.github/scripts/test-angular-app.sh` builds a fresh Angular app (lowest and latest 22.x in CI) and checks `ng add`, the "file" output for two locales and the `/index` error.

‎README.md‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# @angular-schule/flat-prerender
1+
# @angular-schule/prerender-format
22

33
[![NPM version][npm-image]][npm-url]
4-
[![GitHub Actions](https://github.com/angular-schule/flat-prerender/actions/workflows/main.yml/badge.svg)](https://github.com/angular-schule/flat-prerender/actions/workflows/main.yml)
4+
[![GitHub Actions](https://github.com/angular-schule/prerender-format/actions/workflows/main.yml/badge.svg)](https://github.com/angular-schule/prerender-format/actions/workflows/main.yml)
55
[![The MIT License](https://img.shields.io/badge/license-MIT-orange.svg?color=blue&style=flat-square)](http://opensource.org/licenses/MIT)
66

77
**Prerender your Angular app as `about.html` instead of `about/index.html`: no more trailing slash redirects! 🚀**
@@ -42,21 +42,21 @@ The builder is a stopgap: developed and tested for Angular 22, until Angular has
4242
## 🚀 Quick Start <a name="quickstart"></a>
4343

4444
```sh
45-
ng add @angular-schule/flat-prerender
45+
ng add @angular-schule/prerender-format
4646
ng build
4747
```
4848

4949
## ⚙️ Installation <a name="installation"></a>
5050

51-
`ng add @angular-schule/flat-prerender` installs the package and changes the builder of your build target in `angular.json` and sets `prerenderOutputStyle`.
51+
`ng add @angular-schule/prerender-format` installs the package and changes the builder of your build target in `angular.json` and sets `prerenderFormat`.
5252
Your build must already use `"outputMode": "static"`: if the build target (or one of its configurations) ships an SSR server, `ng add` stops and tells you so.
5353

5454
```json
5555
"build": {
56-
"builder": "@angular-schule/flat-prerender:application",
56+
"builder": "@angular-schule/prerender-format:application",
5757
"options": {
5858
"outputMode": "static",
59-
"prerenderOutputStyle": "flat"
59+
"prerenderFormat": "file"
6060
}
6161
}
6262
```
@@ -66,15 +66,17 @@ Use `--project` to choose the project in a workspace with several projects.
6666

6767
## 📦 Options <a name="options"></a>
6868

69-
#### prerenderOutputStyle
69+
#### prerenderFormat
7070

7171
- **optional**
7272
- Default: `directory`
7373

74-
| `prerenderOutputStyle` | Route `blog/my-article` | Start page |
74+
| `prerenderFormat` | Route `blog/my-article` | Start page |
7575
|---|---|---|
7676
| `directory` | `blog/my-article/index.html` | `index.html` |
77-
| `flat` | `blog/my-article.html` | `index.html` |
77+
| `file` | `blog/my-article.html` | `index.html` |
78+
79+
The name and the values follow Astro's [`build.format`](#other-frameworks).
7880

7981
Parent and child routes live side by side: `blog.html` next to the folder `blog/`.
8082
The start page of each locale (for example with base href `/en/`) stays `index.html`.
@@ -98,7 +100,7 @@ Astro's documentation recommends `build.format: 'file'` together with `trailingS
98100

99101
## 🌍 Hosts <a name="hosts"></a>
100102

101-
Measured on **Cloudflare Pages** with a flat build:
103+
Measured on **Cloudflare Pages** with `prerenderFormat: "file"`:
102104

103105
| Request | Response |
104106
|---|---|
@@ -129,5 +131,5 @@ After a successful build, the builder checks that the prerendered pages actually
129131

130132
Code released under the [MIT license](LICENSE).
131133

132-
[npm-url]: https://www.npmjs.com/package/@angular-schule/flat-prerender
133-
[npm-image]: https://badge.fury.io/js/@angular-schule%2Fflat-prerender.svg
134+
[npm-url]: https://www.npmjs.com/package/@angular-schule/prerender-format
135+
[npm-image]: https://badge.fury.io/js/@angular-schule%2Fprerender-format.svg

‎src/application/builder.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/ar
22
import { ApplicationBuilderOptions, buildApplication } from '@angular/build';
33
import * as path from 'path';
44

5-
import { getPrerenderCalls, installFlatPrerender } from './flat-output';
5+
import { getPrerenderCalls, installFileFormat } from './file-format';
66
import { shipsServer } from './ships-server';
77

8-
export type PrerenderOutputStyle = 'directory' | 'flat';
8+
export type PrerenderFormat = 'directory' | 'file';
99

1010
export interface Schema extends ApplicationBuilderOptions {
1111
/**
1212
* File layout of prerendered routes.
1313
* - `directory` (default): `foo/index.html`
14-
* - `flat`: `foo.html`, the start page stays `index.html`
14+
* - `file`: `foo.html`, the start page stays `index.html`
1515
*/
16-
prerenderOutputStyle?: PrerenderOutputStyle;
16+
prerenderFormat?: PrerenderFormat;
1717
}
1818

1919
/**
20-
* Runs `@angular/build:application` and, with `prerenderOutputStyle: "flat"`,
20+
* Runs `@angular/build:application` and, with `prerenderFormat: "file"`,
2121
* writes prerendered routes as `foo.html` instead of `foo/index.html`.
2222
* Exported separately for testing purposes.
2323
*/
2424
export async function* executeBuild(
2525
options: Schema,
2626
context: BuilderContext
2727
): AsyncIterable<BuilderOutput> {
28-
const { prerenderOutputStyle = 'directory', ...applicationOptions } = options;
28+
const { prerenderFormat = 'directory', ...applicationOptions } = options;
2929

30-
if (prerenderOutputStyle !== 'flat') {
30+
if (prerenderFormat !== 'file') {
3131
yield* buildApplication(applicationOptions, context);
3232

3333
return;
3434
}
3535

3636
if (shipsServer(applicationOptions)) {
3737
context.logger.error(
38-
`❌ 'prerenderOutputStyle: "flat"' requires 'outputMode: "static"': ` +
38+
`❌ 'prerenderFormat: "file"' requires 'outputMode: "static"': ` +
3939
`the Angular SSR server looks up prerendered pages as 'index.html'.`
4040
);
4141
yield { success: false };
@@ -44,7 +44,7 @@ export async function* executeBuild(
4444
}
4545

4646
try {
47-
installFlatPrerender(path.dirname(require.resolve('@angular/build/package.json')));
47+
installFileFormat(path.dirname(require.resolve('@angular/build/package.json')));
4848
} catch (e) {
4949
context.logger.error('❌ ' + (e instanceof Error ? e.message : String(e)));
5050
yield { success: false };
@@ -59,7 +59,7 @@ export async function* executeBuild(
5959
const calls = getPrerenderCalls();
6060
if (result.success && calls === callsBefore) {
6161
context.logger.error(
62-
`❌ 'prerenderOutputStyle: "flat"' had no effect: no pages were prerendered through @angular-schule/flat-prerender. ` +
62+
`❌ 'prerenderFormat: "file"' had no effect: no pages were prerendered through @angular-schule/prerender-format. ` +
6363
`Check that prerendering is enabled ('outputMode: "static"' with server routes, or 'prerender'). ` +
6464
`If it is, this version of @angular/build is not supported.`
6565
);
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@ import * as fs from 'fs';
22
import * as path from 'path';
33

44
import {
5-
flattenOutput,
5+
toFileOutput,
66
getPrerenderCalls,
7-
installFlatPrerender,
7+
installFileFormat,
88
PrerenderPages,
9-
toFlatPath,
9+
toFilePath,
1010
wrapPrerenderPages
11-
} from './flat-output';
11+
} from './file-format';
1212

1313
const angularBuildRoot = path.dirname(require.resolve('@angular/build/package.json'));
1414

15-
describe('toFlatPath', () => {
16-
it('maps nested index files to flat files', () => {
17-
expect(toFlatPath('blog/index.html')).toBe('blog.html');
18-
expect(toFlatPath('blog/my-article/index.html')).toBe('blog/my-article.html');
15+
describe('toFilePath', () => {
16+
it('maps nested index files to .html files', () => {
17+
expect(toFilePath('blog/index.html')).toBe('blog.html');
18+
expect(toFilePath('blog/my-article/index.html')).toBe('blog/my-article.html');
1919
});
2020

2121
it('keeps the root index.html of a build (start page, locale start page)', () => {
22-
expect(toFlatPath('index.html')).toBe('index.html');
22+
expect(toFilePath('index.html')).toBe('index.html');
2323
});
2424

2525
it('leaves other files untouched', () => {
26-
expect(toFlatPath('blog/feed.xml')).toBe('blog/feed.xml');
27-
expect(toFlatPath('blog/my-index.html')).toBe('blog/my-index.html');
26+
expect(toFilePath('blog/feed.xml')).toBe('blog/feed.xml');
27+
expect(toFilePath('blog/my-index.html')).toBe('blog/my-index.html');
2828
});
2929
});
3030

31-
describe('flattenOutput', () => {
31+
describe('toFileOutput', () => {
3232
const file = (content: string) => ({ content, appShellRoute: false });
3333

3434
it('keeps parent and child routes side by side', () => {
3535
expect(
36-
flattenOutput({
36+
toFileOutput({
3737
'index.html': file('home'),
3838
'blog/index.html': file('blog'),
3939
'blog/a/index.html': file('a')
@@ -49,30 +49,30 @@ describe('flattenOutput', () => {
4949
});
5050

5151
it('reports the route /index, which would overwrite the start page', () => {
52-
const { errors } = flattenOutput({ 'index.html': file('home'), 'index/index.html': file('x') });
52+
const { errors } = toFileOutput({ 'index.html': file('home'), 'index/index.html': file('x') });
5353

5454
expect(errors).toEqual([expect.stringContaining("Route '/index' cannot be prerendered")]);
5555
expect(errors[0]).toContain("would be served as '/'");
5656
});
5757

5858
it('reports nested routes ending in index', () => {
59-
const { errors } = flattenOutput({ 'docs/index/index.html': file('x') });
59+
const { errors } = toFileOutput({ 'docs/index/index.html': file('x') });
6060

6161
expect(errors).toEqual([expect.stringContaining("Route '/docs/index' cannot be prerendered")]);
6262
expect(errors[0]).toContain("would be served as '/docs/'");
6363
});
6464

6565
it('reports colliding routes', () => {
66-
const { errors } = flattenOutput({ 'foo/index.html': file('1'), 'foo.html': file('2') });
66+
const { errors } = toFileOutput({ 'foo/index.html': file('1'), 'foo.html': file('2') });
6767

6868
expect(errors).toEqual([
69-
"Routes '/foo' and '/foo.html' both map to the file 'foo.html' with 'prerenderOutputStyle: \"flat\"'."
69+
"Routes '/foo' and '/foo.html' both map to the file 'foo.html' with 'prerenderFormat: \"file\"'."
7070
]);
7171
});
7272
});
7373

7474
describe('wrapPrerenderPages', () => {
75-
it('flattens output, keeps the rest of the result and counts the call', async () => {
75+
it('renames output, keeps the rest of the result and counts the call', async () => {
7676
const original: PrerenderPages = async (...args) => ({
7777
errors: [],
7878
warnings: ['w'],
@@ -106,14 +106,14 @@ describe('wrapPrerenderPages', () => {
106106
});
107107
});
108108

109-
describe('installFlatPrerender', () => {
109+
describe('installFileFormat', () => {
110110
it('patches the module instance used by execute-post-bundle, once', () => {
111111
const prerender = require(path.join(angularBuildRoot, 'src/utils/server-rendering/prerender.js'));
112112
const original = prerender.prerenderPages;
113113

114-
installFlatPrerender(angularBuildRoot);
114+
installFileFormat(angularBuildRoot);
115115
const patched = prerender.prerenderPages;
116-
installFlatPrerender(angularBuildRoot);
116+
installFileFormat(angularBuildRoot);
117117

118118
expect(patched).not.toBe(original);
119119
expect(prerender.prerenderPages).toBe(patched);
@@ -129,6 +129,6 @@ describe('installFlatPrerender', () => {
129129
});
130130

131131
it('rejects an unsupported @angular/build layout', () => {
132-
expect(() => installFlatPrerender('/does/not/exist')).toThrow(/not supported/);
132+
expect(() => installFileFormat('/does/not/exist')).toThrow(/not supported/);
133133
});
134134
});

0 commit comments

Comments
 (0)