@pithyjs/ssr 0.0.0-stage → 0.1.2-beta.8
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,704 @@
|
|
|
1
|
+
/** @codex { "id": "pithy.ssr.build.css-live-props", "title": "Static Export Dead Custom-Property Purge", "category": "feature", "risk": "high" } */
|
|
2
|
+
/**
|
|
3
|
+
* Drops custom-property declarations nothing live references, from the CSS that
|
|
4
|
+
* survived the rule-level purge in `css-purge.ts`.
|
|
5
|
+
*
|
|
6
|
+
* It reads RESOLVED CSS, never SCSS source: by this point the `@use` graph,
|
|
7
|
+
* `#{}` interpolation and package aliases are gone, so there is nothing left to
|
|
8
|
+
* resolve and no reference a scanner could miss. Do not reintroduce source
|
|
9
|
+
* scanning here — see `docs/ssg/static-export-unification.md` Step 6.
|
|
10
|
+
*
|
|
11
|
+
* Liveness is TRANSITIVE and per NAME: `--a: var(--b)` makes `--b` live only
|
|
12
|
+
* when `--a` is, and a name defined in both `:root` and `[data-theme=dark]`
|
|
13
|
+
* contributes refs from every definition. Iterated to a fixed point — one pass
|
|
14
|
+
* keeps a chain's first hop and drops the rest.
|
|
15
|
+
*
|
|
16
|
+
* Output is produced by SPLICING drop ranges out of the input, never by
|
|
17
|
+
* re-emitting. A sheet with nothing dead comes back byte-identical, so
|
|
18
|
+
* `oklch()` and `color-mix()` cannot be reformatted away.
|
|
19
|
+
*
|
|
20
|
+
* Fail-open end to end: an unparseable name, an unterminated block, an opaque
|
|
21
|
+
* at-rule and anything else it cannot classify are KEPT. Keeping a declaration
|
|
22
|
+
* costs bytes; dropping a live one renders a JS-disabled page wrong.
|
|
23
|
+
*/
|
|
24
|
+
import { atRuleName, holdsCascadeSlot, isRecursiveAtRule, } from '../shared/css-purge.js';
|
|
25
|
+
import { splitFirstRule, skipComment, skipString, } from './css-purge.js';
|
|
26
|
+
/**
|
|
27
|
+
* Names kept however the CSS reads, because JavaScript composes them from a
|
|
28
|
+
* string at runtime and no scan of any kind can see the reference.
|
|
29
|
+
*
|
|
30
|
+
* Static export ships no framework JS, but it does still carry whatever
|
|
31
|
+
* `scripts` the caller configured, so these stay. Anything else a static page's
|
|
32
|
+
* own JS reads through `getComputedStyle` belongs in {@link DeadPropOptions}'s
|
|
33
|
+
* `safelist` — in particular `getDefaultTheme(true)`, which bootstraps a theme
|
|
34
|
+
* by READING `--color-*` off the sheet before anything has written it.
|
|
35
|
+
*/
|
|
36
|
+
/** @codexApi {"parent":"pithy.ssr.build.css-live-props","name":"DEFAULT_LIVE_PROP_SAFELIST","stability":"experimental","signature":"readonly string[]"} */
|
|
37
|
+
export const DEFAULT_LIVE_PROP_SAFELIST = [
|
|
38
|
+
// `ShadowManager.resolveTintColor()` reads `--color-${color}-rgb` for these
|
|
39
|
+
// three. Re-derived, not imported: `@pithyjs/ssr` has no dependency on
|
|
40
|
+
// `@pithyjs/vite-plugin`. Keep in step with `DEFAULT_CHANNEL_VAR_SAFELIST`,
|
|
41
|
+
// `packages/vitePlugins/src/designSystemPlugin/channelVars.ts:205`.
|
|
42
|
+
'--color-primary-rgb',
|
|
43
|
+
'--color-secondary-rgb',
|
|
44
|
+
'--color-tertiary-rgb',
|
|
45
|
+
// `packages/ui-kit/src/shared/headerOffset.ts:152`, a fixed literal.
|
|
46
|
+
'--app-header-offset',
|
|
47
|
+
];
|
|
48
|
+
/**
|
|
49
|
+
* Matches the same charset the declaration parser will accept as a name.
|
|
50
|
+
*
|
|
51
|
+
* Case-insensitive because `VAR(--x)` is a live reference — CSS function names
|
|
52
|
+
* are ASCII case-insensitive — and missing one DROPS the declaration. The
|
|
53
|
+
* capture stays case-sensitive: names are compared by exact string, as CSS
|
|
54
|
+
* requires.
|
|
55
|
+
*
|
|
56
|
+
* A comment is whitespace to the tokenizer, so a `var(` may be separated from
|
|
57
|
+
* its name by one. That case belongs to {@link addSpacedVarRefs}, not here: a
|
|
58
|
+
* `(?:comment\s*)*` group in this pattern backtracked exponentially when the
|
|
59
|
+
* tail failed, so a sheet with `var(` followed by comments and no name could
|
|
60
|
+
* hang the build (60 ms at 24 comments, and doubling per comment).
|
|
61
|
+
*/
|
|
62
|
+
const VAR_REF_RE = /var\(\s*(--[A-Za-z0-9_-]+)/gi;
|
|
63
|
+
/**
|
|
64
|
+
* Opens a `style()` query, whose whole argument {@link addStyleFnRefs} reads.
|
|
65
|
+
*
|
|
66
|
+
* The lookbehind keeps `getComputedStyle(` and prose out. An unclosed one of
|
|
67
|
+
* those runs to the end of the text, so it kept every name after it.
|
|
68
|
+
*/
|
|
69
|
+
const STYLE_FN_OPEN_RE = /(?<![\w-])style\(/gi;
|
|
70
|
+
/** Opens a `var()` reference, whose name {@link addSpacedVarRefs} reads. */
|
|
71
|
+
const VAR_OPEN_RE = /var\(/gi;
|
|
72
|
+
/** Reads a name at an exact offset, for a scanner that already found it. */
|
|
73
|
+
const PROP_NAME_AT_RE = /--[A-Za-z0-9_-]+/y;
|
|
74
|
+
/**
|
|
75
|
+
* Index past the whitespace and comments at `i`, or `-1` when an unclosed
|
|
76
|
+
* comment means nothing after it is code.
|
|
77
|
+
*/
|
|
78
|
+
function skipTrivia(text, i) {
|
|
79
|
+
for (;;) {
|
|
80
|
+
while (i < text.length && /\s/.test(text[i]))
|
|
81
|
+
i++;
|
|
82
|
+
if (text[i] !== '/' || text[i + 1] !== '*')
|
|
83
|
+
return i;
|
|
84
|
+
const close = text.indexOf('*/', i + 2);
|
|
85
|
+
if (close === -1)
|
|
86
|
+
return -1;
|
|
87
|
+
i = close + 2;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Every name a comment separates from its `var(`, which a comment being
|
|
92
|
+
* whitespace to the tokenizer makes a real reference.
|
|
93
|
+
*
|
|
94
|
+
* Comments are read HERE, where a name is expected, rather than blanked out of
|
|
95
|
+
* the text first. Blanking was wrong about what it was reading: these sources
|
|
96
|
+
* carry HTML prose and CSS strings too, so a comment opener in `<pre>src/*` or
|
|
97
|
+
* in `content:"/*"` paired with the closer of a real reference further along
|
|
98
|
+
* and deleted the `var(` between them — dropping a definition that renders.
|
|
99
|
+
*/
|
|
100
|
+
function addSpacedVarRefs(text, into) {
|
|
101
|
+
VAR_OPEN_RE.lastIndex = 0;
|
|
102
|
+
for (;;) {
|
|
103
|
+
const m = VAR_OPEN_RE.exec(text);
|
|
104
|
+
if (m === null)
|
|
105
|
+
return;
|
|
106
|
+
const i = skipTrivia(text, m.index + m[0].length);
|
|
107
|
+
// Nothing closes a comment after this point, so no later `var(` can be
|
|
108
|
+
// separated from its name by one either; a bare name is the raw scan's.
|
|
109
|
+
if (i === -1)
|
|
110
|
+
return;
|
|
111
|
+
PROP_NAME_AT_RE.lastIndex = i;
|
|
112
|
+
const name = PROP_NAME_AT_RE.exec(text);
|
|
113
|
+
if (name !== null)
|
|
114
|
+
into.add(name[0]);
|
|
115
|
+
// Resume past what this scan consumed, so no span is read twice — which
|
|
116
|
+
// is what keeps a source full of comment openers linear.
|
|
117
|
+
VAR_OPEN_RE.lastIndex = Math.max(VAR_OPEN_RE.lastIndex, i);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A name this pass is willing to decide about. Anything else — an escaped or
|
|
122
|
+
* non-ASCII name the `var()` extractor could not match either — is kept, so the
|
|
123
|
+
* two sides can never disagree about what a reference refers to.
|
|
124
|
+
*
|
|
125
|
+
* It answers "may this name be DELETED", which is not the same question as
|
|
126
|
+
* "is this a custom-property declaration" — see {@link isCustomPropDecl}.
|
|
127
|
+
*/
|
|
128
|
+
const PROP_NAME_RE = /^--[A-Za-z0-9_-]+$/;
|
|
129
|
+
/** Where `@container style(--x: y)` and `transition: --x 200ms` read a name bare. */
|
|
130
|
+
const BARE_PROP_RE = /--[A-Za-z0-9_-]+/g;
|
|
131
|
+
/**
|
|
132
|
+
* Properties whose VALUE names a registered custom property without `var()`.
|
|
133
|
+
* Harvested bare, which over-keeps — the safe direction.
|
|
134
|
+
*/
|
|
135
|
+
const BARE_REF_PROPS = new Set([
|
|
136
|
+
'transition',
|
|
137
|
+
'transition-property',
|
|
138
|
+
'animation',
|
|
139
|
+
'will-change',
|
|
140
|
+
]);
|
|
141
|
+
/**
|
|
142
|
+
* Every custom property named anywhere inside a `style()` query.
|
|
143
|
+
*
|
|
144
|
+
* A `style()` query reads a name with no `var()` around it, in a CONDITION
|
|
145
|
+
* rather than a value, and the same query reaches the pass three ways: a
|
|
146
|
+
* declaration value (`if(style(--v: a): …)`), an opaque at-rule kept whole
|
|
147
|
+
* (`@starting-style{@container style(--x: 1){…}}`) and a page's inline
|
|
148
|
+
* `<style>`. Only the recursive-at-rule prelude read it before.
|
|
149
|
+
*
|
|
150
|
+
* The WHOLE argument, not the name after the paren: a query may be compound or
|
|
151
|
+
* negated — `style((--a: 1) and (--b: 2))`, `style(not (--a: 1))` — and
|
|
152
|
+
* matching only `style(\s*--name` dropped every property such a condition
|
|
153
|
+
* tests, which then silently stops matching.
|
|
154
|
+
*
|
|
155
|
+
* Linear: `lastIndex` resumes past each closed group, so a nested `style(`
|
|
156
|
+
* already covered by the enclosing argument is not rescanned. An unterminated
|
|
157
|
+
* `style(` runs to the end of the text and is harvested, which over-keeps.
|
|
158
|
+
*/
|
|
159
|
+
function addStyleFnRefs(text, into) {
|
|
160
|
+
STYLE_FN_OPEN_RE.lastIndex = 0;
|
|
161
|
+
for (;;) {
|
|
162
|
+
const m = STYLE_FN_OPEN_RE.exec(text);
|
|
163
|
+
if (m === null)
|
|
164
|
+
return;
|
|
165
|
+
let depth = 0;
|
|
166
|
+
let i = m.index + m[0].length - 1;
|
|
167
|
+
const open = i;
|
|
168
|
+
for (; i < text.length; i++) {
|
|
169
|
+
const c = text[i];
|
|
170
|
+
// A `)` inside a string, a comment or behind a backslash closes nothing,
|
|
171
|
+
// and ending the argument early drops every name after it.
|
|
172
|
+
if (c === '\\')
|
|
173
|
+
i++;
|
|
174
|
+
else if (c === '"' || c === "'")
|
|
175
|
+
i = skipString(text, i, text.length);
|
|
176
|
+
else if (c === '/' && text[i + 1] === '*')
|
|
177
|
+
i = skipComment(text, i, text.length) - 1;
|
|
178
|
+
else if (c === '(')
|
|
179
|
+
depth++;
|
|
180
|
+
else if (c === ')' && --depth === 0)
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
for (const name of text.slice(open + 1, i).matchAll(BARE_PROP_RE)) {
|
|
184
|
+
into.add(name[0]);
|
|
185
|
+
}
|
|
186
|
+
STYLE_FN_OPEN_RE.lastIndex = i;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function addVarRefs(text, into) {
|
|
190
|
+
// The raw scan counts a reference inside a comment too
|
|
191
|
+
// (`color:red/*var(--half)*/`), a deliberate over-keep.
|
|
192
|
+
for (const m of text.matchAll(VAR_REF_RE))
|
|
193
|
+
into.add(m[1]);
|
|
194
|
+
addSpacedVarRefs(text, into);
|
|
195
|
+
addStyleFnRefs(text, into);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Whether the text before a declaration's colon names a custom property — ANY
|
|
199
|
+
* name, with comments and whitespace read as the trivia they are, so
|
|
200
|
+
* `--x/*c*\/:` and `--x /*c*\/ :` are declarations.
|
|
201
|
+
*
|
|
202
|
+
* Deliberately wider than {@link PROP_NAME_RE}, which answers which names may
|
|
203
|
+
* be deleted. Deciding declaration SYNTAX with that narrow matcher read
|
|
204
|
+
* `--café:{--inner:1}` as a nested RULE, recursed into it and removed a live
|
|
205
|
+
* definition — past the safelist, which never saw a declaration to protect.
|
|
206
|
+
*/
|
|
207
|
+
function isCustomPropDecl(text) {
|
|
208
|
+
// Only the first two characters decide, which is why trivia needs no
|
|
209
|
+
// handling here: a comment before the colon comes after the name, and the
|
|
210
|
+
// splitter never starts an item on leading trivia — that is its own item.
|
|
211
|
+
//
|
|
212
|
+
// Either hyphen may be ESCAPED, and CSS spells one several ways (`\--x`,
|
|
213
|
+
// `-\-x`, `\2d\2d x`). Rather than decode them, an escape in either
|
|
214
|
+
// position is read as "possibly a custom property". Being wrong that way
|
|
215
|
+
// keeps an escape-led nested rule (`\64 iv:hover{…}`) whole, which costs
|
|
216
|
+
// bytes; being wrong the other way recursed INTO a declaration's value and
|
|
217
|
+
// deleted the definition around it.
|
|
218
|
+
if (text.startsWith('\\'))
|
|
219
|
+
return true;
|
|
220
|
+
if (!text.startsWith('-'))
|
|
221
|
+
return false;
|
|
222
|
+
return text.startsWith('-', 1) || text.startsWith('\\', 1);
|
|
223
|
+
}
|
|
224
|
+
function addBareRefs(text, into) {
|
|
225
|
+
for (const m of text.matchAll(BARE_PROP_RE))
|
|
226
|
+
into.add(m[0]);
|
|
227
|
+
}
|
|
228
|
+
/** Advance past the block starting at `start`, as `splitRules` sees it. */
|
|
229
|
+
function blockAt(css, start, to) {
|
|
230
|
+
const node = splitFirstRule(css, start, to);
|
|
231
|
+
return node ? { node, end: start + node.raw.length } : null;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Split `css[from, to)` into declarations and nested blocks.
|
|
235
|
+
*
|
|
236
|
+
* Parens, strings and comments all hold the scan: `url(data:…;base64,…)`,
|
|
237
|
+
* `content: "a;b"` and `/* ; *\/` each carry a `;` that would otherwise end a
|
|
238
|
+
* declaration early and shift every name after it. A `{` before any `;` means
|
|
239
|
+
* the item is a nested rule, and `splitRules` is asked where it ends.
|
|
240
|
+
*/
|
|
241
|
+
function splitBody(css, from, to, scan) {
|
|
242
|
+
const items = [];
|
|
243
|
+
let i = from;
|
|
244
|
+
while (i < to) {
|
|
245
|
+
while (i < to && /\s/.test(css[i]))
|
|
246
|
+
i++;
|
|
247
|
+
if (i >= to)
|
|
248
|
+
break;
|
|
249
|
+
if (css[i] === '/' && css[i + 1] === '*') {
|
|
250
|
+
const stop = skipComment(css, i, to);
|
|
251
|
+
// A ref inside a comment is NOT a ref — a comment cannot render. Safe
|
|
252
|
+
// only because `skipComment` finds the real end; a comment misjudged as
|
|
253
|
+
// code would instead keep a dead name, which costs bytes, not render.
|
|
254
|
+
items.push({
|
|
255
|
+
kind: 'comment',
|
|
256
|
+
start: i,
|
|
257
|
+
end: stop,
|
|
258
|
+
pinned: css.startsWith('/*!', i),
|
|
259
|
+
});
|
|
260
|
+
i = stop;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const start = i;
|
|
264
|
+
// Closers still owed, innermost last. A `;` inside ANY block belongs to
|
|
265
|
+
// that block's value, not to this declaration: `--x:[a;b]` is one
|
|
266
|
+
// declaration, and reading its `;` as a separator spliced the text either
|
|
267
|
+
// side of it into different items — promoting `color:red` out of a dead
|
|
268
|
+
// custom property's value into a real declaration, and leaving an
|
|
269
|
+
// unmatched `[` that swallows the rules after it.
|
|
270
|
+
const owed = [];
|
|
271
|
+
let colonAt = -1;
|
|
272
|
+
let end = -1;
|
|
273
|
+
let isBlock = false;
|
|
274
|
+
let valueHasBlock = false;
|
|
275
|
+
for (; i < to; i++) {
|
|
276
|
+
const ch = css[i];
|
|
277
|
+
// An escape outside a string: `.w-\(` is one class name, not an open
|
|
278
|
+
// paren. Without this the paren count desynchronises and the rest of the
|
|
279
|
+
// sheet is swallowed into one verbatim item.
|
|
280
|
+
if (ch === '\\') {
|
|
281
|
+
i++;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (ch === '/' && css[i + 1] === '*') {
|
|
285
|
+
i = skipComment(css, i, to) - 1;
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (ch === '"' || ch === "'") {
|
|
289
|
+
i = skipString(css, i, to);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (ch === '(') {
|
|
293
|
+
owed.push(')');
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (ch === '[') {
|
|
297
|
+
owed.push(']');
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (owed.length > 0) {
|
|
301
|
+
if (ch === '{')
|
|
302
|
+
owed.push('}');
|
|
303
|
+
else if (ch === owed[owed.length - 1])
|
|
304
|
+
owed.pop();
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (ch === '{') {
|
|
308
|
+
// A custom property's VALUE may be a block (`--x:{--y:1}`, the old
|
|
309
|
+
// `@apply` idiom); anywhere else a `{` opens a nested RULE.
|
|
310
|
+
if (colonAt !== -1 && isCustomPropDecl(css.slice(start, colonAt))) {
|
|
311
|
+
owed.push('}');
|
|
312
|
+
valueHasBlock = true;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
isBlock = true;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
if (ch === '}') {
|
|
319
|
+
// Unbalanced input; `i === start` would otherwise never advance.
|
|
320
|
+
end = i === start ? i + 1 : i;
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
if (ch === ';') {
|
|
324
|
+
end = i + 1;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
if (ch === ':' && colonAt === -1)
|
|
328
|
+
colonAt = i;
|
|
329
|
+
}
|
|
330
|
+
if (isBlock) {
|
|
331
|
+
const found = blockAt(css, start, to);
|
|
332
|
+
// `splitRules` is paren-aware now, so it and this loop tokenise alike and
|
|
333
|
+
// `found.end <= i` can no longer happen — kept as a fail-open floor,
|
|
334
|
+
// because trusting a disagreeing end emitted an unterminated at-rule.
|
|
335
|
+
if (!found || found.end <= i) {
|
|
336
|
+
items.push({ kind: 'other', start, end: to });
|
|
337
|
+
addVarRefs(css.slice(start, to), scan.roots);
|
|
338
|
+
i = to;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
items.push(groupItem(css, start, found, scan));
|
|
342
|
+
i = found.end;
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const stop = end === -1 ? to : end;
|
|
346
|
+
const text = css.slice(start, stop);
|
|
347
|
+
const name = colonAt === -1 ? '' : css.slice(start, colonAt).trim();
|
|
348
|
+
// A leading `@` is a statement at-rule, not a declaration, whatever colons
|
|
349
|
+
// it carries (`@import url(a:b);`). A block-valued custom property and an
|
|
350
|
+
// unclosed block are kept verbatim: neither one's liveness is modelled, so
|
|
351
|
+
// dropping it on the strength of its NAME can take live text with it.
|
|
352
|
+
if (valueHasBlock || owed.length > 0) {
|
|
353
|
+
items.push({ kind: 'other', start, end: stop });
|
|
354
|
+
addVarRefs(text, scan.roots);
|
|
355
|
+
}
|
|
356
|
+
else if (text[0] !== '@' && PROP_NAME_RE.test(name)) {
|
|
357
|
+
items.push({ kind: 'prop', name, start, end: stop });
|
|
358
|
+
const refs = scan.edges.get(name) ?? new Set();
|
|
359
|
+
// Counts a `var()` in a comment EMBEDDED here, unlike a standalone
|
|
360
|
+
// comment item's: a half commented-out fallback is still a reference.
|
|
361
|
+
addVarRefs(css.slice(colonAt + 1, stop), refs);
|
|
362
|
+
scan.edges.set(name, refs);
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
items.push({ kind: 'other', start, end: stop });
|
|
366
|
+
addVarRefs(text, scan.roots);
|
|
367
|
+
// `transition: --x 200ms` and `will-change: --x` name a registered
|
|
368
|
+
// property with no `var()` around it.
|
|
369
|
+
if (BARE_REF_PROPS.has(name.toLowerCase())) {
|
|
370
|
+
addBareRefs(css.slice(colonAt + 1, stop), scan.roots);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
i = stop;
|
|
374
|
+
}
|
|
375
|
+
return items;
|
|
376
|
+
}
|
|
377
|
+
/** Classify a block: recurse into it, or keep it whole and harvest its refs. */
|
|
378
|
+
function groupItem(css, start, found, scan) {
|
|
379
|
+
const { node, end } = found;
|
|
380
|
+
if (node.kind !== 'block') {
|
|
381
|
+
addVarRefs(node.raw, scan.roots);
|
|
382
|
+
return { kind: 'other', start, end };
|
|
383
|
+
}
|
|
384
|
+
const at = atRuleName(node.prelude);
|
|
385
|
+
const base = {
|
|
386
|
+
kind: 'group',
|
|
387
|
+
start,
|
|
388
|
+
end,
|
|
389
|
+
bodyStart: node.bodyStart,
|
|
390
|
+
bodyEnd: node.bodyEnd,
|
|
391
|
+
};
|
|
392
|
+
if (at !== null && !isRecursiveAtRule(at)) {
|
|
393
|
+
// `@property`, `@keyframes`, `@font-face`, `@page`: descriptors and
|
|
394
|
+
// keyframe offsets, not declarations to decide about. Kept whole — an
|
|
395
|
+
// `@property` for a dropped name costs a few bytes and removes a
|
|
396
|
+
// correctness question — and every `var()` inside is a root.
|
|
397
|
+
addVarRefs(node.raw, scan.roots);
|
|
398
|
+
return { ...base, children: null };
|
|
399
|
+
}
|
|
400
|
+
addVarRefs(node.prelude, scan.roots);
|
|
401
|
+
if (at !== null) {
|
|
402
|
+
// `@supports (color: var(--x))` is covered above; `@container style(--x: y)`
|
|
403
|
+
// reads the name with no `var()` around it.
|
|
404
|
+
addBareRefs(node.prelude, scan.roots);
|
|
405
|
+
}
|
|
406
|
+
return {
|
|
407
|
+
...base,
|
|
408
|
+
// A NAMED `@layer{}` declares ORDER, so an emptied one is emptied rather
|
|
409
|
+
// than dropped: removing it moves the layer behind any later block that
|
|
410
|
+
// reopens the name. An anonymous one names nothing and holds no slot —
|
|
411
|
+
// the rule-level pass and Studio decide this with the same helper.
|
|
412
|
+
holdsSlot: holdsCascadeSlot(at, node.prelude),
|
|
413
|
+
children: splitBody(css, node.bodyStart, node.bodyEnd, scan),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/** Every name reachable from a root or the safelist, to a fixed point. */
|
|
417
|
+
function resolveLive(scan, inSafelist) {
|
|
418
|
+
const live = new Set();
|
|
419
|
+
const queue = [];
|
|
420
|
+
const mark = (name) => {
|
|
421
|
+
if (live.has(name))
|
|
422
|
+
return;
|
|
423
|
+
live.add(name);
|
|
424
|
+
queue.push(name);
|
|
425
|
+
};
|
|
426
|
+
for (const r of scan.roots)
|
|
427
|
+
mark(r);
|
|
428
|
+
for (const name of scan.edges.keys())
|
|
429
|
+
if (inSafelist(name))
|
|
430
|
+
mark(name);
|
|
431
|
+
while (queue.length > 0) {
|
|
432
|
+
const name = queue.pop();
|
|
433
|
+
for (const ref of scan.edges.get(name) ?? [])
|
|
434
|
+
mark(ref);
|
|
435
|
+
}
|
|
436
|
+
return live;
|
|
437
|
+
}
|
|
438
|
+
/** `range` minus `holes`, so a dropped rule can leave a `/*!` comment behind. */
|
|
439
|
+
function carve(range, holes) {
|
|
440
|
+
if (holes.length === 0)
|
|
441
|
+
return [range];
|
|
442
|
+
const out = [];
|
|
443
|
+
let at = range.start;
|
|
444
|
+
for (const h of [...holes].sort((a, b) => a.start - b.start)) {
|
|
445
|
+
if (h.start > at)
|
|
446
|
+
out.push({ start: at, end: h.start });
|
|
447
|
+
at = Math.max(at, h.end);
|
|
448
|
+
}
|
|
449
|
+
if (at < range.end)
|
|
450
|
+
out.push({ start: at, end: range.end });
|
|
451
|
+
return out;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Collect the ranges to splice out of `css[bodyStart, bodyEnd)`.
|
|
455
|
+
*
|
|
456
|
+
* `gone` means every item here can go, which lets the CALLER drop the whole rule
|
|
457
|
+
* rather than leave `:root{}` or a dangling `@media{}` behind; the local ranges
|
|
458
|
+
* are then discarded, since the caller's range covers them. `keep` carries the
|
|
459
|
+
* `/*!` comments the caller must carve out of that range.
|
|
460
|
+
*
|
|
461
|
+
* `inBody` is false only for the stylesheet itself, which has no enclosing rule
|
|
462
|
+
* to drop and no `}` to tidy up to.
|
|
463
|
+
*/
|
|
464
|
+
function collectDrops(css, items, live, out, bodyStart, bodyEnd, inBody) {
|
|
465
|
+
const local = [];
|
|
466
|
+
const pinned = [];
|
|
467
|
+
let allGone = true;
|
|
468
|
+
for (const item of items) {
|
|
469
|
+
if (item.kind === 'comment') {
|
|
470
|
+
// `/*!` is a licence marker, so it outlives its rule rather than keeping
|
|
471
|
+
// an empty one alive. Authoring noise goes with the rule.
|
|
472
|
+
if (item.pinned)
|
|
473
|
+
pinned.push({ start: item.start, end: item.end });
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (item.kind === 'prop') {
|
|
477
|
+
if (live.has(item.name))
|
|
478
|
+
allGone = false;
|
|
479
|
+
else
|
|
480
|
+
local.push({ start: item.start, end: item.end });
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (item.kind === 'group' && item.children !== null) {
|
|
484
|
+
const sub = [];
|
|
485
|
+
const r = collectDrops(css, item.children, live, sub, item.bodyStart, item.bodyEnd, true);
|
|
486
|
+
if (r.gone) {
|
|
487
|
+
const span = item.holdsSlot
|
|
488
|
+
? { start: item.bodyStart, end: item.bodyEnd }
|
|
489
|
+
: { start: item.start, end: item.end };
|
|
490
|
+
local.push(...carve(span, r.keep));
|
|
491
|
+
pinned.push(...r.keep);
|
|
492
|
+
// An emptied slot-holder still occupies its position, so the parent is
|
|
493
|
+
// not empty either.
|
|
494
|
+
if (item.holdsSlot)
|
|
495
|
+
allGone = false;
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
local.push(...sub);
|
|
499
|
+
allGone = false;
|
|
500
|
+
}
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
allGone = false;
|
|
504
|
+
}
|
|
505
|
+
if (allGone && inBody)
|
|
506
|
+
return { gone: true, keep: pinned };
|
|
507
|
+
out.push(...tidy(css, local, bodyStart, bodyEnd, inBody));
|
|
508
|
+
return { gone: false, keep: [] };
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Merge the ranges, then let a drop that reaches the end of the body absorb the
|
|
512
|
+
* separator in front of it — without it, `{--a:1;--b:2}` losing `--b` emits
|
|
513
|
+
* `{--a:1;}`.
|
|
514
|
+
*
|
|
515
|
+
* Only inside a body: at stylesheet level the `;` in front of a dropped rule
|
|
516
|
+
* terminates a statement of its own, and eating it turns `@import url(a.css);`
|
|
517
|
+
* into `@import url(a.css)`.
|
|
518
|
+
*/
|
|
519
|
+
function tidy(css, ranges, bodyStart, bodyEnd, inBody) {
|
|
520
|
+
if (ranges.length === 0)
|
|
521
|
+
return ranges;
|
|
522
|
+
const sorted = [...ranges].sort((a, b) => a.start - b.start);
|
|
523
|
+
const merged = [sorted[0]];
|
|
524
|
+
for (const r of sorted.slice(1)) {
|
|
525
|
+
const prev = merged[merged.length - 1];
|
|
526
|
+
// Only separators between two drops means no survivor sits between them.
|
|
527
|
+
if (r.start <= prev.end || /^[\s;]*$/.test(css.slice(prev.end, r.start))) {
|
|
528
|
+
prev.end = Math.max(prev.end, r.end);
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
merged.push(r);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const last = merged[merged.length - 1];
|
|
535
|
+
if (inBody && /^\s*$/.test(css.slice(last.end, bodyEnd))) {
|
|
536
|
+
while (last.start > bodyStart && /[\s;]/.test(css[last.start - 1])) {
|
|
537
|
+
last.start--;
|
|
538
|
+
}
|
|
539
|
+
// Give a trailing escape its character back. `font-family:Foo\ ;` ends in
|
|
540
|
+
// an escaped SPACE, and trimming the space left `Foo\` against the closing
|
|
541
|
+
// `}` — which the backslash then escaped, swallowing the rule. An ODD run
|
|
542
|
+
// of backslashes means the last one is an escape still looking for a
|
|
543
|
+
// victim.
|
|
544
|
+
let slashes = 0;
|
|
545
|
+
while (last.start - 1 - slashes >= bodyStart &&
|
|
546
|
+
css[last.start - 1 - slashes] === '\\') {
|
|
547
|
+
slashes++;
|
|
548
|
+
}
|
|
549
|
+
if (slashes % 2 === 1)
|
|
550
|
+
last.start++;
|
|
551
|
+
}
|
|
552
|
+
return merged;
|
|
553
|
+
}
|
|
554
|
+
function splice(css, ranges) {
|
|
555
|
+
if (ranges.length === 0)
|
|
556
|
+
return css;
|
|
557
|
+
const sorted = [...ranges].sort((a, b) => a.start - b.start);
|
|
558
|
+
let out = '';
|
|
559
|
+
let at = 0;
|
|
560
|
+
for (const r of sorted) {
|
|
561
|
+
if (r.start > at)
|
|
562
|
+
out += css.slice(at, r.start);
|
|
563
|
+
at = Math.max(at, r.end);
|
|
564
|
+
}
|
|
565
|
+
return out + css.slice(at);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* `keepCustomProperties`, coerced to an array BEFORE anything spreads it.
|
|
569
|
+
*
|
|
570
|
+
* A plain string is iterable, so `keepCustomProperties: '--brand'` spread into
|
|
571
|
+
* characters and produced one "ignoring \"-\"" warning per letter, none of
|
|
572
|
+
* which said the option takes an array — while the name itself was purged. A
|
|
573
|
+
* number or object is not iterable at all and threw.
|
|
574
|
+
*
|
|
575
|
+
* Exported because every caller that assembles the safelist must coerce first;
|
|
576
|
+
* `livePropSafelist` spread the raw option and hit both of those.
|
|
577
|
+
*/
|
|
578
|
+
/** @codexApi {"parent":"pithy.ssr.build.css-live-props","name":"asSafelistArray","stability":"experimental","signature":"(value: unknown) => readonly (string | RegExp)[]"} */
|
|
579
|
+
export function asSafelistArray(value) {
|
|
580
|
+
if (value === undefined || value === null)
|
|
581
|
+
return [];
|
|
582
|
+
if (typeof value === 'string') {
|
|
583
|
+
console.warn(`[pithy:ssg] keepCustomProperties must be an ARRAY; got the string ` +
|
|
584
|
+
`"${value}". Reading it as ["${value}"] — wrap it to silence this.`);
|
|
585
|
+
return [value];
|
|
586
|
+
}
|
|
587
|
+
if (Array.isArray(value))
|
|
588
|
+
return value;
|
|
589
|
+
// A `Set` is the shape an author reaches for after de-duplicating names, and
|
|
590
|
+
// rejecting it dropped every entry it held.
|
|
591
|
+
if (typeof value[Symbol.iterator] === 'function') {
|
|
592
|
+
return [...value];
|
|
593
|
+
}
|
|
594
|
+
console.warn(`[pithy:ssg] keepCustomProperties must be an ARRAY; ignoring a ` +
|
|
595
|
+
`${typeof value} value. Nothing extra is kept.`);
|
|
596
|
+
return [];
|
|
597
|
+
}
|
|
598
|
+
function safelistMatcher(entries) {
|
|
599
|
+
const literals = new Set();
|
|
600
|
+
const prefixes = [];
|
|
601
|
+
const patterns = [];
|
|
602
|
+
for (const entry of entries) {
|
|
603
|
+
if (entry instanceof RegExp) {
|
|
604
|
+
// `g`/`y` are dropped: `.test()` advances `lastIndex` on a sticky or
|
|
605
|
+
// global regex, so the same entry would match every other name.
|
|
606
|
+
patterns.push(new RegExp(entry.source, entry.flags.replace(/[gy]/g, '')));
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
// Validated, not trusted. An entry that cannot match any name used to be
|
|
610
|
+
// accepted in silence, so `keepCustomProperties: ['--color-*-rgb']` — a
|
|
611
|
+
// MIDDLE glob, which this matcher does not support — protected nothing and
|
|
612
|
+
// the author lost the declarations they had asked to keep. A non-string
|
|
613
|
+
// entry was worse: `entry.endsWith` threw and the caller's catch skipped
|
|
614
|
+
// the whole pass, so nothing was purged at all and nothing said why.
|
|
615
|
+
if (typeof entry !== 'string') {
|
|
616
|
+
console.warn(`[pithy:ssg] keepCustomProperties: ignoring a non-string entry ` +
|
|
617
|
+
`(${typeof entry}). Entries are names, \`--prefix-*\` patterns or ` +
|
|
618
|
+
`RegExps.`);
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (!entry.startsWith('--')) {
|
|
622
|
+
console.warn(`[pithy:ssg] keepCustomProperties: ignoring "${entry}" — a custom ` +
|
|
623
|
+
`property name starts with "--".`);
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const star = entry.indexOf('*');
|
|
627
|
+
if (star !== -1 && star !== entry.length - 1) {
|
|
628
|
+
console.warn(`[pithy:ssg] keepCustomProperties: ignoring "${entry}" — "*" is only ` +
|
|
629
|
+
`supported as a trailing wildcard, as in "--color-*".`);
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
if (star !== -1)
|
|
633
|
+
prefixes.push(entry.slice(0, -1));
|
|
634
|
+
else
|
|
635
|
+
literals.add(entry);
|
|
636
|
+
}
|
|
637
|
+
return name => literals.has(name) ||
|
|
638
|
+
prefixes.some(p => name.startsWith(p)) ||
|
|
639
|
+
patterns.some(re => re.test(name));
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Drop the custom-property declarations in `css` that nothing live references.
|
|
643
|
+
*
|
|
644
|
+
* Run it on the output of `purgeCssForPages` in `css-purge.ts`, not on the
|
|
645
|
+
* collected sheet: the rule purge is what makes a channel variable dead in the
|
|
646
|
+
* first place, by removing the unused utility rule that referenced it.
|
|
647
|
+
*
|
|
648
|
+
* @param css - Stylesheet text, already rule-purged.
|
|
649
|
+
* @param options - Safelist and the rendered HTML to read roots from.
|
|
650
|
+
* @returns The stylesheet with dead declarations spliced out.
|
|
651
|
+
*/
|
|
652
|
+
/** @codexApi {"parent":"pithy.ssr.build.css-live-props","name":"purgeDeadCustomProps","stability":"experimental","signature":"(css: string, options?: DeadPropOptions) => string"} */
|
|
653
|
+
export function purgeDeadCustomProps(css, options = {}) {
|
|
654
|
+
if (!css)
|
|
655
|
+
return css;
|
|
656
|
+
try {
|
|
657
|
+
const scan = { roots: new Set(), edges: new Map() };
|
|
658
|
+
const items = splitBody(css, 0, css.length, scan);
|
|
659
|
+
for (const html of options.htmlSources ?? []) {
|
|
660
|
+
addVarRefs(html, scan.roots);
|
|
661
|
+
}
|
|
662
|
+
const live = resolveLive(scan, safelistMatcher([
|
|
663
|
+
...DEFAULT_LIVE_PROP_SAFELIST,
|
|
664
|
+
...asSafelistArray(options.safelist),
|
|
665
|
+
]));
|
|
666
|
+
const ranges = [];
|
|
667
|
+
collectDrops(css, items, live, ranges, 0, css.length, false);
|
|
668
|
+
return splice(css, ranges);
|
|
669
|
+
}
|
|
670
|
+
catch (err) {
|
|
671
|
+
console.warn('[pithy:ssg] Dead custom-property purge failed; shipping every ' +
|
|
672
|
+
'declaration. The stylesheet renders correctly but larger.', err);
|
|
673
|
+
return css;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Everything in a rendered page that can reference a custom property outside
|
|
678
|
+
* the stylesheet: the body fragment, the shell's `html`/`body` attribute values
|
|
679
|
+
* (`style="color:var(--x)"`), and inline `script` bodies, which ship in the
|
|
680
|
+
* page and can write a style string.
|
|
681
|
+
*
|
|
682
|
+
* Extracted and exported because the assembly in `runBuildSSG` is otherwise
|
|
683
|
+
* unreachable from a test — gutting it to `[]` reddened nothing, so a refactor
|
|
684
|
+
* could drop a source silently and every affected page would lose its colour.
|
|
685
|
+
*/
|
|
686
|
+
/** @codexApi {"parent":"pithy.ssr.build.css-live-props","name":"collectHtmlSources","stability":"experimental","signature":"(pages: readonly LivePropPageSource[], sprite?: LivePropSpriteSource) => string[]"} */
|
|
687
|
+
export function collectHtmlSources(pages, sprite) {
|
|
688
|
+
const out = pages.flatMap(p => [
|
|
689
|
+
p.html ?? '',
|
|
690
|
+
...Object.values(p.head?.htmlAttrs ?? {}),
|
|
691
|
+
...Object.values(p.head?.bodyAttrs ?? {}),
|
|
692
|
+
...(p.head?.script ?? []).map(t => t.content ?? ''),
|
|
693
|
+
]);
|
|
694
|
+
// Sprite symbols are inlined AFTER this purge runs, and the sprite parser's
|
|
695
|
+
// `SAFE_ATTR_VALUE_RE` admits `var(--x)` in a presentational attribute — so
|
|
696
|
+
// a `<stop stop-color="var(--brand)">` inside `<defs>` (which `optimize.ts`
|
|
697
|
+
// does not rewrite to `currentColor`) ships a reference nothing else scans.
|
|
698
|
+
for (const sym of sprite?.symbols?.values() ?? []) {
|
|
699
|
+
out.push(sym.inner ?? '');
|
|
700
|
+
out.push(...Object.values(sym.presentation ?? {}));
|
|
701
|
+
}
|
|
702
|
+
return out;
|
|
703
|
+
}
|
|
704
|
+
//# sourceMappingURL=css-live-props.js.map
|