@pithyjs/ssr 0.0.0-stage → 0.1.2-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/bin/pithy-ssg.mjs +23 -0
- package/dist/browser.d.ts +38 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +40 -0
- package/dist/browser.js.map +1 -0
- package/dist/build/cli-args.d.ts +104 -0
- package/dist/build/cli-args.d.ts.map +1 -0
- package/dist/build/cli-args.js +88 -0
- package/dist/build/cli-args.js.map +1 -0
- package/dist/build/cli-exit.d.ts +95 -0
- package/dist/build/cli-exit.d.ts.map +1 -0
- package/dist/build/cli-exit.js +237 -0
- package/dist/build/cli-exit.js.map +1 -0
- package/dist/build/cli.d.ts +11 -0
- package/dist/build/cli.d.ts.map +1 -0
- package/dist/build/cli.js +93 -0
- package/dist/build/cli.js.map +1 -0
- package/dist/build/collect-css.d.ts +168 -0
- package/dist/build/collect-css.d.ts.map +1 -0
- package/dist/build/collect-css.js +479 -0
- package/dist/build/collect-css.js.map +1 -0
- package/dist/build/css-live-props.d.ts +89 -0
- package/dist/build/css-live-props.d.ts.map +1 -0
- package/dist/build/css-live-props.js +704 -0
- package/dist/build/css-live-props.js.map +1 -0
- package/dist/build/css-purge.d.ts +153 -0
- package/dist/build/css-purge.d.ts.map +1 -0
- package/dist/build/css-purge.js +463 -0
- package/dist/build/css-purge.js.map +1 -0
- package/dist/build/index.d.ts +24 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +31 -0
- package/dist/build/index.js.map +1 -0
- package/dist/build/not-found.d.ts +94 -0
- package/dist/build/not-found.d.ts.map +1 -0
- package/dist/build/not-found.js +99 -0
- package/dist/build/not-found.js.map +1 -0
- package/dist/build/relative-links.d.ts +31 -0
- package/dist/build/relative-links.d.ts.map +1 -0
- package/dist/build/relative-links.js +79 -0
- package/dist/build/relative-links.js.map +1 -0
- package/dist/build/shell-head.d.ts +68 -0
- package/dist/build/shell-head.d.ts.map +1 -0
- package/dist/build/shell-head.js +236 -0
- package/dist/build/shell-head.js.map +1 -0
- package/dist/build/sprite-ssg.d.ts +61 -0
- package/dist/build/sprite-ssg.d.ts.map +1 -0
- package/dist/build/sprite-ssg.js +114 -0
- package/dist/build/sprite-ssg.js.map +1 -0
- package/dist/build/ssg.d.ts +117 -0
- package/dist/build/ssg.d.ts.map +1 -0
- package/dist/build/ssg.js +404 -0
- package/dist/build/ssg.js.map +1 -0
- package/dist/build/vite-ssg.d.ts +396 -0
- package/dist/build/vite-ssg.d.ts.map +1 -0
- package/dist/build/vite-ssg.js +1130 -0
- package/dist/build/vite-ssg.js.map +1 -0
- package/dist/client/bootstrap.d.ts +51 -0
- package/dist/client/bootstrap.d.ts.map +1 -0
- package/dist/client/bootstrap.js +121 -0
- package/dist/client/bootstrap.js.map +1 -0
- package/dist/client/hydrate.d.ts +60 -0
- package/dist/client/hydrate.d.ts.map +1 -0
- package/dist/client/hydrate.js +168 -0
- package/dist/client/hydrate.js.map +1 -0
- package/dist/client/index.d.ts +21 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +16 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/server/app.d.ts +166 -0
- package/dist/server/app.d.ts.map +1 -0
- package/dist/server/app.js +200 -0
- package/dist/server/app.js.map +1 -0
- package/dist/server/context.d.ts +238 -0
- package/dist/server/context.d.ts.map +1 -0
- package/dist/server/context.js +371 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/environment.d.ts +88 -0
- package/dist/server/environment.d.ts.map +1 -0
- package/dist/server/environment.js +261 -0
- package/dist/server/environment.js.map +1 -0
- package/dist/server/html-shell.d.ts +41 -0
- package/dist/server/html-shell.d.ts.map +1 -0
- package/dist/server/html-shell.js +208 -0
- package/dist/server/html-shell.js.map +1 -0
- package/dist/server/index.d.ts +26 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +37 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/layout-taint.d.ts +62 -0
- package/dist/server/layout-taint.d.ts.map +1 -0
- package/dist/server/layout-taint.js +313 -0
- package/dist/server/layout-taint.js.map +1 -0
- package/dist/server/loaders.d.ts +194 -0
- package/dist/server/loaders.d.ts.map +1 -0
- package/dist/server/loaders.js +479 -0
- package/dist/server/loaders.js.map +1 -0
- package/dist/server/render-route.d.ts +102 -0
- package/dist/server/render-route.d.ts.map +1 -0
- package/dist/server/render-route.js +182 -0
- package/dist/server/render-route.js.map +1 -0
- package/dist/server/render.d.ts +80 -0
- package/dist/server/render.d.ts.map +1 -0
- package/dist/server/render.js +256 -0
- package/dist/server/render.js.map +1 -0
- package/dist/server/router-adapter.d.ts +134 -0
- package/dist/server/router-adapter.d.ts.map +1 -0
- package/dist/server/router-adapter.js +225 -0
- package/dist/server/router-adapter.js.map +1 -0
- package/dist/server/serialization.d.ts +54 -0
- package/dist/server/serialization.d.ts.map +1 -0
- package/dist/server/serialization.js +287 -0
- package/dist/server/serialization.js.map +1 -0
- package/dist/server/ssr-worker.d.ts +18 -0
- package/dist/server/ssr-worker.d.ts.map +1 -0
- package/dist/server/ssr-worker.js +118 -0
- package/dist/server/ssr-worker.js.map +1 -0
- package/dist/server/utils.d.ts +11 -0
- package/dist/server/utils.d.ts.map +1 -0
- package/dist/server/utils.js +14 -0
- package/dist/server/utils.js.map +1 -0
- package/dist/server/worker-pool.d.ts +153 -0
- package/dist/server/worker-pool.d.ts.map +1 -0
- package/dist/server/worker-pool.js +451 -0
- package/dist/server/worker-pool.js.map +1 -0
- package/dist/server/worker-protocol.d.ts +61 -0
- package/dist/server/worker-protocol.d.ts.map +1 -0
- package/dist/server/worker-protocol.js +9 -0
- package/dist/server/worker-protocol.js.map +1 -0
- package/dist/shared/css-purge.d.ts +71 -0
- package/dist/shared/css-purge.d.ts.map +1 -0
- package/dist/shared/css-purge.js +177 -0
- package/dist/shared/css-purge.js.map +1 -0
- package/dist/shared/entry-path.d.ts +41 -0
- package/dist/shared/entry-path.d.ts.map +1 -0
- package/dist/shared/entry-path.js +87 -0
- package/dist/shared/entry-path.js.map +1 -0
- package/dist/shared/head.d.ts +87 -0
- package/dist/shared/head.d.ts.map +1 -0
- package/dist/shared/head.js +412 -0
- package/dist/shared/head.js.map +1 -0
- package/dist/shared/shell-helpers.d.ts +69 -0
- package/dist/shared/shell-helpers.d.ts.map +1 -0
- package/dist/shared/shell-helpers.js +160 -0
- package/dist/shared/shell-helpers.js.map +1 -0
- package/dist/shared/types.d.ts +332 -0
- package/dist/shared/types.d.ts.map +1 -0
- package/dist/shared/types.js +27 -0
- package/dist/shared/types.js.map +1 -0
- package/package.json +84 -3
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.build.shell-head",
|
|
4
|
+
"title": "SSG App-Shell Head Bridge",
|
|
5
|
+
"category": "feature",
|
|
6
|
+
"risk": "medium"
|
|
7
|
+
}
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Carry the head an app declares in its HTML shell into the SSG artifact.
|
|
11
|
+
*
|
|
12
|
+
* SSG builds its `<head>` from scratch rather than from `index.html`, so a
|
|
13
|
+
* `<title>` or description written there reached the dev server and `vite
|
|
14
|
+
* build` but never the static output — the shipping page had no title element
|
|
15
|
+
* at all and an empty search snippet.
|
|
16
|
+
*
|
|
17
|
+
* Links are carried only for relations whose target the build actually emits.
|
|
18
|
+
* Static output copies the app's `publicDir`, so a favicon resolves; the build
|
|
19
|
+
* pipeline owns stylesheets and preloads, and copying the shell's versions of
|
|
20
|
+
* those would point at files it never writes.
|
|
21
|
+
*
|
|
22
|
+
* The shell is parsed with happy-dom rather than by regex. `buildSSG()` already
|
|
23
|
+
* cannot run without it (rendering needs the DOM), so this adds no requirement,
|
|
24
|
+
* and it is the difference between handling entities, implicit `<head>` and
|
|
25
|
+
* attribute quoting correctly and approximating all three.
|
|
26
|
+
*/
|
|
27
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
28
|
+
import { resolve } from 'node:path';
|
|
29
|
+
import { createRequire } from 'module';
|
|
30
|
+
import { mergeHead } from '../shared/shell-helpers.js';
|
|
31
|
+
const require = createRequire(import.meta.url);
|
|
32
|
+
/**
|
|
33
|
+
* Meta names the shell must not hand over.
|
|
34
|
+
*
|
|
35
|
+
* `htmlShell` emits its own charset and viewport before rendering head state,
|
|
36
|
+
* so carrying these would duplicate them — and a second `<meta charset>` is
|
|
37
|
+
* invalid, not merely redundant.
|
|
38
|
+
*/
|
|
39
|
+
const SHELL_OWNED_META = new Set(['viewport']);
|
|
40
|
+
/**
|
|
41
|
+
* Link relations worth carrying into a static page.
|
|
42
|
+
*
|
|
43
|
+
* An allowlist, because the cost of a wrong entry is a reference to a file the
|
|
44
|
+
* artifact never ships. These resolve against the copied `publicDir`; anything
|
|
45
|
+
* the build emits itself — stylesheets, preloads, modulepreloads — is excluded
|
|
46
|
+
* because output that inlines its own CSS has no such file to point at.
|
|
47
|
+
*/
|
|
48
|
+
const CARRIED_LINK_RELS = new Set([
|
|
49
|
+
'icon',
|
|
50
|
+
'shortcut icon',
|
|
51
|
+
'apple-touch-icon',
|
|
52
|
+
'mask-icon',
|
|
53
|
+
'manifest',
|
|
54
|
+
]);
|
|
55
|
+
/**
|
|
56
|
+
* Named entities the DOM leaves undecoded, which would ship as visible source.
|
|
57
|
+
*
|
|
58
|
+
* happy-dom resolves `&`, ` ` and numeric/hex references but not the
|
|
59
|
+
* rest of the HTML5 table, so `—` survives parsing and is then escaped
|
|
60
|
+
* again at render, putting a literal `&mdash;` in the browser tab. The
|
|
61
|
+
* leftovers cannot simply be decoded here: the parser has already consumed one
|
|
62
|
+
* layer, so `&mdash;` arrives as `—` and decoding it again would
|
|
63
|
+
* corrupt text that was correct. Reported instead of guessed.
|
|
64
|
+
*/
|
|
65
|
+
function warnUndecodedEntities(where, value) {
|
|
66
|
+
const residual = value.match(/&[a-zA-Z][a-zA-Z0-9]*;/g);
|
|
67
|
+
if (!residual)
|
|
68
|
+
return;
|
|
69
|
+
console.warn(` Warning: the app shell's ${where} contains ${[...new Set(residual)].join(', ')}, ` +
|
|
70
|
+
`which the HTML parser does not decode.\n` +
|
|
71
|
+
` If the symbol was meant, write the character itself or a numeric ` +
|
|
72
|
+
`reference such as —. If the literal text was meant, it already ` +
|
|
73
|
+
`renders correctly and this warning does not apply — the two are ` +
|
|
74
|
+
`indistinguishable after parsing.`);
|
|
75
|
+
}
|
|
76
|
+
function readLink(el) {
|
|
77
|
+
const rel = el.getAttribute('rel');
|
|
78
|
+
const href = el.getAttribute('href');
|
|
79
|
+
if (rel === null || href === null)
|
|
80
|
+
return undefined;
|
|
81
|
+
if (!CARRIED_LINK_RELS.has(rel.toLowerCase()))
|
|
82
|
+
return undefined;
|
|
83
|
+
const link = { rel, href };
|
|
84
|
+
const type = el.getAttribute('type');
|
|
85
|
+
if (type !== null)
|
|
86
|
+
link.type = type;
|
|
87
|
+
const media = el.getAttribute('media');
|
|
88
|
+
if (media !== null)
|
|
89
|
+
link.media = media;
|
|
90
|
+
// `sizes` picks which icon a browser uses when a shell declares several, so
|
|
91
|
+
// dropping it silently changes the chosen icon rather than losing a hint.
|
|
92
|
+
const sizes = el.getAttribute('sizes');
|
|
93
|
+
if (sizes !== null)
|
|
94
|
+
link.sizes = sizes;
|
|
95
|
+
const crossorigin = el.getAttribute('crossorigin');
|
|
96
|
+
if (crossorigin === 'anonymous' || crossorigin === 'use-credentials') {
|
|
97
|
+
link.crossorigin = crossorigin;
|
|
98
|
+
}
|
|
99
|
+
return link;
|
|
100
|
+
}
|
|
101
|
+
function readMeta(el) {
|
|
102
|
+
const content = el.getAttribute('content');
|
|
103
|
+
if (content === null)
|
|
104
|
+
return undefined;
|
|
105
|
+
const name = el.getAttribute('name');
|
|
106
|
+
if (name !== null) {
|
|
107
|
+
if (SHELL_OWNED_META.has(name.toLowerCase()))
|
|
108
|
+
return undefined;
|
|
109
|
+
return { name, content };
|
|
110
|
+
}
|
|
111
|
+
const property = el.getAttribute('property');
|
|
112
|
+
if (property !== null)
|
|
113
|
+
return { property, content };
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
/** @codexApi {"parent":"pithy.ssr.build.shell-head","name":"parseShellHead","stability":"experimental","signature":"(html: string) => Partial<HeadState>"} */
|
|
117
|
+
/**
|
|
118
|
+
* Parse the carried head values out of an HTML shell.
|
|
119
|
+
*
|
|
120
|
+
* @param html - Contents of the app's shell document.
|
|
121
|
+
* @returns Title, meta and link entries worth carrying, empty when there are none.
|
|
122
|
+
*/
|
|
123
|
+
export function parseShellHead(html) {
|
|
124
|
+
let Window;
|
|
125
|
+
try {
|
|
126
|
+
Window = require('happy-dom').Window;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
console.warn(` Warning: happy-dom could not be loaded, so the app shell's head was ` +
|
|
130
|
+
`not carried into the output. Pages that name themselves are ` +
|
|
131
|
+
`unaffected; the rest ship untitled.`);
|
|
132
|
+
return {};
|
|
133
|
+
}
|
|
134
|
+
const window = new Window();
|
|
135
|
+
try {
|
|
136
|
+
const doc = window.document;
|
|
137
|
+
if (typeof doc.write !== 'function') {
|
|
138
|
+
console.warn(` Warning: the DOM implementation exposes no document.write, so the ` +
|
|
139
|
+
`app shell's head was not carried into the output.`);
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
doc.write(html);
|
|
143
|
+
const title = doc.title.replace(/\s+/g, ' ').trim();
|
|
144
|
+
if (title.length > 0)
|
|
145
|
+
warnUndecodedEntities('<title>', title);
|
|
146
|
+
const meta = [];
|
|
147
|
+
for (const el of doc.head.querySelectorAll('meta')) {
|
|
148
|
+
const tag = readMeta(el);
|
|
149
|
+
if (tag) {
|
|
150
|
+
warnUndecodedEntities(`<meta ${tag.name ?? tag.property}> content`, tag.content);
|
|
151
|
+
meta.push(tag);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const link = [];
|
|
155
|
+
for (const el of doc.head.querySelectorAll('link')) {
|
|
156
|
+
const tag = readLink(el);
|
|
157
|
+
if (tag)
|
|
158
|
+
link.push(tag);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
...(title.length > 0 ? { title } : {}),
|
|
162
|
+
...(meta.length > 0 ? { meta } : {}),
|
|
163
|
+
...(link.length > 0 ? { link } : {}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
void window.happyDOM?.close();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** @codexApi {"parent":"pithy.ssr.build.shell-head","name":"readShellHead","stability":"experimental","signature":"(root: string, options?: ReadShellHeadOptions) => Promise<Partial<HeadState>>"} */
|
|
171
|
+
/**
|
|
172
|
+
* Read the head an app declares in its HTML shell.
|
|
173
|
+
*
|
|
174
|
+
* An app with no shell file has nothing to carry and is not an error. A shell
|
|
175
|
+
* that exists but cannot be read or transformed is: `vite build` fails when a
|
|
176
|
+
* `transformIndexHtml` hook throws, and continuing here would report success
|
|
177
|
+
* while writing the untitled page this module exists to prevent.
|
|
178
|
+
*
|
|
179
|
+
* @param root - App root directory.
|
|
180
|
+
* @param options - See {@link ReadShellHeadOptions}.
|
|
181
|
+
* @returns Carried head values, empty when there is no shell file.
|
|
182
|
+
* @throws When the shell exists but cannot be read or transformed.
|
|
183
|
+
*/
|
|
184
|
+
export async function readShellHead(root, options) {
|
|
185
|
+
const shellPath = resolve(root, options?.shellFile ?? 'index.html');
|
|
186
|
+
if (!existsSync(shellPath))
|
|
187
|
+
return {};
|
|
188
|
+
try {
|
|
189
|
+
const raw = readFileSync(shellPath, 'utf-8');
|
|
190
|
+
const html = options?.transform ? await options.transform(raw) : raw;
|
|
191
|
+
return parseShellHead(html);
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
throw new Error(`Could not read the app shell at ${shellPath}: ` +
|
|
195
|
+
`${err instanceof Error ? err.message : String(err)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Drop shell-file links for any relation a later layer also declares.
|
|
200
|
+
*
|
|
201
|
+
* Deliberately coarser than the `rel`+`href` identity `head()` merges on, and
|
|
202
|
+
* for a different question: not "is this the same link" but "who owns this
|
|
203
|
+
* relation". Two `<link rel="icon">` lets the browser choose by rules the page
|
|
204
|
+
* cannot see, so a page declaring one must displace the shell's whole icon
|
|
205
|
+
* set — including a multi-`sizes` set, which is coherent only as a set.
|
|
206
|
+
* Relations no later layer claims are untouched.
|
|
207
|
+
*/
|
|
208
|
+
function withoutClaimedRels(shellFile, claimed) {
|
|
209
|
+
const rels = new Set(claimed.map(link => link.rel.toLowerCase()));
|
|
210
|
+
return shellFile.filter(link => !rels.has(link.rel.toLowerCase()));
|
|
211
|
+
}
|
|
212
|
+
/** @codexApi {"parent":"pithy.ssr.build.shell-head","name":"resolvePageHead","stability":"experimental","signature":"(shellFile: Partial<HeadState>, shell: Partial<HeadState>, page: Partial<HeadState>) => HeadState"} */
|
|
213
|
+
/**
|
|
214
|
+
* Layer a page's head over the app shell's, over the shell file's.
|
|
215
|
+
*
|
|
216
|
+
* Precedence for the title is page, then app shell, then shell file — so a
|
|
217
|
+
* route that names itself still wins, and one that names nothing inherits
|
|
218
|
+
* rather than shipping untitled. Meta follows the same precedence via
|
|
219
|
+
* `mergeHead`; links the shell file carries yield per relation, since two
|
|
220
|
+
* `<link rel="icon">` lets the shell's beat the page's on rules the page
|
|
221
|
+
* cannot see.
|
|
222
|
+
*
|
|
223
|
+
* @param shellFile - Head values read from the app's HTML shell.
|
|
224
|
+
* @param shell - Head state from the App shell render.
|
|
225
|
+
* @param page - Head state from the page render.
|
|
226
|
+
* @returns The merged head state.
|
|
227
|
+
*/
|
|
228
|
+
export function resolvePageHead(shellFile, shell, page) {
|
|
229
|
+
const claimed = [...(shell.link ?? []), ...(page.link ?? [])];
|
|
230
|
+
const carried = {
|
|
231
|
+
...shellFile,
|
|
232
|
+
link: withoutClaimedRels(shellFile.link ?? [], claimed),
|
|
233
|
+
};
|
|
234
|
+
return mergeHead(mergeHead(carried, shell), page);
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=shell-head.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-head.js","sourceRoot":"","sources":["../../src/build/shell-head.ts"],"names":[],"mappings":"AAAA;;;;;;;EAOE;AAEF;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAQvC,OAAO,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAEvD,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAmB/C;;;;;;GAMG;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;AAE/C;;;;;;;GAOG;AACH,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAChC,MAAM;IACN,eAAe;IACf,kBAAkB;IAClB,WAAW;IACX,UAAU;CACX,CAAC,CAAC;AAEH;;;;;;;;;GASG;AACH,SAAS,qBAAqB,CAAC,KAAa,EAAE,KAAa;IACzD,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;IACxD,IAAI,CAAC,QAAQ;QAAE,OAAO;IACtB,OAAO,CAAC,IAAI,CACV,8BAA8B,KAAK,aAAa,CAAC,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI;QACnF,0CAA0C;QAC1C,qEAAqE;QACrE,uEAAuE;QACvE,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,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;IAC9D,MAAM,OAAO,GAAG;QACd,GAAG,SAAS;QACZ,IAAI,EAAE,kBAAkB,CAAC,SAAS,CAAC,IAAI,IAAI,EAAE,EAAE,OAAO,CAAC;KACxD,CAAC;IACF,OAAO,SAAS,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,IAAI,CAAC,CAAC;AACpD,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.ssr.build.shell-head\",\n \"title\": \"SSG App-Shell Head Bridge\",\n \"category\": \"feature\",\n \"risk\": \"medium\"\n}\n*/\n\n/**\n * Carry the head an app declares in its HTML shell into the SSG artifact.\n *\n * SSG builds its `<head>` from scratch rather than from `index.html`, so a\n * `<title>` or description written there reached the dev server and `vite\n * build` but never the static output — the shipping page had no title element\n * at all and an empty search snippet.\n *\n * Links are carried only for relations whose target the build actually emits.\n * Static output copies the app's `publicDir`, so a favicon resolves; the build\n * pipeline owns stylesheets and preloads, and copying the shell's versions of\n * those would point at files it never writes.\n *\n * The shell is parsed with happy-dom rather than by regex. `buildSSG()` already\n * cannot run without it (rendering needs the DOM), so this adds no requirement,\n * and it is the difference between handling entities, implicit `<head>` and\n * attribute quoting correctly and approximating all three.\n */\n\nimport { existsSync, readFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { createRequire } from 'module';\n\nimport type {\n HeadState,\n MetaTag,\n LinkTag,\n CrossOrigin,\n} from '../shared/types.js';\nimport { mergeHead } from '../shared/shell-helpers.js';\n\nconst require = createRequire(import.meta.url);\n\ninterface ShellDocument {\n title: string;\n head: {\n querySelectorAll: (selector: string) => Iterable<ShellElement>;\n };\n}\n\ninterface ShellElement {\n getAttribute: (name: string) => string | null;\n}\n\ninterface ShellWindow {\n document: ShellDocument;\n happyDOM?: { close: () => Promise<void> | void };\n close?: () => void;\n}\n\n/**\n * Meta names the shell must not hand over.\n *\n * `htmlShell` emits its own charset and viewport before rendering head state,\n * so carrying these would duplicate them — and a second `<meta charset>` is\n * invalid, not merely redundant.\n */\nconst SHELL_OWNED_META = new Set(['viewport']);\n\n/**\n * Link relations worth carrying into a static page.\n *\n * An allowlist, because the cost of a wrong entry is a reference to a file the\n * artifact never ships. These resolve against the copied `publicDir`; anything\n * the build emits itself — stylesheets, preloads, modulepreloads — is excluded\n * because output that inlines its own CSS has no such file to point at.\n */\nconst CARRIED_LINK_RELS = new Set([\n 'icon',\n 'shortcut icon',\n 'apple-touch-icon',\n 'mask-icon',\n 'manifest',\n]);\n\n/**\n * Named entities the DOM leaves undecoded, which would ship as visible source.\n *\n * happy-dom resolves `&`, ` ` and numeric/hex references but not the\n * rest of the HTML5 table, so `—` survives parsing and is then escaped\n * again at render, putting a literal `&mdash;` in the browser tab. The\n * leftovers cannot simply be decoded here: the parser has already consumed one\n * layer, so `&mdash;` arrives as `—` and decoding it again would\n * corrupt text that was correct. Reported instead of guessed.\n */\nfunction warnUndecodedEntities(where: string, value: string): void {\n const residual = value.match(/&[a-zA-Z][a-zA-Z0-9]*;/g);\n if (!residual) return;\n console.warn(\n ` Warning: the app shell's ${where} contains ${[...new Set(residual)].join(', ')}, ` +\n `which the HTML parser does not decode.\\n` +\n ` If the symbol was meant, write the character itself or a numeric ` +\n `reference such as —. If the literal text was meant, it already ` +\n `renders correctly and this warning does not apply — the two are ` +\n `indistinguishable after parsing.`\n );\n}\n\nfunction readLink(el: ShellElement): LinkTag | undefined {\n const rel = el.getAttribute('rel');\n const href = el.getAttribute('href');\n if (rel === null || href === null) return undefined;\n if (!CARRIED_LINK_RELS.has(rel.toLowerCase())) return undefined;\n\n const link: LinkTag = { rel, href };\n const type = el.getAttribute('type');\n if (type !== null) link.type = type;\n const media = el.getAttribute('media');\n if (media !== null) link.media = media;\n // `sizes` picks which icon a browser uses when a shell declares several, so\n // dropping it silently changes the chosen icon rather than losing a hint.\n const sizes = el.getAttribute('sizes');\n if (sizes !== null) link.sizes = sizes;\n const crossorigin = el.getAttribute('crossorigin');\n if (crossorigin === 'anonymous' || crossorigin === 'use-credentials') {\n link.crossorigin = crossorigin satisfies CrossOrigin;\n }\n return link;\n}\n\nfunction readMeta(el: ShellElement): MetaTag | undefined {\n const content = el.getAttribute('content');\n if (content === null) return undefined;\n\n const name = el.getAttribute('name');\n if (name !== null) {\n if (SHELL_OWNED_META.has(name.toLowerCase())) return undefined;\n return { name, content };\n }\n\n const property = el.getAttribute('property');\n if (property !== null) return { property, content };\n\n return undefined;\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.build.shell-head\",\"name\":\"parseShellHead\",\"stability\":\"experimental\",\"signature\":\"(html: string) => Partial<HeadState>\"} */\n/**\n * Parse the carried head values out of an HTML shell.\n *\n * @param html - Contents of the app's shell document.\n * @returns Title, meta and link entries worth carrying, empty when there are none.\n */\nexport function parseShellHead(html: string): Partial<HeadState> {\n let Window: new (options?: { url?: string }) => ShellWindow;\n try {\n Window = (require('happy-dom') as { Window: typeof Window }).Window;\n } catch {\n console.warn(\n ` Warning: happy-dom could not be loaded, so the app shell's head was ` +\n `not carried into the output. Pages that name themselves are ` +\n `unaffected; the rest ship untitled.`\n );\n return {};\n }\n\n const window = new Window();\n try {\n const doc = window.document as ShellDocument & {\n write?: (markup: string) => void;\n };\n if (typeof doc.write !== 'function') {\n console.warn(\n ` Warning: the DOM implementation exposes no document.write, so the ` +\n `app shell's head was not carried into the output.`\n );\n return {};\n }\n doc.write(html);\n\n const title = doc.title.replace(/\\s+/g, ' ').trim();\n if (title.length > 0) warnUndecodedEntities('<title>', title);\n\n const meta: MetaTag[] = [];\n for (const el of doc.head.querySelectorAll('meta')) {\n const tag = readMeta(el);\n if (tag) {\n warnUndecodedEntities(\n `<meta ${tag.name ?? tag.property}> content`,\n tag.content\n );\n meta.push(tag);\n }\n }\n\n const link: LinkTag[] = [];\n for (const el of doc.head.querySelectorAll('link')) {\n const tag = readLink(el);\n if (tag) link.push(tag);\n }\n\n return {\n ...(title.length > 0 ? { title } : {}),\n ...(meta.length > 0 ? { meta } : {}),\n ...(link.length > 0 ? { link } : {}),\n };\n } finally {\n void window.happyDOM?.close();\n }\n}\n\n/** Options for {@link readShellHead}. */\n/** @codexApi {\"parent\":\"pithy.ssr.build.shell-head\",\"name\":\"ReadShellHeadOptions\",\"stability\":\"experimental\",\"signature\":\"interface ReadShellHeadOptions\"} */\nexport interface ReadShellHeadOptions {\n /** Shell filename relative to `root` (default: `index.html`). */\n shellFile?: string;\n /**\n * Run the shell through Vite before parsing.\n *\n * `%VITE_*%` placeholders and `transformIndexHtml` plugin hooks are what dev\n * and `vite build` render. Skipping them carries a literal `%VITE_APP_TITLE%`\n * into the artifact and diverges from every other way the app is built.\n *\n * Called once for the whole build, and `buildSSG()` passes `/` as the URL, so\n * a plugin whose `transformIndexHtml` varies by URL contributes its `/`\n * variant to every page.\n */\n transform?: (html: string) => Promise<string>;\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.build.shell-head\",\"name\":\"readShellHead\",\"stability\":\"experimental\",\"signature\":\"(root: string, options?: ReadShellHeadOptions) => Promise<Partial<HeadState>>\"} */\n/**\n * Read the head an app declares in its HTML shell.\n *\n * An app with no shell file has nothing to carry and is not an error. A shell\n * that exists but cannot be read or transformed is: `vite build` fails when a\n * `transformIndexHtml` hook throws, and continuing here would report success\n * while writing the untitled page this module exists to prevent.\n *\n * @param root - App root directory.\n * @param options - See {@link ReadShellHeadOptions}.\n * @returns Carried head values, empty when there is no shell file.\n * @throws When the shell exists but cannot be read or transformed.\n */\nexport async function readShellHead(\n root: string,\n options?: ReadShellHeadOptions\n): Promise<Partial<HeadState>> {\n const shellPath = resolve(root, options?.shellFile ?? 'index.html');\n if (!existsSync(shellPath)) return {};\n\n try {\n const raw = readFileSync(shellPath, 'utf-8');\n const html = options?.transform ? await options.transform(raw) : raw;\n return parseShellHead(html);\n } catch (err) {\n throw new Error(\n `Could not read the app shell at ${shellPath}: ` +\n `${err instanceof Error ? err.message : String(err)}`\n );\n }\n}\n\n/**\n * Drop shell-file links for any relation a later layer also declares.\n *\n * Deliberately coarser than the `rel`+`href` identity `head()` merges on, and\n * for a different question: not \"is this the same link\" but \"who owns this\n * relation\". Two `<link rel=\"icon\">` lets the browser choose by rules the page\n * cannot see, so a page declaring one must displace the shell's whole icon\n * set — including a multi-`sizes` set, which is coherent only as a set.\n * Relations no later layer claims are untouched.\n */\nfunction withoutClaimedRels(\n shellFile: readonly LinkTag[],\n claimed: readonly LinkTag[]\n): LinkTag[] {\n const rels = new Set(claimed.map(link => link.rel.toLowerCase()));\n return shellFile.filter(link => !rels.has(link.rel.toLowerCase()));\n}\n\n/** @codexApi {\"parent\":\"pithy.ssr.build.shell-head\",\"name\":\"resolvePageHead\",\"stability\":\"experimental\",\"signature\":\"(shellFile: Partial<HeadState>, shell: Partial<HeadState>, page: Partial<HeadState>) => HeadState\"} */\n/**\n * Layer a page's head over the app shell's, over the shell file's.\n *\n * Precedence for the title is page, then app shell, then shell file — so a\n * route that names itself still wins, and one that names nothing inherits\n * rather than shipping untitled. Meta follows the same precedence via\n * `mergeHead`; links the shell file carries yield per relation, since two\n * `<link rel=\"icon\">` lets the shell's beat the page's on rules the page\n * cannot see.\n *\n * @param shellFile - Head values read from the app's HTML shell.\n * @param shell - Head state from the App shell render.\n * @param page - Head state from the page render.\n * @returns The merged head state.\n */\nexport function resolvePageHead(\n shellFile: Partial<HeadState>,\n shell: Partial<HeadState>,\n page: Partial<HeadState>\n): HeadState {\n const claimed = [...(shell.link ?? []), ...(page.link ?? [])];\n const carried = {\n ...shellFile,\n link: withoutClaimedRels(shellFile.link ?? [], claimed),\n };\n return mergeHead(mergeHead(carried, shell), page);\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.build.sprite-ssg",
|
|
4
|
+
"title": "SSG Icon-Sprite Bridge",
|
|
5
|
+
"category": "feature",
|
|
6
|
+
"risk": "high"
|
|
7
|
+
}
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Make the app's icon sprite available to a static SSG render.
|
|
11
|
+
*
|
|
12
|
+
* Apps register their sprite from `src/main.ts`
|
|
13
|
+
* (`registerIconSprite(SPRITE_URL, ICON_NAMES, ICON_META)`), and `main.ts` never
|
|
14
|
+
* executes server-side — so during SSG the icon registry was empty, every
|
|
15
|
+
* sprite icon fell through to the dashed-circle "?" placeholder, and the output
|
|
16
|
+
* did not even contain a `<use href>` to repair. Fixing static icons therefore
|
|
17
|
+
* takes two steps, not one: register the sprite so components emit references,
|
|
18
|
+
* then inline the referenced symbols so those references resolve.
|
|
19
|
+
*
|
|
20
|
+
* Everything is reached through the live `ViteDevServer` rather than imported.
|
|
21
|
+
* `@pithyjs/ssr` deliberately does not depend on `@pithyjs/ui-kit`, and
|
|
22
|
+
* `ssrLoadModule` hands back the *same* module instance the rendered components
|
|
23
|
+
* resolve — which is the only reason writing to that singleton has any effect.
|
|
24
|
+
*/
|
|
25
|
+
import { type SpriteBundle } from '@pithyjs/shared/sprite';
|
|
26
|
+
/** The slice of `ViteDevServer` this bridge needs — keeps it testable without Vite. */
|
|
27
|
+
/** @codexApi {"parent":"pithy.ssr.build.sprite-ssg","name":"SpriteModuleLoader","stability":"experimental","signature":"interface SpriteModuleLoader"} */
|
|
28
|
+
export interface SpriteModuleLoader {
|
|
29
|
+
ssrLoadModule: (id: string) => Promise<Record<string, unknown>>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Register the app's sprite for server-side rendering and parse its symbols.
|
|
33
|
+
*
|
|
34
|
+
* Safe to call for any app: one without the `pithyIcons()` plugin, without the
|
|
35
|
+
* UI Kit, or with an empty icons directory gets the empty bundle and no side
|
|
36
|
+
* effects.
|
|
37
|
+
*
|
|
38
|
+
* The registry write is a **process-lifetime side effect** — pair every call
|
|
39
|
+
* with {@link clearSpriteForSsg} in a `finally`.
|
|
40
|
+
*
|
|
41
|
+
* @param vite - The live SSG Vite server (or anything with `ssrLoadModule`).
|
|
42
|
+
* @returns A {@link SpriteBundle} carrying the sprite's symbols and its URL key.
|
|
43
|
+
*/
|
|
44
|
+
/** @codexApi {"parent":"pithy.ssr.build.sprite-ssg","name":"loadSpriteForSsg","stability":"experimental","signature":"(vite: SpriteModuleLoader) => Promise<SpriteBundle>","description":"Loads virtual:pithy-icons through the live Vite server, registers it into the UI Kit icon registry so SSR-rendered Icons emit <use href>, and returns the sprite's parsed <symbol> entries plus its URL key for inlining."} */
|
|
45
|
+
export declare function loadSpriteForSsg(vite: SpriteModuleLoader): Promise<SpriteBundle>;
|
|
46
|
+
/**
|
|
47
|
+
* Undo {@link loadSpriteForSsg}'s registry write.
|
|
48
|
+
*
|
|
49
|
+
* The icon registry is documented as client-only module-singleton state. A
|
|
50
|
+
* build-time SSG run is a legitimate exception — one process, one app, no
|
|
51
|
+
* concurrent requests — but leaving the singleton populated after the build
|
|
52
|
+
* would leak into anything else that shares the process (a watch-mode rebuild,
|
|
53
|
+
* a test run, an embedder calling `buildSSG()` twice for different apps).
|
|
54
|
+
*
|
|
55
|
+
* Safe to call unconditionally; a no-op when nothing was registered.
|
|
56
|
+
*
|
|
57
|
+
* @param vite - The same loader passed to {@link loadSpriteForSsg}.
|
|
58
|
+
*/
|
|
59
|
+
/** @codexApi {"parent":"pithy.ssr.build.sprite-ssg","name":"clearSpriteForSsg","stability":"experimental","signature":"(vite: SpriteModuleLoader) => Promise<void>"} */
|
|
60
|
+
export declare function clearSpriteForSsg(vite: SpriteModuleLoader): Promise<void>;
|
|
61
|
+
//# sourceMappingURL=sprite-ssg.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sprite-ssg.d.ts","sourceRoot":"","sources":["../../src/build/sprite-ssg.ts"],"names":[],"mappings":"AAAA;;;;;;;EAOE;AAEF;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,wBAAwB,CAAC;AAEhC,uFAAuF;AACvF,0JAA0J;AAC1J,MAAM,WAAW,kBAAkB;IACjC,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CACjE;AAuBD;;;;;;;;;;;;GAYG;AACH,yZAAyZ;AACzZ,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,YAAY,CAAC,CAmDvB;AAED;;;;;;;;;;;;GAYG;AACH,wKAAwK;AACxK,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,IAAI,CAAC,CAIf"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.build.sprite-ssg",
|
|
4
|
+
"title": "SSG Icon-Sprite Bridge",
|
|
5
|
+
"category": "feature",
|
|
6
|
+
"risk": "high"
|
|
7
|
+
}
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Make the app's icon sprite available to a static SSG render.
|
|
11
|
+
*
|
|
12
|
+
* Apps register their sprite from `src/main.ts`
|
|
13
|
+
* (`registerIconSprite(SPRITE_URL, ICON_NAMES, ICON_META)`), and `main.ts` never
|
|
14
|
+
* executes server-side — so during SSG the icon registry was empty, every
|
|
15
|
+
* sprite icon fell through to the dashed-circle "?" placeholder, and the output
|
|
16
|
+
* did not even contain a `<use href>` to repair. Fixing static icons therefore
|
|
17
|
+
* takes two steps, not one: register the sprite so components emit references,
|
|
18
|
+
* then inline the referenced symbols so those references resolve.
|
|
19
|
+
*
|
|
20
|
+
* Everything is reached through the live `ViteDevServer` rather than imported.
|
|
21
|
+
* `@pithyjs/ssr` deliberately does not depend on `@pithyjs/ui-kit`, and
|
|
22
|
+
* `ssrLoadModule` hands back the *same* module instance the rendered components
|
|
23
|
+
* resolve — which is the only reason writing to that singleton has any effect.
|
|
24
|
+
*/
|
|
25
|
+
import { createSpriteBundle, EMPTY_SPRITE_BUNDLE, } from '@pithyjs/shared/sprite';
|
|
26
|
+
/** Virtual module the `pithyIcons()` plugin serves the sprite URL + names from. */
|
|
27
|
+
const ICONS_MODULE_ID = 'virtual:pithy-icons';
|
|
28
|
+
/** Virtual module the `pithyIcons()` plugin serves the raw sprite text from. */
|
|
29
|
+
const SPRITE_CONTENT_MODULE_ID = 'virtual:pithy-icons/sprite';
|
|
30
|
+
/** Subpath entry for the UI Kit's client-side icon registry. */
|
|
31
|
+
const ICON_REGISTRY_MODULE_ID = '@pithyjs/ui-kit/icons';
|
|
32
|
+
async function tryLoad(vite, id) {
|
|
33
|
+
try {
|
|
34
|
+
return await vite.ssrLoadModule(id);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// Absent by design for apps with no `pithyIcons()` plugin or no UI Kit.
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Register the app's sprite for server-side rendering and parse its symbols.
|
|
43
|
+
*
|
|
44
|
+
* Safe to call for any app: one without the `pithyIcons()` plugin, without the
|
|
45
|
+
* UI Kit, or with an empty icons directory gets the empty bundle and no side
|
|
46
|
+
* effects.
|
|
47
|
+
*
|
|
48
|
+
* The registry write is a **process-lifetime side effect** — pair every call
|
|
49
|
+
* with {@link clearSpriteForSsg} in a `finally`.
|
|
50
|
+
*
|
|
51
|
+
* @param vite - The live SSG Vite server (or anything with `ssrLoadModule`).
|
|
52
|
+
* @returns A {@link SpriteBundle} carrying the sprite's symbols and its URL key.
|
|
53
|
+
*/
|
|
54
|
+
/** @codexApi {"parent":"pithy.ssr.build.sprite-ssg","name":"loadSpriteForSsg","stability":"experimental","signature":"(vite: SpriteModuleLoader) => Promise<SpriteBundle>","description":"Loads virtual:pithy-icons through the live Vite server, registers it into the UI Kit icon registry so SSR-rendered Icons emit <use href>, and returns the sprite's parsed <symbol> entries plus its URL key for inlining."} */
|
|
55
|
+
export async function loadSpriteForSsg(vite) {
|
|
56
|
+
const iconsMod = await tryLoad(vite, ICONS_MODULE_ID);
|
|
57
|
+
if (!iconsMod)
|
|
58
|
+
return EMPTY_SPRITE_BUNDLE;
|
|
59
|
+
const spriteUrl = typeof iconsMod.SPRITE_URL === 'string' ? iconsMod.SPRITE_URL : '';
|
|
60
|
+
const names = Array.isArray(iconsMod.ICON_NAMES)
|
|
61
|
+
? iconsMod.ICON_NAMES
|
|
62
|
+
: [];
|
|
63
|
+
const meta = (iconsMod.ICON_META ?? {});
|
|
64
|
+
if (!spriteUrl || names.length === 0)
|
|
65
|
+
return EMPTY_SPRITE_BUNDLE;
|
|
66
|
+
// Load and parse the sprite BEFORE registering it.
|
|
67
|
+
//
|
|
68
|
+
// Registering is what makes SSR emit `<use href="SPRITE_URL#name">`. If the
|
|
69
|
+
// sprite text is missing or unparseable, those references point at a file
|
|
70
|
+
// static output does not ship and nothing can inline them — empty icons, no
|
|
71
|
+
// error, a successful build. Registration is therefore conditional on having
|
|
72
|
+
// symbols to inline with: an app whose sprite fails to load renders the
|
|
73
|
+
// built-in inline icons or the visible placeholder instead, which is a
|
|
74
|
+
// legible failure rather than a silent one.
|
|
75
|
+
const spriteMod = await tryLoad(vite, SPRITE_CONTENT_MODULE_ID);
|
|
76
|
+
const text = typeof spriteMod?.SPRITE_CONTENT === 'string'
|
|
77
|
+
? spriteMod.SPRITE_CONTENT
|
|
78
|
+
: '';
|
|
79
|
+
const bundle = createSpriteBundle([{ url: spriteUrl, text }]);
|
|
80
|
+
if (bundle.symbols.size === 0) {
|
|
81
|
+
console.log(` Warning: icon sprite "${spriteUrl}" declares ${names.length} icon(s) ` +
|
|
82
|
+
`but no <symbol> could be parsed from it.\n` +
|
|
83
|
+
` Sprite icons will fall back to UI Kit built-ins or the missing-icon ` +
|
|
84
|
+
`placeholder in static output.`);
|
|
85
|
+
return EMPTY_SPRITE_BUNDLE;
|
|
86
|
+
}
|
|
87
|
+
const registry = await tryLoad(vite, ICON_REGISTRY_MODULE_ID);
|
|
88
|
+
const register = registry?.registerIconSprite;
|
|
89
|
+
if (typeof register === 'function') {
|
|
90
|
+
register(spriteUrl, names, meta);
|
|
91
|
+
}
|
|
92
|
+
return bundle;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Undo {@link loadSpriteForSsg}'s registry write.
|
|
96
|
+
*
|
|
97
|
+
* The icon registry is documented as client-only module-singleton state. A
|
|
98
|
+
* build-time SSG run is a legitimate exception — one process, one app, no
|
|
99
|
+
* concurrent requests — but leaving the singleton populated after the build
|
|
100
|
+
* would leak into anything else that shares the process (a watch-mode rebuild,
|
|
101
|
+
* a test run, an embedder calling `buildSSG()` twice for different apps).
|
|
102
|
+
*
|
|
103
|
+
* Safe to call unconditionally; a no-op when nothing was registered.
|
|
104
|
+
*
|
|
105
|
+
* @param vite - The same loader passed to {@link loadSpriteForSsg}.
|
|
106
|
+
*/
|
|
107
|
+
/** @codexApi {"parent":"pithy.ssr.build.sprite-ssg","name":"clearSpriteForSsg","stability":"experimental","signature":"(vite: SpriteModuleLoader) => Promise<void>"} */
|
|
108
|
+
export async function clearSpriteForSsg(vite) {
|
|
109
|
+
const registry = await tryLoad(vite, ICON_REGISTRY_MODULE_ID);
|
|
110
|
+
const clear = registry?.clearIconRegistry;
|
|
111
|
+
if (typeof clear === 'function')
|
|
112
|
+
clear();
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=sprite-ssg.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sprite-ssg.js","sourceRoot":"","sources":["../../src/build/sprite-ssg.ts"],"names":[],"mappings":"AAAA;;;;;;;EAOE;AAEF;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EACL,kBAAkB,EAClB,mBAAmB,GAEpB,MAAM,wBAAwB,CAAC;AAQhC,mFAAmF;AACnF,MAAM,eAAe,GAAG,qBAAq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AAE,cAAc,KAAK,QAAQ;QAC3C,CAAC,CAAC,SAAS,CAAC,cAAc;QAC1B,CAAC,CAAC,EAAE,CAAC;IAET,MAAM,MAAM,GAAG,kBAAkB,CAAC,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAE9D,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QAC9B,OAAO,CAAC,GAAG,CACT,2BAA2B,SAAS,cAAc,KAAK,CAAC,MAAM,WAAW;YACvE,4CAA4C;YAC5C,wEAAwE;YACxE,+BAA+B,CAClC,CAAC;QACF,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,uBAAuB,CAAC,CAAC;IAC9D,MAAM,QAAQ,GAAG,QAAQ,EAAE,kBAAkB,CAAC;IAC9C,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QAClC,QAA0D,CACzD,SAAS,EACT,KAAK,EACL,IAAI,CACL,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,wKAAwK;AACxK,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAwB;IAExB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,uBAAuB,CAAC,CAAC;IAC9D,MAAM,KAAK,GAAG,QAAQ,EAAE,iBAAiB,CAAC;IAC1C,IAAI,OAAO,KAAK,KAAK,UAAU;QAAG,KAAoB,EAAE,CAAC;AAC3D,CAAC","sourcesContent":["/** @codex\n{\n \"id\": \"pithy.ssr.build.sprite-ssg\",\n \"title\": \"SSG Icon-Sprite Bridge\",\n \"category\": \"feature\",\n \"risk\": \"high\"\n}\n*/\n\n/**\n * Make the app's icon sprite available to a static SSG render.\n *\n * Apps register their sprite from `src/main.ts`\n * (`registerIconSprite(SPRITE_URL, ICON_NAMES, ICON_META)`), and `main.ts` never\n * executes server-side — so during SSG the icon registry was empty, every\n * sprite icon fell through to the dashed-circle \"?\" placeholder, and the output\n * did not even contain a `<use href>` to repair. Fixing static icons therefore\n * takes two steps, not one: register the sprite so components emit references,\n * then inline the referenced symbols so those references resolve.\n *\n * Everything is reached through the live `ViteDevServer` rather than imported.\n * `@pithyjs/ssr` deliberately does not depend on `@pithyjs/ui-kit`, and\n * `ssrLoadModule` hands back the *same* module instance the rendered components\n * resolve — which is the only reason writing to that singleton has any effect.\n */\n\nimport {\n createSpriteBundle,\n EMPTY_SPRITE_BUNDLE,\n type SpriteBundle,\n} from '@pithyjs/shared/sprite';\n\n/** The slice of `ViteDevServer` this bridge needs — keeps it testable without Vite. */\n/** @codexApi {\"parent\":\"pithy.ssr.build.sprite-ssg\",\"name\":\"SpriteModuleLoader\",\"stability\":\"experimental\",\"signature\":\"interface SpriteModuleLoader\"} */\nexport interface SpriteModuleLoader {\n ssrLoadModule: (id: string) => Promise<Record<string, unknown>>;\n}\n\n/** Virtual module the `pithyIcons()` plugin serves the sprite URL + names from. */\nconst ICONS_MODULE_ID = 'virtual:pithy-icons';\n\n/** Virtual module the `pithyIcons()` plugin serves the raw sprite text from. */\nconst SPRITE_CONTENT_MODULE_ID = 'virtual:pithy-icons/sprite';\n\n/** Subpath entry for the UI Kit's client-side icon registry. */\nconst ICON_REGISTRY_MODULE_ID = '@pithyjs/ui-kit/icons';\n\nasync function tryLoad(\n vite: SpriteModuleLoader,\n id: string\n): Promise<Record<string, unknown> | null> {\n try {\n return await vite.ssrLoadModule(id);\n } catch {\n // Absent by design for apps with no `pithyIcons()` plugin or no UI Kit.\n return null;\n }\n}\n\n/**\n * Register the app's sprite for server-side rendering and parse its symbols.\n *\n * Safe to call for any app: one without the `pithyIcons()` plugin, without the\n * UI Kit, or with an empty icons directory gets the empty bundle and no side\n * effects.\n *\n * The registry write is a **process-lifetime side effect** — pair every call\n * with {@link clearSpriteForSsg} in a `finally`.\n *\n * @param vite - The live SSG Vite server (or anything with `ssrLoadModule`).\n * @returns A {@link SpriteBundle} carrying the sprite's symbols and its URL key.\n */\n/** @codexApi {\"parent\":\"pithy.ssr.build.sprite-ssg\",\"name\":\"loadSpriteForSsg\",\"stability\":\"experimental\",\"signature\":\"(vite: SpriteModuleLoader) => Promise<SpriteBundle>\",\"description\":\"Loads virtual:pithy-icons through the live Vite server, registers it into the UI Kit icon registry so SSR-rendered Icons emit <use href>, and returns the sprite's parsed <symbol> entries plus its URL key for inlining.\"} */\nexport async function loadSpriteForSsg(\n vite: SpriteModuleLoader\n): Promise<SpriteBundle> {\n const iconsMod = await tryLoad(vite, ICONS_MODULE_ID);\n if (!iconsMod) return EMPTY_SPRITE_BUNDLE;\n\n const spriteUrl =\n typeof iconsMod.SPRITE_URL === 'string' ? iconsMod.SPRITE_URL : '';\n const names = Array.isArray(iconsMod.ICON_NAMES)\n ? (iconsMod.ICON_NAMES as string[])\n : [];\n const meta = (iconsMod.ICON_META ?? {}) as Record<string, unknown>;\n\n if (!spriteUrl || names.length === 0) return EMPTY_SPRITE_BUNDLE;\n\n // Load and parse the sprite BEFORE registering it.\n //\n // Registering is what makes SSR emit `<use href=\"SPRITE_URL#name\">`. If the\n // sprite text is missing or unparseable, those references point at a file\n // static output does not ship and nothing can inline them — empty icons, no\n // error, a successful build. Registration is therefore conditional on having\n // symbols to inline with: an app whose sprite fails to load renders the\n // built-in inline icons or the visible placeholder instead, which is a\n // legible failure rather than a silent one.\n const spriteMod = await tryLoad(vite, SPRITE_CONTENT_MODULE_ID);\n const text =\n typeof spriteMod?.SPRITE_CONTENT === 'string'\n ? spriteMod.SPRITE_CONTENT\n : '';\n\n const bundle = createSpriteBundle([{ url: spriteUrl, text }]);\n\n if (bundle.symbols.size === 0) {\n console.log(\n ` Warning: icon sprite \"${spriteUrl}\" declares ${names.length} icon(s) ` +\n `but no <symbol> could be parsed from it.\\n` +\n ` Sprite icons will fall back to UI Kit built-ins or the missing-icon ` +\n `placeholder in static output.`\n );\n return EMPTY_SPRITE_BUNDLE;\n }\n\n const registry = await tryLoad(vite, ICON_REGISTRY_MODULE_ID);\n const register = registry?.registerIconSprite;\n if (typeof register === 'function') {\n (register as (u: string, n: string[], m?: unknown) => void)(\n spriteUrl,\n names,\n meta\n );\n }\n\n return bundle;\n}\n\n/**\n * Undo {@link loadSpriteForSsg}'s registry write.\n *\n * The icon registry is documented as client-only module-singleton state. A\n * build-time SSG run is a legitimate exception — one process, one app, no\n * concurrent requests — but leaving the singleton populated after the build\n * would leak into anything else that shares the process (a watch-mode rebuild,\n * a test run, an embedder calling `buildSSG()` twice for different apps).\n *\n * Safe to call unconditionally; a no-op when nothing was registered.\n *\n * @param vite - The same loader passed to {@link loadSpriteForSsg}.\n */\n/** @codexApi {\"parent\":\"pithy.ssr.build.sprite-ssg\",\"name\":\"clearSpriteForSsg\",\"stability\":\"experimental\",\"signature\":\"(vite: SpriteModuleLoader) => Promise<void>\"} */\nexport async function clearSpriteForSsg(\n vite: SpriteModuleLoader\n): Promise<void> {\n const registry = await tryLoad(vite, ICON_REGISTRY_MODULE_ID);\n const clear = registry?.clearIconRegistry;\n if (typeof clear === 'function') (clear as () => void)();\n}\n"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.ssr.build.ssg",
|
|
4
|
+
"title": "Static Site Generation Pipeline",
|
|
5
|
+
"category": "runtime"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
import type { RouteNode } from '@pithyjs/router';
|
|
9
|
+
import type { Component } from '@pithyjs/core';
|
|
10
|
+
import type { SSGResult } from '../shared/types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Options for static site generation
|
|
13
|
+
*/
|
|
14
|
+
export interface GenerateStaticSiteOptions {
|
|
15
|
+
/** Output directory for generated HTML files */
|
|
16
|
+
outDir: string;
|
|
17
|
+
/** Root component to render */
|
|
18
|
+
App: Component;
|
|
19
|
+
/** Explicit paths to pre-render (takes priority over auto-detection) */
|
|
20
|
+
paths?: string[];
|
|
21
|
+
/** Route tree for auto-detecting static paths (used when paths not provided) */
|
|
22
|
+
routes?: RouteNode[];
|
|
23
|
+
/** Base URL to prepend to asset paths (e.g., '/my-app') */
|
|
24
|
+
base?: string;
|
|
25
|
+
/** Script URLs to include in generated HTML */
|
|
26
|
+
scripts?: string[];
|
|
27
|
+
/** Stylesheet URLs to include in generated HTML */
|
|
28
|
+
styles?: string[];
|
|
29
|
+
/** Callback for each successfully rendered page */
|
|
30
|
+
onPage?: (path: string, html: string) => void;
|
|
31
|
+
/** Callback on render error */
|
|
32
|
+
onError?: (path: string, error: Error) => void;
|
|
33
|
+
}
|
|
34
|
+
/** @codexApi {"parent":"pithy.ssr.build.ssg","name":"collectStaticPaths","stability":"internal","signature":"(routes: RouteNode[]) => Promise<string[]>"} */
|
|
35
|
+
/**
|
|
36
|
+
* Collect paths from static routes for pre-rendering.
|
|
37
|
+
*
|
|
38
|
+
* Walks the route tree and collects paths for routes that:
|
|
39
|
+
* - Are not dynamic (no :param segments)
|
|
40
|
+
* - Are not catch-all (no * or [...] segments)
|
|
41
|
+
* - Have a page import
|
|
42
|
+
* - Do not have prerender=false in their config
|
|
43
|
+
*
|
|
44
|
+
* @param routes - Route node tree
|
|
45
|
+
* @returns Array of static paths to pre-render
|
|
46
|
+
*/
|
|
47
|
+
export declare function collectStaticPaths(routes: RouteNode[]): Promise<string[]>;
|
|
48
|
+
/** @codexApi {"parent":"pithy.ssr.build.ssg","name":"findRootCatchAllRoute","stability":"internal","signature":"(routes: RouteNode[]) => RouteNode | undefined"} */
|
|
49
|
+
/**
|
|
50
|
+
* Find the top-level catch-all route that backs the site-wide `404.html`.
|
|
51
|
+
*
|
|
52
|
+
* Netlify, Vercel, GitHub Pages and Cloudflare Pages serve a root `404.html`
|
|
53
|
+
* for an unmatched path with no per-host configuration; S3 needs
|
|
54
|
+
* `ErrorDocument: 404.html` on its static-website endpoint. So rendering
|
|
55
|
+
* the app's own catch-all into that file is the one framework-side answer that
|
|
56
|
+
* works everywhere. Emission is automatic when a top-level catch-all exists —
|
|
57
|
+
* there is no config key to set.
|
|
58
|
+
*
|
|
59
|
+
* Semantics, all deliberate:
|
|
60
|
+
*
|
|
61
|
+
* - **Only a TOP-LEVEL catch-all qualifies.** A nested one (`/admin/*`) answers
|
|
62
|
+
* only for URLs beneath its own prefix, so it cannot stand in as the
|
|
63
|
+
* site-wide not-found document and is ignored.
|
|
64
|
+
* - **No catch-all at all is a complete no-op.** The caller writes no file and
|
|
65
|
+
* reports no failure; a site without a not-found page is a valid site.
|
|
66
|
+
* - **More than one top-level catch-all** cannot arise from the file scanner,
|
|
67
|
+
* but this takes an arbitrary array. The FIRST match in depth-first traversal
|
|
68
|
+
* order wins, deterministically. It does not throw — an ambiguous route tree
|
|
69
|
+
* is not a reason to fail an otherwise complete export.
|
|
70
|
+
*
|
|
71
|
+
* Children ARE walked, and that is load-bearing rather than defensive: an app
|
|
72
|
+
* with a root `+layout` nests its catch-all under that layout node, and because
|
|
73
|
+
* patterns are absolute the child's pattern is still `/*`. The pattern check is
|
|
74
|
+
* what confines the result to the site-wide catch-all — a nested `/admin/*`
|
|
75
|
+
* fails it at any depth — so recursion cannot promote a nested catch-all.
|
|
76
|
+
*
|
|
77
|
+
* @param routes - Route node tree.
|
|
78
|
+
* @returns The top-level catch-all node, or `undefined` if the app has none.
|
|
79
|
+
*/
|
|
80
|
+
export declare function findRootCatchAllRoute(routes: RouteNode[]): RouteNode | undefined;
|
|
81
|
+
export { pathToFilePath } from '../shared/shell-helpers.js';
|
|
82
|
+
/** @codexApi {"parent":"pithy.ssr.build.ssg","name":"generateStaticSite","stability":"experimental","signature":"(options: GenerateStaticSiteOptions) => Promise<SSGResult>"} */
|
|
83
|
+
/**
|
|
84
|
+
* Generate a static site by pre-rendering routes to HTML files.
|
|
85
|
+
*
|
|
86
|
+
* This function:
|
|
87
|
+
* 1. Collects paths to render (from explicit list or auto-detected from routes)
|
|
88
|
+
* 2. Renders each path using renderToString
|
|
89
|
+
* 3. Wraps in htmlShell for complete HTML documents
|
|
90
|
+
* 4. Writes files to the output directory
|
|
91
|
+
*
|
|
92
|
+
* @param options - SSG options
|
|
93
|
+
* @returns Build result with rendered/failed paths and timing
|
|
94
|
+
*
|
|
95
|
+
* @example Basic usage with explicit paths
|
|
96
|
+
* ```ts
|
|
97
|
+
* const result = await generateStaticSite({
|
|
98
|
+
* outDir: './dist',
|
|
99
|
+
* App: MyApp,
|
|
100
|
+
* paths: ['/', '/about', '/blog/post-1'],
|
|
101
|
+
* scripts: ['/entry-client.js'],
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* console.log(`Rendered ${result.rendered.length} pages`);
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @example Auto-detect from route tree
|
|
108
|
+
* ```ts
|
|
109
|
+
* const result = await generateStaticSite({
|
|
110
|
+
* outDir: './dist',
|
|
111
|
+
* App: MyApp,
|
|
112
|
+
* routes: myRouteTree,
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export declare function generateStaticSite(options: GenerateStaticSiteOptions): Promise<SSGResult>;
|
|
117
|
+
//# sourceMappingURL=ssg.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ssg.d.ts","sourceRoot":"","sources":["../../src/build/ssg.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAWF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAgB,MAAM,oBAAoB,CAAC;AAWlE;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;IACf,+BAA+B;IAC/B,GAAG,EAAE,SAAS,CAAC;IACf,wEAAwE;IACxE,KAAK,CAAC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