@bamboocss/node 1.20.4 → 1.22.0
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/dist/index.cjs +155 -74
- package/dist/index.d.cts +23 -16
- package/dist/index.d.mts +23 -16
- package/dist/index.mjs +156 -75
- package/package.json +13 -13
package/dist/index.cjs
CHANGED
|
@@ -134,7 +134,6 @@ async function codegen(ctx, ids) {
|
|
|
134
134
|
}
|
|
135
135
|
const promises = artifacts.map((artifact) => limit(() => ctx.output.write(artifact)));
|
|
136
136
|
await Promise.allSettled(promises);
|
|
137
|
-
await ctx.writeGroupRegistry();
|
|
138
137
|
await ctx.hooks["codegen:done"]?.({ changed: ids });
|
|
139
138
|
return {
|
|
140
139
|
box: ctx.initMessage(),
|
|
@@ -1606,9 +1605,7 @@ var OutputEngine = class {
|
|
|
1606
1605
|
const unresolvedReasons = {
|
|
1607
1606
|
"unresolvable-value": (prop) => [`${prop} will not reach the stylesheet because its value is not statically known`, "Make the value static to group it."],
|
|
1608
1607
|
"missing-property": (prop) => [`${prop} will not reach the stylesheet because its value could not be evaluated at build time`, "Make the value static to group it."],
|
|
1609
|
-
"unenumerable-keys": () => ["an object spread or computed key leaves the build unable to tell which properties this call sets", "Write the properties out, or spread a value the build can resolve, to group it."]
|
|
1610
|
-
"ambiguous-merge": () => ["two of these arguments set the same property, and the build cannot tell a merge from a pair of alternatives", "Merge them into a single object to group it."],
|
|
1611
|
-
"too-many-combinations": () => ["this call has more conditional branches than the build will enumerate", "Split it into fewer conditional properties to group it."]
|
|
1608
|
+
"unenumerable-keys": () => ["an object spread or computed key leaves the build unable to tell which properties this call sets", "Write the properties out, or spread a value the build can resolve, to group it."]
|
|
1612
1609
|
};
|
|
1613
1610
|
var BambooContext = class extends _bamboocss_generator.Generator {
|
|
1614
1611
|
runtime;
|
|
@@ -1637,15 +1634,13 @@ var BambooContext = class extends _bamboocss_generator.Generator {
|
|
|
1637
1634
|
this.explicitDeps = this.getExplicitDependencies();
|
|
1638
1635
|
const disagreement = (0, _bamboocss_core.checkNamingAgreement)(this);
|
|
1639
1636
|
if (disagreement) throw new _bamboocss_shared.BambooError("NAMING_DISAGREEMENT", (0, _bamboocss_core.formatNamingDisagreement)(disagreement));
|
|
1640
|
-
if (config.cssMode === "grouped" && config.staticCss?.css?.length) _bamboocss_logger.logger.warn("config", "`staticCss.css` does not back runtime `css()` calls under `cssMode: 'grouped'`. It pre-generates one rule per property and value, while a grouped class names a whole call. The rules are still emitted, but nothing the runtime returns will match them — see https://bamboocss.com/docs/references/config#cssmode");
|
|
1641
1637
|
}
|
|
1642
1638
|
/**
|
|
1643
1639
|
* Report `css()` calls whose styles the build could not fully see.
|
|
1644
1640
|
*
|
|
1645
|
-
*
|
|
1646
|
-
*
|
|
1647
|
-
*
|
|
1648
|
-
* than merely losing a declaration, so it must not be silent.
|
|
1641
|
+
* A warning rather than an error: the build is not wrong, the call site is unresolvable,
|
|
1642
|
+
* and the declarations it did resolve still apply. But the ones it could not have no rule
|
|
1643
|
+
* behind them and are simply absent, so it must not be silent.
|
|
1649
1644
|
*/
|
|
1650
1645
|
reportUnresolvedStyles = (result) => {
|
|
1651
1646
|
const unresolved = result.unresolved;
|
|
@@ -1653,18 +1648,16 @@ var BambooContext = class extends _bamboocss_generator.Generator {
|
|
|
1653
1648
|
for (const entry of unresolved) {
|
|
1654
1649
|
const where = `${entry.filePath}:${entry.line}:${entry.column}`;
|
|
1655
1650
|
const prop = entry.prop ? `\`${entry.prop}\`` : "a property";
|
|
1656
|
-
const [what
|
|
1651
|
+
const [what] = unresolvedReasons[entry.reason](prop);
|
|
1657
1652
|
if (entry.kind === "recipe") {
|
|
1658
1653
|
const at = entry.prop ? ` at \`${entry.prop}\`` : "";
|
|
1659
1654
|
_bamboocss_logger.logger.warn("recipe", `${where} — ${what}${at}. A recipe's classes are named from a hash of its config, so a declaration the build cannot see gives the build and the browser different names and the element renders with no styles at all. Set \`className\` on the recipe, so its name does not depend on what the build could resolve. See https://bamboocss.com/docs/concepts/recipes`);
|
|
1660
1655
|
continue;
|
|
1661
1656
|
}
|
|
1662
|
-
|
|
1657
|
+
{
|
|
1663
1658
|
const at = entry.prop ? ` at \`${entry.prop}\`` : "";
|
|
1664
1659
|
_bamboocss_logger.logger.warn("css", `${where} — ${what}${at}. The build emits a rule per declaration it can see, and the runtime names a class for every declaration the object actually has — so the ones it could not see have no rule behind them and are simply absent. Write the value out, or generate it with \`staticCss\` if it is genuinely dynamic. See https://bamboocss.com/docs/guides/dynamic-styling`);
|
|
1665
|
-
continue;
|
|
1666
1660
|
}
|
|
1667
|
-
_bamboocss_logger.logger.warn("grouped", `${where} — ${what}. Under \`cssMode: 'grouped'\` one class names the whole \`css()\` call, so this call cannot use one: it falls back to naming each declaration separately and keeps only the ones the build could resolve. ${fix} See https://bamboocss.com/docs/references/config#cssmode`);
|
|
1668
1661
|
}
|
|
1669
1662
|
};
|
|
1670
1663
|
getExplicitDependencies = () => {
|
|
@@ -1718,44 +1711,12 @@ var BambooContext = class extends _bamboocss_generator.Generator {
|
|
|
1718
1711
|
filesWithCss.push(file);
|
|
1719
1712
|
results.push(result);
|
|
1720
1713
|
});
|
|
1721
|
-
this.writeGroupRegistry();
|
|
1722
1714
|
return {
|
|
1723
1715
|
filesWithCss,
|
|
1724
1716
|
files,
|
|
1725
1717
|
results
|
|
1726
1718
|
};
|
|
1727
1719
|
};
|
|
1728
|
-
/**
|
|
1729
|
-
* Refresh the group registry the generated `css()` consults.
|
|
1730
|
-
*
|
|
1731
|
-
* `codegen` emits it empty — it runs on config change, before anything is extracted — so
|
|
1732
|
-
* this is the pass that fills it in. Called after extraction rather than alongside the
|
|
1733
|
-
* CSS, because the PostCSS plugin never writes a stylesheet to disk at all and would
|
|
1734
|
-
* otherwise never get one.
|
|
1735
|
-
*
|
|
1736
|
-
* Safe to run late or not at all: the runtime *adds* atomic names to the group class
|
|
1737
|
-
* rather than replacing it, so a stale registry costs a class that matches nothing.
|
|
1738
|
-
*/
|
|
1739
|
-
writeGroupRegistry = () => {
|
|
1740
|
-
if (this.config.cssMode !== "grouped") return;
|
|
1741
|
-
const names = this.getGroupRegistry();
|
|
1742
|
-
if (!names.length) {
|
|
1743
|
-
const existing = this.runtime.path.join(...this.paths.css, this.file.ext(_bamboocss_generator.GROUP_REGISTRY_FILE));
|
|
1744
|
-
if (this.runtime.fs.existsSync(existing)) return;
|
|
1745
|
-
}
|
|
1746
|
-
const registry = (0, _bamboocss_generator.generateGroupRegistry)(this, names);
|
|
1747
|
-
return this.output.write({
|
|
1748
|
-
id: "css-fn",
|
|
1749
|
-
dir: this.paths.css,
|
|
1750
|
-
files: [{
|
|
1751
|
-
file: this.file.ext(_bamboocss_generator.GROUP_REGISTRY_FILE),
|
|
1752
|
-
code: registry.js
|
|
1753
|
-
}, {
|
|
1754
|
-
file: this.file.extDts(_bamboocss_generator.GROUP_REGISTRY_FILE),
|
|
1755
|
-
code: registry.dts
|
|
1756
|
-
}]
|
|
1757
|
-
});
|
|
1758
|
-
};
|
|
1759
1720
|
writeCss = (sheet) => {
|
|
1760
1721
|
_bamboocss_logger.logger.info("css", this.runtime.path.join(...this.paths.root, "styles.css"));
|
|
1761
1722
|
return this.output.write({
|
|
@@ -2162,6 +2123,39 @@ function parseDependency(fileOrGlob) {
|
|
|
2162
2123
|
*/
|
|
2163
2124
|
const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2164
2125
|
/**
|
|
2126
|
+
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2127
|
+
* parser understands it.
|
|
2128
|
+
*
|
|
2129
|
+
* Both, because neither alone is complete and every scan below is safe when over-fed. A file
|
|
2130
|
+
* the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`), and
|
|
2131
|
+
* those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2132
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2133
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2134
|
+
* like that reports no tokens and no elements at all.
|
|
2135
|
+
*
|
|
2136
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2137
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2138
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2139
|
+
*
|
|
2140
|
+
* So the cost is one extra read per file, and a second regex pass over the files a transform
|
|
2141
|
+
* actually changed. Measured at ~14ms for 806 files, against a build where css emission alone
|
|
2142
|
+
* is an order of magnitude more.
|
|
2143
|
+
*/
|
|
2144
|
+
function* sourceTexts(ctx) {
|
|
2145
|
+
for (const file of ctx.getFiles()) {
|
|
2146
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2147
|
+
let onDisk;
|
|
2148
|
+
try {
|
|
2149
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2150
|
+
} catch {
|
|
2151
|
+
onDisk = void 0;
|
|
2152
|
+
}
|
|
2153
|
+
const parsed = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2154
|
+
if (onDisk != null) yield onDisk;
|
|
2155
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2165
2159
|
* Collect token references that reading the generated css cannot reveal.
|
|
2166
2160
|
*
|
|
2167
2161
|
* This is deliberately textual and therefore over-inclusive: a match inside a comment or
|
|
@@ -2174,22 +2168,30 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2174
2168
|
* encode every style a second time — pass none, and stay correct only because a path the
|
|
2175
2169
|
* scan misses can lose nothing: `token()` hands javascript a literal for exactly the
|
|
2176
2170
|
* tokens this cannot see, and hands it a `var()` only for virtual, conditional and
|
|
2177
|
-
* negative tokens, which `getAlwaysKeptTokenVars` keeps
|
|
2178
|
-
*
|
|
2179
|
-
*
|
|
2171
|
+
* negative tokens, which `getAlwaysKeptTokenVars` keeps.
|
|
2172
|
+
*
|
|
2173
|
+
* Those blanket keeps are no longer unconditional — `tokensReachableFromJs` gates them — and
|
|
2174
|
+
* this comment used to warn that narrowing them would break the argument above. It does not,
|
|
2175
|
+
* for a reason worth stating rather than rediscovering: the gate matches `token(` regardless
|
|
2176
|
+
* of what follows, where `TOKEN_CALL` below needs a string literal. So the one shape this
|
|
2177
|
+
* scan cannot resolve, a path built from a constant, is exactly the shape that turns the
|
|
2178
|
+
* gate on and restores every blanket keep. The two failures line up, which is what makes the
|
|
2179
|
+
* redundancy hold.
|
|
2180
|
+
*
|
|
2181
|
+
* That alignment is a real coupling, not an observation: the gate's call pattern has to stay
|
|
2182
|
+
* a superset of this one, matching wherever `TOKEN_CALL` matches and also wherever it gives
|
|
2183
|
+
* up. It briefly was not — this allowed whitespace around the `.` of `token.var` and the gate
|
|
2184
|
+
* did not, so a formatter wrapping `token\n .var(SOME_CONST)` slipped past both. Change one
|
|
2185
|
+
* of the two and change the other; `token-references.test.ts` pins the property directly.
|
|
2186
|
+
*
|
|
2187
|
+
* Where it stops holding is a binding renamed away from `token` — `const t = token`, then
|
|
2188
|
+
* `t('spacing.4')` — which matches neither. `token-references.test.ts` pins both halves.
|
|
2180
2189
|
*/
|
|
2181
2190
|
function collectTokenReferences(ctx, results) {
|
|
2182
2191
|
const paths = /* @__PURE__ */ new Set();
|
|
2183
2192
|
const vars = /* @__PURE__ */ new Set();
|
|
2184
2193
|
for (const result of results) for (const item of result.token) for (const value of item.data ?? []) if (typeof value === "string") paths.add(value);
|
|
2185
|
-
for (const
|
|
2186
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2187
|
-
let content = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2188
|
-
if (content == null) try {
|
|
2189
|
-
content = ctx.runtime.fs.readFileSync(filePath);
|
|
2190
|
-
} catch {
|
|
2191
|
-
continue;
|
|
2192
|
-
}
|
|
2194
|
+
for (const content of sourceTexts(ctx)) {
|
|
2193
2195
|
for (const match of content.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2194
2196
|
for (const name of (0, _bamboocss_shared.cssVarRefs)(content)) vars.add(name);
|
|
2195
2197
|
}
|
|
@@ -2219,22 +2221,80 @@ function collectKeyframeReferences(ctx, names) {
|
|
|
2219
2221
|
const found = /* @__PURE__ */ new Set();
|
|
2220
2222
|
if (!declared.length) return found;
|
|
2221
2223
|
const patterns = declared.map((name) => [name, new RegExp(`\\b${escapeRegExp(name)}\\b`)]);
|
|
2222
|
-
for (const
|
|
2223
|
-
if (found.size === declared.length) break;
|
|
2224
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2225
|
-
let content = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2226
|
-
if (content == null) try {
|
|
2227
|
-
content = ctx.runtime.fs.readFileSync(filePath);
|
|
2228
|
-
} catch {
|
|
2229
|
-
continue;
|
|
2230
|
-
}
|
|
2224
|
+
for (const content of sourceTexts(ctx)) {
|
|
2231
2225
|
for (const [name, pattern] of patterns) if (!found.has(name) && pattern.test(content)) found.add(name);
|
|
2226
|
+
if (found.size === declared.length) break;
|
|
2232
2227
|
}
|
|
2233
2228
|
return found;
|
|
2234
2229
|
}
|
|
2235
2230
|
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2236
2231
|
/** The keyframes the theme declares — the allow-list `pruneKeyframes` works against. */
|
|
2237
2232
|
const keyframeNames = (ctx) => Object.keys(ctx.config.theme?.keyframes ?? {});
|
|
2233
|
+
/**
|
|
2234
|
+
* The HTML element names the source renders.
|
|
2235
|
+
*
|
|
2236
|
+
* A textual scan for an opening tag, over the same files the other collectors read. Matching
|
|
2237
|
+
* `<tag` rather than parsing: a template can spell an element in more ways than a parser of
|
|
2238
|
+
* any one framework's syntax would find, and over-reporting an element only keeps a reset
|
|
2239
|
+
* rule that would otherwise go.
|
|
2240
|
+
*
|
|
2241
|
+
* Lowercase-initial only, so a JSX component (`<Button />`) is not mistaken for an element.
|
|
2242
|
+
* That cuts the other way too — a component rendering `<button>` inside a dependency is
|
|
2243
|
+
* invisible here, which is why `prunePreflight` is opt-in.
|
|
2244
|
+
*
|
|
2245
|
+
* The commoner blind spot is nearer than a dependency: this reads `include`, and `include`
|
|
2246
|
+
* conventionally covers components rather than markup. An entry template — `index.html`,
|
|
2247
|
+
* `app.html` — is where `<table>`, `<noscript>` and a page's static markup usually live, and a
|
|
2248
|
+
* glob rooted at `./src` does not match it, so every element appearing only there loses its
|
|
2249
|
+
* reset. Nothing here can detect that; the file simply is not in the list. Listing it in
|
|
2250
|
+
* `include` fixes it, because this reads whatever `include` covers rather than only what the
|
|
2251
|
+
* parser understands — `token-references.test.ts` pins both halves.
|
|
2252
|
+
*
|
|
2253
|
+
* Reading the raw file as well as the parsed copy is what makes an SFC work here; see
|
|
2254
|
+
* `sourceTexts`. On a healthy `.svelte` file the two agree on everything but `<script>`, so
|
|
2255
|
+
* the raw read earns its place only when a transform fails -- and there it is the difference
|
|
2256
|
+
* between the file's elements and none of them.
|
|
2257
|
+
*/
|
|
2258
|
+
function collectRenderedElements(ctx) {
|
|
2259
|
+
const found = /* @__PURE__ */ new Set();
|
|
2260
|
+
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2261
|
+
return found;
|
|
2262
|
+
}
|
|
2263
|
+
/**
|
|
2264
|
+
* Whether any source file reaches for a token from javascript.
|
|
2265
|
+
*
|
|
2266
|
+
* The tokens artifact is generated into the project rather than installed, so the import is
|
|
2267
|
+
* written in the project's own source and a scan of `include` sees it. When this comes back
|
|
2268
|
+
* false, the declarations kept purely so `token()` can answer have no caller to answer.
|
|
2269
|
+
*
|
|
2270
|
+
* Deliberately loose, and loose in a specific direction: a false positive keeps a declaration
|
|
2271
|
+
* nothing reads, a false negative returns a `var()` nothing declares.
|
|
2272
|
+
*
|
|
2273
|
+
* So the import test is any module specifier with a `/tokens` path segment, rather than the
|
|
2274
|
+
* literal `styled-system/tokens` it used to be. `outdir` is configurable, so the artifact is
|
|
2275
|
+
* only at `styled-system/` by default, and the literal missed `outdir: 'design-system'`, a
|
|
2276
|
+
* tsconfig path alias, and `styled-system/tokens/index.mjs` -- which is the only spelling
|
|
2277
|
+
* NodeNext accepts, the artifact being a directory. It is still anchored to `from`, `import`
|
|
2278
|
+
* or `require`, because without that anchor a route or a url (`fetch('/api/tokens')`) reads
|
|
2279
|
+
* as an import and quietly switches the whole optimisation off.
|
|
2280
|
+
*
|
|
2281
|
+
* The call test allows whitespace around the dot for the same reason `TOKEN_CALL` does. It
|
|
2282
|
+
* did not, which broke the alignment the note on `collectTokenReferences` rests on: a
|
|
2283
|
+
* formatter wrapping `token\n .var(SOME_CONST)` was invisible to that scan *and* to this
|
|
2284
|
+
* gate *and* to the parser, whose callee is a property access. A comment in the same position
|
|
2285
|
+
* -- `token/*x*\/.var(` -- is still invisible to all three.
|
|
2286
|
+
*
|
|
2287
|
+
* One shape it still does not see, pinned in `token-references.test.ts`: a binding renamed
|
|
2288
|
+
* away from `token`, as in `const t = token`. Also unseen is a caller outside `include`,
|
|
2289
|
+
* which scopes style extraction rather than everything that may import — a script, a config,
|
|
2290
|
+
* or a sibling workspace package. Both prune declarations the running app then asks for, and
|
|
2291
|
+
* neither reports itself; `pruneUnusedTokens: false` is the way out.
|
|
2292
|
+
*/
|
|
2293
|
+
function tokensReachableFromJs(ctx) {
|
|
2294
|
+
const pattern = /\btoken(\s*\.\s*var)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2295
|
+
for (const content of sourceTexts(ctx)) if (pattern.test(content)) return true;
|
|
2296
|
+
return false;
|
|
2297
|
+
}
|
|
2238
2298
|
//#endregion
|
|
2239
2299
|
//#region src/builder.ts
|
|
2240
2300
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2380,7 +2440,6 @@ var Builder = class {
|
|
|
2380
2440
|
const files = ctx.getFiles();
|
|
2381
2441
|
const done = _bamboocss_logger.logger.time.info("Extracted in");
|
|
2382
2442
|
files.map((file) => this.extractFile(ctx, file));
|
|
2383
|
-
ctx.writeGroupRegistry();
|
|
2384
2443
|
done();
|
|
2385
2444
|
};
|
|
2386
2445
|
isValidRoot = (root) => {
|
|
@@ -2396,14 +2455,33 @@ var Builder = class {
|
|
|
2396
2455
|
_bamboocss_logger.logger.warn("postcss", "Generated css is already present in this file, so nothing was injected. It is imported and generated here at once — keep the `@import` of `styles.css` or the `@layer` statement that the postcss plugin injects at, not both.");
|
|
2397
2456
|
return;
|
|
2398
2457
|
}
|
|
2458
|
+
root.append(this.toCss());
|
|
2459
|
+
};
|
|
2460
|
+
/**
|
|
2461
|
+
* The finished stylesheet, as a string.
|
|
2462
|
+
*
|
|
2463
|
+
* The same sheet `write` injects into a postcss root, for callers that want the css
|
|
2464
|
+
* rather than a mutated root -- the vite plugin serves it as a virtual module. Both go
|
|
2465
|
+
* through here so a build cannot depend on which integration asked for it.
|
|
2466
|
+
*
|
|
2467
|
+
* `layerParams` is the one thing that differs between them, and it is not cosmetic: the
|
|
2468
|
+
* `@layer a, b, c;` statement is what fixes layer *order*, and css layers are ordered by
|
|
2469
|
+
* first appearance otherwise. `write` leaves it out because the root it appends to is a
|
|
2470
|
+
* file that already declares it -- that declaration is what `isValidRoot` matches on. A
|
|
2471
|
+
* virtual module is the whole stylesheet and has nothing to inherit it from.
|
|
2472
|
+
*
|
|
2473
|
+
* `extract` has to have run first: this reads the encoder rather than filling it.
|
|
2474
|
+
*/
|
|
2475
|
+
toCss = ({ layerParams = false } = {}) => {
|
|
2399
2476
|
const ctx = this.getContextOrThrow();
|
|
2400
2477
|
const sheet = ctx.createSheet();
|
|
2478
|
+
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2401
2479
|
ctx.appendBaselineCss(sheet);
|
|
2402
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []));
|
|
2480
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []), tokensReachableFromJs(ctx));
|
|
2403
2481
|
else ctx.pruneTokens(sheet);
|
|
2482
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2404
2483
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2405
|
-
|
|
2406
|
-
root.append(css);
|
|
2484
|
+
return ctx.getCss(sheet);
|
|
2407
2485
|
};
|
|
2408
2486
|
registerDependency = (fn) => {
|
|
2409
2487
|
const ctx = this.getContextOrThrow();
|
|
@@ -2490,6 +2568,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2490
2568
|
if (type) {
|
|
2491
2569
|
const done = _bamboocss_logger.logger.time.info(ctx.messages.cssArtifactComplete(type));
|
|
2492
2570
|
ctx.appendCssOfType(type, sheet);
|
|
2571
|
+
if (type === "preflight" && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2493
2572
|
if (outfile) {
|
|
2494
2573
|
const css = ctx.getCss(sheet);
|
|
2495
2574
|
_bamboocss_logger.logger.info("css", ctx.runtime.path.resolve(outfile));
|
|
@@ -2504,8 +2583,9 @@ const cssgen = async (ctx, options) => {
|
|
|
2504
2583
|
ctx.appendBaselineCss(sheet);
|
|
2505
2584
|
}
|
|
2506
2585
|
ctx.appendParserCss(sheet);
|
|
2507
|
-
if (!minimal && ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, results));
|
|
2586
|
+
if (!minimal && ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, results), tokensReachableFromJs(ctx));
|
|
2508
2587
|
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2588
|
+
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2509
2589
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2510
2590
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
2511
2591
|
else if (outfile) {
|
|
@@ -2576,8 +2656,9 @@ async function build(ctx, artifactIds) {
|
|
|
2576
2656
|
ctx.appendBaselineCss(sheet);
|
|
2577
2657
|
const parsed = ctx.parseFiles();
|
|
2578
2658
|
ctx.appendParserCss(sheet);
|
|
2579
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, parsed.results));
|
|
2659
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, parsed.results), tokensReachableFromJs(ctx));
|
|
2580
2660
|
else ctx.pruneTokens(sheet);
|
|
2661
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2581
2662
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2582
2663
|
await ctx.writeCss(sheet);
|
|
2583
2664
|
done(ctx.messages.buildComplete(parsed.files.length));
|
|
@@ -2622,11 +2703,11 @@ async function generate(config, configPath) {
|
|
|
2622
2703
|
ctx.appendLayerParams(sheet);
|
|
2623
2704
|
ctx.appendBaselineCss(sheet);
|
|
2624
2705
|
ctx.appendParserCss(sheet);
|
|
2625
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []));
|
|
2706
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []), tokensReachableFromJs(ctx));
|
|
2707
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2626
2708
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2627
2709
|
const css = ctx.getCss(sheet);
|
|
2628
2710
|
await ctx.runtime.fs.writeFile(outfile, css);
|
|
2629
|
-
await ctx.writeGroupRegistry();
|
|
2630
2711
|
done();
|
|
2631
2712
|
};
|
|
2632
2713
|
ctx.watchFiles(async (event, file) => {
|
|
@@ -2725,7 +2806,7 @@ export default defineConfig({
|
|
|
2725
2806
|
exclude: [],
|
|
2726
2807
|
|
|
2727
2808
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2728
|
-
// by default — worth
|
|
2809
|
+
// by default — worth 36-78% of a new project's stylesheet. Set \`pruneUnusedTokens: false\`
|
|
2729
2810
|
// if you read tokens from somewhere the build cannot see them — \`token.var()\` with a
|
|
2730
2811
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2731
2812
|
// \`staticCss\` to keep them.
|
package/dist/index.d.cts
CHANGED
|
@@ -55,10 +55,9 @@ declare class BambooContext extends Generator {
|
|
|
55
55
|
/**
|
|
56
56
|
* Report `css()` calls whose styles the build could not fully see.
|
|
57
57
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* than merely losing a declaration, so it must not be silent.
|
|
58
|
+
* A warning rather than an error: the build is not wrong, the call site is unresolvable,
|
|
59
|
+
* and the declarations it did resolve still apply. But the ones it could not have no rule
|
|
60
|
+
* behind them and are simply absent, so it must not be silent.
|
|
62
61
|
*/
|
|
63
62
|
reportUnresolvedStyles: (result: {
|
|
64
63
|
unresolved?: ParserResult["unresolved"];
|
|
@@ -72,18 +71,6 @@ declare class BambooContext extends Generator {
|
|
|
72
71
|
files: string[];
|
|
73
72
|
results: ParserResult[];
|
|
74
73
|
};
|
|
75
|
-
/**
|
|
76
|
-
* Refresh the group registry the generated `css()` consults.
|
|
77
|
-
*
|
|
78
|
-
* `codegen` emits it empty — it runs on config change, before anything is extracted — so
|
|
79
|
-
* this is the pass that fills it in. Called after extraction rather than alongside the
|
|
80
|
-
* CSS, because the PostCSS plugin never writes a stylesheet to disk at all and would
|
|
81
|
-
* otherwise never get one.
|
|
82
|
-
*
|
|
83
|
-
* Safe to run late or not at all: the runtime *adds* atomic names to the group class
|
|
84
|
-
* rather than replacing it, so a stale registry costs a class that matches nothing.
|
|
85
|
-
*/
|
|
86
|
-
writeGroupRegistry: () => Promise<PromiseSettledResult<void>[]> | undefined;
|
|
87
74
|
writeCss: (sheet?: Stylesheet) => Promise<PromiseSettledResult<void>[]> | undefined;
|
|
88
75
|
writeSplitCss: (sheet: Stylesheet) => Promise<void>;
|
|
89
76
|
watchConfig: (cb: (file: string) => void | Promise<void>, opts?: Omit<WatchOptions, "include">) => void;
|
|
@@ -137,6 +124,26 @@ declare class Builder {
|
|
|
137
124
|
extract: () => void;
|
|
138
125
|
isValidRoot: (root: Root) => boolean;
|
|
139
126
|
write: (root: Root) => void;
|
|
127
|
+
/**
|
|
128
|
+
* The finished stylesheet, as a string.
|
|
129
|
+
*
|
|
130
|
+
* The same sheet `write` injects into a postcss root, for callers that want the css
|
|
131
|
+
* rather than a mutated root -- the vite plugin serves it as a virtual module. Both go
|
|
132
|
+
* through here so a build cannot depend on which integration asked for it.
|
|
133
|
+
*
|
|
134
|
+
* `layerParams` is the one thing that differs between them, and it is not cosmetic: the
|
|
135
|
+
* `@layer a, b, c;` statement is what fixes layer *order*, and css layers are ordered by
|
|
136
|
+
* first appearance otherwise. `write` leaves it out because the root it appends to is a
|
|
137
|
+
* file that already declares it -- that declaration is what `isValidRoot` matches on. A
|
|
138
|
+
* virtual module is the whole stylesheet and has nothing to inherit it from.
|
|
139
|
+
*
|
|
140
|
+
* `extract` has to have run first: this reads the encoder rather than filling it.
|
|
141
|
+
*/
|
|
142
|
+
toCss: ({
|
|
143
|
+
layerParams
|
|
144
|
+
}?: {
|
|
145
|
+
layerParams?: boolean;
|
|
146
|
+
}) => string;
|
|
140
147
|
registerDependency: (fn: (dep: Message) => void) => void;
|
|
141
148
|
}
|
|
142
149
|
interface FileMeta {
|
package/dist/index.d.mts
CHANGED
|
@@ -55,10 +55,9 @@ declare class BambooContext extends Generator {
|
|
|
55
55
|
/**
|
|
56
56
|
* Report `css()` calls whose styles the build could not fully see.
|
|
57
57
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* than merely losing a declaration, so it must not be silent.
|
|
58
|
+
* A warning rather than an error: the build is not wrong, the call site is unresolvable,
|
|
59
|
+
* and the declarations it did resolve still apply. But the ones it could not have no rule
|
|
60
|
+
* behind them and are simply absent, so it must not be silent.
|
|
62
61
|
*/
|
|
63
62
|
reportUnresolvedStyles: (result: {
|
|
64
63
|
unresolved?: ParserResult["unresolved"];
|
|
@@ -72,18 +71,6 @@ declare class BambooContext extends Generator {
|
|
|
72
71
|
files: string[];
|
|
73
72
|
results: ParserResult[];
|
|
74
73
|
};
|
|
75
|
-
/**
|
|
76
|
-
* Refresh the group registry the generated `css()` consults.
|
|
77
|
-
*
|
|
78
|
-
* `codegen` emits it empty — it runs on config change, before anything is extracted — so
|
|
79
|
-
* this is the pass that fills it in. Called after extraction rather than alongside the
|
|
80
|
-
* CSS, because the PostCSS plugin never writes a stylesheet to disk at all and would
|
|
81
|
-
* otherwise never get one.
|
|
82
|
-
*
|
|
83
|
-
* Safe to run late or not at all: the runtime *adds* atomic names to the group class
|
|
84
|
-
* rather than replacing it, so a stale registry costs a class that matches nothing.
|
|
85
|
-
*/
|
|
86
|
-
writeGroupRegistry: () => Promise<PromiseSettledResult<void>[]> | undefined;
|
|
87
74
|
writeCss: (sheet?: Stylesheet) => Promise<PromiseSettledResult<void>[]> | undefined;
|
|
88
75
|
writeSplitCss: (sheet: Stylesheet) => Promise<void>;
|
|
89
76
|
watchConfig: (cb: (file: string) => void | Promise<void>, opts?: Omit<WatchOptions, "include">) => void;
|
|
@@ -137,6 +124,26 @@ declare class Builder {
|
|
|
137
124
|
extract: () => void;
|
|
138
125
|
isValidRoot: (root: Root) => boolean;
|
|
139
126
|
write: (root: Root) => void;
|
|
127
|
+
/**
|
|
128
|
+
* The finished stylesheet, as a string.
|
|
129
|
+
*
|
|
130
|
+
* The same sheet `write` injects into a postcss root, for callers that want the css
|
|
131
|
+
* rather than a mutated root -- the vite plugin serves it as a virtual module. Both go
|
|
132
|
+
* through here so a build cannot depend on which integration asked for it.
|
|
133
|
+
*
|
|
134
|
+
* `layerParams` is the one thing that differs between them, and it is not cosmetic: the
|
|
135
|
+
* `@layer a, b, c;` statement is what fixes layer *order*, and css layers are ordered by
|
|
136
|
+
* first appearance otherwise. `write` leaves it out because the root it appends to is a
|
|
137
|
+
* file that already declares it -- that declaration is what `isValidRoot` matches on. A
|
|
138
|
+
* virtual module is the whole stylesheet and has nothing to inherit it from.
|
|
139
|
+
*
|
|
140
|
+
* `extract` has to have run first: this reads the encoder rather than filling it.
|
|
141
|
+
*/
|
|
142
|
+
toCss: ({
|
|
143
|
+
layerParams
|
|
144
|
+
}?: {
|
|
145
|
+
layerParams?: boolean;
|
|
146
|
+
}) => string;
|
|
140
147
|
registerDependency: (fn: (dep: Message) => void) => void;
|
|
141
148
|
}
|
|
142
149
|
interface FileMeta {
|
package/dist/index.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { pluginSvelte } from "@bamboocss/plugin-svelte";
|
|
|
9
9
|
import { pluginVue } from "@bamboocss/plugin-vue";
|
|
10
10
|
import browserslist from "browserslist";
|
|
11
11
|
import { checkNamingAgreement, formatNamingDisagreement, messages } from "@bamboocss/core";
|
|
12
|
-
import {
|
|
12
|
+
import { Generator } from "@bamboocss/generator";
|
|
13
13
|
import { Project } from "@bamboocss/parser";
|
|
14
14
|
import { debounce } from "perfect-debounce";
|
|
15
15
|
import process$1 from "node:process";
|
|
@@ -119,7 +119,6 @@ async function codegen(ctx, ids) {
|
|
|
119
119
|
}
|
|
120
120
|
const promises = artifacts.map((artifact) => limit(() => ctx.output.write(artifact)));
|
|
121
121
|
await Promise.allSettled(promises);
|
|
122
|
-
await ctx.writeGroupRegistry();
|
|
123
122
|
await ctx.hooks["codegen:done"]?.({ changed: ids });
|
|
124
123
|
return {
|
|
125
124
|
box: ctx.initMessage(),
|
|
@@ -1591,9 +1590,7 @@ var OutputEngine = class {
|
|
|
1591
1590
|
const unresolvedReasons = {
|
|
1592
1591
|
"unresolvable-value": (prop) => [`${prop} will not reach the stylesheet because its value is not statically known`, "Make the value static to group it."],
|
|
1593
1592
|
"missing-property": (prop) => [`${prop} will not reach the stylesheet because its value could not be evaluated at build time`, "Make the value static to group it."],
|
|
1594
|
-
"unenumerable-keys": () => ["an object spread or computed key leaves the build unable to tell which properties this call sets", "Write the properties out, or spread a value the build can resolve, to group it."]
|
|
1595
|
-
"ambiguous-merge": () => ["two of these arguments set the same property, and the build cannot tell a merge from a pair of alternatives", "Merge them into a single object to group it."],
|
|
1596
|
-
"too-many-combinations": () => ["this call has more conditional branches than the build will enumerate", "Split it into fewer conditional properties to group it."]
|
|
1593
|
+
"unenumerable-keys": () => ["an object spread or computed key leaves the build unable to tell which properties this call sets", "Write the properties out, or spread a value the build can resolve, to group it."]
|
|
1597
1594
|
};
|
|
1598
1595
|
var BambooContext = class extends Generator {
|
|
1599
1596
|
runtime;
|
|
@@ -1622,15 +1619,13 @@ var BambooContext = class extends Generator {
|
|
|
1622
1619
|
this.explicitDeps = this.getExplicitDependencies();
|
|
1623
1620
|
const disagreement = checkNamingAgreement(this);
|
|
1624
1621
|
if (disagreement) throw new BambooError("NAMING_DISAGREEMENT", formatNamingDisagreement(disagreement));
|
|
1625
|
-
if (config.cssMode === "grouped" && config.staticCss?.css?.length) logger.warn("config", "`staticCss.css` does not back runtime `css()` calls under `cssMode: 'grouped'`. It pre-generates one rule per property and value, while a grouped class names a whole call. The rules are still emitted, but nothing the runtime returns will match them — see https://bamboocss.com/docs/references/config#cssmode");
|
|
1626
1622
|
}
|
|
1627
1623
|
/**
|
|
1628
1624
|
* Report `css()` calls whose styles the build could not fully see.
|
|
1629
1625
|
*
|
|
1630
|
-
*
|
|
1631
|
-
*
|
|
1632
|
-
*
|
|
1633
|
-
* than merely losing a declaration, so it must not be silent.
|
|
1626
|
+
* A warning rather than an error: the build is not wrong, the call site is unresolvable,
|
|
1627
|
+
* and the declarations it did resolve still apply. But the ones it could not have no rule
|
|
1628
|
+
* behind them and are simply absent, so it must not be silent.
|
|
1634
1629
|
*/
|
|
1635
1630
|
reportUnresolvedStyles = (result) => {
|
|
1636
1631
|
const unresolved = result.unresolved;
|
|
@@ -1638,18 +1633,16 @@ var BambooContext = class extends Generator {
|
|
|
1638
1633
|
for (const entry of unresolved) {
|
|
1639
1634
|
const where = `${entry.filePath}:${entry.line}:${entry.column}`;
|
|
1640
1635
|
const prop = entry.prop ? `\`${entry.prop}\`` : "a property";
|
|
1641
|
-
const [what
|
|
1636
|
+
const [what] = unresolvedReasons[entry.reason](prop);
|
|
1642
1637
|
if (entry.kind === "recipe") {
|
|
1643
1638
|
const at = entry.prop ? ` at \`${entry.prop}\`` : "";
|
|
1644
1639
|
logger.warn("recipe", `${where} — ${what}${at}. A recipe's classes are named from a hash of its config, so a declaration the build cannot see gives the build and the browser different names and the element renders with no styles at all. Set \`className\` on the recipe, so its name does not depend on what the build could resolve. See https://bamboocss.com/docs/concepts/recipes`);
|
|
1645
1640
|
continue;
|
|
1646
1641
|
}
|
|
1647
|
-
|
|
1642
|
+
{
|
|
1648
1643
|
const at = entry.prop ? ` at \`${entry.prop}\`` : "";
|
|
1649
1644
|
logger.warn("css", `${where} — ${what}${at}. The build emits a rule per declaration it can see, and the runtime names a class for every declaration the object actually has — so the ones it could not see have no rule behind them and are simply absent. Write the value out, or generate it with \`staticCss\` if it is genuinely dynamic. See https://bamboocss.com/docs/guides/dynamic-styling`);
|
|
1650
|
-
continue;
|
|
1651
1645
|
}
|
|
1652
|
-
logger.warn("grouped", `${where} — ${what}. Under \`cssMode: 'grouped'\` one class names the whole \`css()\` call, so this call cannot use one: it falls back to naming each declaration separately and keeps only the ones the build could resolve. ${fix} See https://bamboocss.com/docs/references/config#cssmode`);
|
|
1653
1646
|
}
|
|
1654
1647
|
};
|
|
1655
1648
|
getExplicitDependencies = () => {
|
|
@@ -1703,44 +1696,12 @@ var BambooContext = class extends Generator {
|
|
|
1703
1696
|
filesWithCss.push(file);
|
|
1704
1697
|
results.push(result);
|
|
1705
1698
|
});
|
|
1706
|
-
this.writeGroupRegistry();
|
|
1707
1699
|
return {
|
|
1708
1700
|
filesWithCss,
|
|
1709
1701
|
files,
|
|
1710
1702
|
results
|
|
1711
1703
|
};
|
|
1712
1704
|
};
|
|
1713
|
-
/**
|
|
1714
|
-
* Refresh the group registry the generated `css()` consults.
|
|
1715
|
-
*
|
|
1716
|
-
* `codegen` emits it empty — it runs on config change, before anything is extracted — so
|
|
1717
|
-
* this is the pass that fills it in. Called after extraction rather than alongside the
|
|
1718
|
-
* CSS, because the PostCSS plugin never writes a stylesheet to disk at all and would
|
|
1719
|
-
* otherwise never get one.
|
|
1720
|
-
*
|
|
1721
|
-
* Safe to run late or not at all: the runtime *adds* atomic names to the group class
|
|
1722
|
-
* rather than replacing it, so a stale registry costs a class that matches nothing.
|
|
1723
|
-
*/
|
|
1724
|
-
writeGroupRegistry = () => {
|
|
1725
|
-
if (this.config.cssMode !== "grouped") return;
|
|
1726
|
-
const names = this.getGroupRegistry();
|
|
1727
|
-
if (!names.length) {
|
|
1728
|
-
const existing = this.runtime.path.join(...this.paths.css, this.file.ext(GROUP_REGISTRY_FILE));
|
|
1729
|
-
if (this.runtime.fs.existsSync(existing)) return;
|
|
1730
|
-
}
|
|
1731
|
-
const registry = generateGroupRegistry(this, names);
|
|
1732
|
-
return this.output.write({
|
|
1733
|
-
id: "css-fn",
|
|
1734
|
-
dir: this.paths.css,
|
|
1735
|
-
files: [{
|
|
1736
|
-
file: this.file.ext(GROUP_REGISTRY_FILE),
|
|
1737
|
-
code: registry.js
|
|
1738
|
-
}, {
|
|
1739
|
-
file: this.file.extDts(GROUP_REGISTRY_FILE),
|
|
1740
|
-
code: registry.dts
|
|
1741
|
-
}]
|
|
1742
|
-
});
|
|
1743
|
-
};
|
|
1744
1705
|
writeCss = (sheet) => {
|
|
1745
1706
|
logger.info("css", this.runtime.path.join(...this.paths.root, "styles.css"));
|
|
1746
1707
|
return this.output.write({
|
|
@@ -2147,6 +2108,39 @@ function parseDependency(fileOrGlob) {
|
|
|
2147
2108
|
*/
|
|
2148
2109
|
const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2149
2110
|
/**
|
|
2111
|
+
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2112
|
+
* parser understands it.
|
|
2113
|
+
*
|
|
2114
|
+
* Both, because neither alone is complete and every scan below is safe when over-fed. A file
|
|
2115
|
+
* the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`), and
|
|
2116
|
+
* those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2117
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2118
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2119
|
+
* like that reports no tokens and no elements at all.
|
|
2120
|
+
*
|
|
2121
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2122
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2123
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2124
|
+
*
|
|
2125
|
+
* So the cost is one extra read per file, and a second regex pass over the files a transform
|
|
2126
|
+
* actually changed. Measured at ~14ms for 806 files, against a build where css emission alone
|
|
2127
|
+
* is an order of magnitude more.
|
|
2128
|
+
*/
|
|
2129
|
+
function* sourceTexts(ctx) {
|
|
2130
|
+
for (const file of ctx.getFiles()) {
|
|
2131
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2132
|
+
let onDisk;
|
|
2133
|
+
try {
|
|
2134
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2135
|
+
} catch {
|
|
2136
|
+
onDisk = void 0;
|
|
2137
|
+
}
|
|
2138
|
+
const parsed = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2139
|
+
if (onDisk != null) yield onDisk;
|
|
2140
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
/**
|
|
2150
2144
|
* Collect token references that reading the generated css cannot reveal.
|
|
2151
2145
|
*
|
|
2152
2146
|
* This is deliberately textual and therefore over-inclusive: a match inside a comment or
|
|
@@ -2159,22 +2153,30 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2159
2153
|
* encode every style a second time — pass none, and stay correct only because a path the
|
|
2160
2154
|
* scan misses can lose nothing: `token()` hands javascript a literal for exactly the
|
|
2161
2155
|
* tokens this cannot see, and hands it a `var()` only for virtual, conditional and
|
|
2162
|
-
* negative tokens, which `getAlwaysKeptTokenVars` keeps
|
|
2163
|
-
*
|
|
2164
|
-
*
|
|
2156
|
+
* negative tokens, which `getAlwaysKeptTokenVars` keeps.
|
|
2157
|
+
*
|
|
2158
|
+
* Those blanket keeps are no longer unconditional — `tokensReachableFromJs` gates them — and
|
|
2159
|
+
* this comment used to warn that narrowing them would break the argument above. It does not,
|
|
2160
|
+
* for a reason worth stating rather than rediscovering: the gate matches `token(` regardless
|
|
2161
|
+
* of what follows, where `TOKEN_CALL` below needs a string literal. So the one shape this
|
|
2162
|
+
* scan cannot resolve, a path built from a constant, is exactly the shape that turns the
|
|
2163
|
+
* gate on and restores every blanket keep. The two failures line up, which is what makes the
|
|
2164
|
+
* redundancy hold.
|
|
2165
|
+
*
|
|
2166
|
+
* That alignment is a real coupling, not an observation: the gate's call pattern has to stay
|
|
2167
|
+
* a superset of this one, matching wherever `TOKEN_CALL` matches and also wherever it gives
|
|
2168
|
+
* up. It briefly was not — this allowed whitespace around the `.` of `token.var` and the gate
|
|
2169
|
+
* did not, so a formatter wrapping `token\n .var(SOME_CONST)` slipped past both. Change one
|
|
2170
|
+
* of the two and change the other; `token-references.test.ts` pins the property directly.
|
|
2171
|
+
*
|
|
2172
|
+
* Where it stops holding is a binding renamed away from `token` — `const t = token`, then
|
|
2173
|
+
* `t('spacing.4')` — which matches neither. `token-references.test.ts` pins both halves.
|
|
2165
2174
|
*/
|
|
2166
2175
|
function collectTokenReferences(ctx, results) {
|
|
2167
2176
|
const paths = /* @__PURE__ */ new Set();
|
|
2168
2177
|
const vars = /* @__PURE__ */ new Set();
|
|
2169
2178
|
for (const result of results) for (const item of result.token) for (const value of item.data ?? []) if (typeof value === "string") paths.add(value);
|
|
2170
|
-
for (const
|
|
2171
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2172
|
-
let content = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2173
|
-
if (content == null) try {
|
|
2174
|
-
content = ctx.runtime.fs.readFileSync(filePath);
|
|
2175
|
-
} catch {
|
|
2176
|
-
continue;
|
|
2177
|
-
}
|
|
2179
|
+
for (const content of sourceTexts(ctx)) {
|
|
2178
2180
|
for (const match of content.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2179
2181
|
for (const name of cssVarRefs(content)) vars.add(name);
|
|
2180
2182
|
}
|
|
@@ -2204,22 +2206,80 @@ function collectKeyframeReferences(ctx, names) {
|
|
|
2204
2206
|
const found = /* @__PURE__ */ new Set();
|
|
2205
2207
|
if (!declared.length) return found;
|
|
2206
2208
|
const patterns = declared.map((name) => [name, new RegExp(`\\b${escapeRegExp(name)}\\b`)]);
|
|
2207
|
-
for (const
|
|
2208
|
-
if (found.size === declared.length) break;
|
|
2209
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2210
|
-
let content = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2211
|
-
if (content == null) try {
|
|
2212
|
-
content = ctx.runtime.fs.readFileSync(filePath);
|
|
2213
|
-
} catch {
|
|
2214
|
-
continue;
|
|
2215
|
-
}
|
|
2209
|
+
for (const content of sourceTexts(ctx)) {
|
|
2216
2210
|
for (const [name, pattern] of patterns) if (!found.has(name) && pattern.test(content)) found.add(name);
|
|
2211
|
+
if (found.size === declared.length) break;
|
|
2217
2212
|
}
|
|
2218
2213
|
return found;
|
|
2219
2214
|
}
|
|
2220
2215
|
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2221
2216
|
/** The keyframes the theme declares — the allow-list `pruneKeyframes` works against. */
|
|
2222
2217
|
const keyframeNames = (ctx) => Object.keys(ctx.config.theme?.keyframes ?? {});
|
|
2218
|
+
/**
|
|
2219
|
+
* The HTML element names the source renders.
|
|
2220
|
+
*
|
|
2221
|
+
* A textual scan for an opening tag, over the same files the other collectors read. Matching
|
|
2222
|
+
* `<tag` rather than parsing: a template can spell an element in more ways than a parser of
|
|
2223
|
+
* any one framework's syntax would find, and over-reporting an element only keeps a reset
|
|
2224
|
+
* rule that would otherwise go.
|
|
2225
|
+
*
|
|
2226
|
+
* Lowercase-initial only, so a JSX component (`<Button />`) is not mistaken for an element.
|
|
2227
|
+
* That cuts the other way too — a component rendering `<button>` inside a dependency is
|
|
2228
|
+
* invisible here, which is why `prunePreflight` is opt-in.
|
|
2229
|
+
*
|
|
2230
|
+
* The commoner blind spot is nearer than a dependency: this reads `include`, and `include`
|
|
2231
|
+
* conventionally covers components rather than markup. An entry template — `index.html`,
|
|
2232
|
+
* `app.html` — is where `<table>`, `<noscript>` and a page's static markup usually live, and a
|
|
2233
|
+
* glob rooted at `./src` does not match it, so every element appearing only there loses its
|
|
2234
|
+
* reset. Nothing here can detect that; the file simply is not in the list. Listing it in
|
|
2235
|
+
* `include` fixes it, because this reads whatever `include` covers rather than only what the
|
|
2236
|
+
* parser understands — `token-references.test.ts` pins both halves.
|
|
2237
|
+
*
|
|
2238
|
+
* Reading the raw file as well as the parsed copy is what makes an SFC work here; see
|
|
2239
|
+
* `sourceTexts`. On a healthy `.svelte` file the two agree on everything but `<script>`, so
|
|
2240
|
+
* the raw read earns its place only when a transform fails -- and there it is the difference
|
|
2241
|
+
* between the file's elements and none of them.
|
|
2242
|
+
*/
|
|
2243
|
+
function collectRenderedElements(ctx) {
|
|
2244
|
+
const found = /* @__PURE__ */ new Set();
|
|
2245
|
+
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2246
|
+
return found;
|
|
2247
|
+
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Whether any source file reaches for a token from javascript.
|
|
2250
|
+
*
|
|
2251
|
+
* The tokens artifact is generated into the project rather than installed, so the import is
|
|
2252
|
+
* written in the project's own source and a scan of `include` sees it. When this comes back
|
|
2253
|
+
* false, the declarations kept purely so `token()` can answer have no caller to answer.
|
|
2254
|
+
*
|
|
2255
|
+
* Deliberately loose, and loose in a specific direction: a false positive keeps a declaration
|
|
2256
|
+
* nothing reads, a false negative returns a `var()` nothing declares.
|
|
2257
|
+
*
|
|
2258
|
+
* So the import test is any module specifier with a `/tokens` path segment, rather than the
|
|
2259
|
+
* literal `styled-system/tokens` it used to be. `outdir` is configurable, so the artifact is
|
|
2260
|
+
* only at `styled-system/` by default, and the literal missed `outdir: 'design-system'`, a
|
|
2261
|
+
* tsconfig path alias, and `styled-system/tokens/index.mjs` -- which is the only spelling
|
|
2262
|
+
* NodeNext accepts, the artifact being a directory. It is still anchored to `from`, `import`
|
|
2263
|
+
* or `require`, because without that anchor a route or a url (`fetch('/api/tokens')`) reads
|
|
2264
|
+
* as an import and quietly switches the whole optimisation off.
|
|
2265
|
+
*
|
|
2266
|
+
* The call test allows whitespace around the dot for the same reason `TOKEN_CALL` does. It
|
|
2267
|
+
* did not, which broke the alignment the note on `collectTokenReferences` rests on: a
|
|
2268
|
+
* formatter wrapping `token\n .var(SOME_CONST)` was invisible to that scan *and* to this
|
|
2269
|
+
* gate *and* to the parser, whose callee is a property access. A comment in the same position
|
|
2270
|
+
* -- `token/*x*\/.var(` -- is still invisible to all three.
|
|
2271
|
+
*
|
|
2272
|
+
* One shape it still does not see, pinned in `token-references.test.ts`: a binding renamed
|
|
2273
|
+
* away from `token`, as in `const t = token`. Also unseen is a caller outside `include`,
|
|
2274
|
+
* which scopes style extraction rather than everything that may import — a script, a config,
|
|
2275
|
+
* or a sibling workspace package. Both prune declarations the running app then asks for, and
|
|
2276
|
+
* neither reports itself; `pruneUnusedTokens: false` is the way out.
|
|
2277
|
+
*/
|
|
2278
|
+
function tokensReachableFromJs(ctx) {
|
|
2279
|
+
const pattern = /\btoken(\s*\.\s*var)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2280
|
+
for (const content of sourceTexts(ctx)) if (pattern.test(content)) return true;
|
|
2281
|
+
return false;
|
|
2282
|
+
}
|
|
2223
2283
|
//#endregion
|
|
2224
2284
|
//#region src/builder.ts
|
|
2225
2285
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2365,7 +2425,6 @@ var Builder = class {
|
|
|
2365
2425
|
const files = ctx.getFiles();
|
|
2366
2426
|
const done = logger.time.info("Extracted in");
|
|
2367
2427
|
files.map((file) => this.extractFile(ctx, file));
|
|
2368
|
-
ctx.writeGroupRegistry();
|
|
2369
2428
|
done();
|
|
2370
2429
|
};
|
|
2371
2430
|
isValidRoot = (root) => {
|
|
@@ -2381,14 +2440,33 @@ var Builder = class {
|
|
|
2381
2440
|
logger.warn("postcss", "Generated css is already present in this file, so nothing was injected. It is imported and generated here at once — keep the `@import` of `styles.css` or the `@layer` statement that the postcss plugin injects at, not both.");
|
|
2382
2441
|
return;
|
|
2383
2442
|
}
|
|
2443
|
+
root.append(this.toCss());
|
|
2444
|
+
};
|
|
2445
|
+
/**
|
|
2446
|
+
* The finished stylesheet, as a string.
|
|
2447
|
+
*
|
|
2448
|
+
* The same sheet `write` injects into a postcss root, for callers that want the css
|
|
2449
|
+
* rather than a mutated root -- the vite plugin serves it as a virtual module. Both go
|
|
2450
|
+
* through here so a build cannot depend on which integration asked for it.
|
|
2451
|
+
*
|
|
2452
|
+
* `layerParams` is the one thing that differs between them, and it is not cosmetic: the
|
|
2453
|
+
* `@layer a, b, c;` statement is what fixes layer *order*, and css layers are ordered by
|
|
2454
|
+
* first appearance otherwise. `write` leaves it out because the root it appends to is a
|
|
2455
|
+
* file that already declares it -- that declaration is what `isValidRoot` matches on. A
|
|
2456
|
+
* virtual module is the whole stylesheet and has nothing to inherit it from.
|
|
2457
|
+
*
|
|
2458
|
+
* `extract` has to have run first: this reads the encoder rather than filling it.
|
|
2459
|
+
*/
|
|
2460
|
+
toCss = ({ layerParams = false } = {}) => {
|
|
2384
2461
|
const ctx = this.getContextOrThrow();
|
|
2385
2462
|
const sheet = ctx.createSheet();
|
|
2463
|
+
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2386
2464
|
ctx.appendBaselineCss(sheet);
|
|
2387
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []));
|
|
2465
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []), tokensReachableFromJs(ctx));
|
|
2388
2466
|
else ctx.pruneTokens(sheet);
|
|
2467
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2389
2468
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2390
|
-
|
|
2391
|
-
root.append(css);
|
|
2469
|
+
return ctx.getCss(sheet);
|
|
2392
2470
|
};
|
|
2393
2471
|
registerDependency = (fn) => {
|
|
2394
2472
|
const ctx = this.getContextOrThrow();
|
|
@@ -2475,6 +2553,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2475
2553
|
if (type) {
|
|
2476
2554
|
const done = logger.time.info(ctx.messages.cssArtifactComplete(type));
|
|
2477
2555
|
ctx.appendCssOfType(type, sheet);
|
|
2556
|
+
if (type === "preflight" && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2478
2557
|
if (outfile) {
|
|
2479
2558
|
const css = ctx.getCss(sheet);
|
|
2480
2559
|
logger.info("css", ctx.runtime.path.resolve(outfile));
|
|
@@ -2489,8 +2568,9 @@ const cssgen = async (ctx, options) => {
|
|
|
2489
2568
|
ctx.appendBaselineCss(sheet);
|
|
2490
2569
|
}
|
|
2491
2570
|
ctx.appendParserCss(sheet);
|
|
2492
|
-
if (!minimal && ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, results));
|
|
2571
|
+
if (!minimal && ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, results), tokensReachableFromJs(ctx));
|
|
2493
2572
|
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2573
|
+
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2494
2574
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2495
2575
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
2496
2576
|
else if (outfile) {
|
|
@@ -2561,8 +2641,9 @@ async function build(ctx, artifactIds) {
|
|
|
2561
2641
|
ctx.appendBaselineCss(sheet);
|
|
2562
2642
|
const parsed = ctx.parseFiles();
|
|
2563
2643
|
ctx.appendParserCss(sheet);
|
|
2564
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, parsed.results));
|
|
2644
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, parsed.results), tokensReachableFromJs(ctx));
|
|
2565
2645
|
else ctx.pruneTokens(sheet);
|
|
2646
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2566
2647
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2567
2648
|
await ctx.writeCss(sheet);
|
|
2568
2649
|
done(ctx.messages.buildComplete(parsed.files.length));
|
|
@@ -2607,11 +2688,11 @@ async function generate(config, configPath) {
|
|
|
2607
2688
|
ctx.appendLayerParams(sheet);
|
|
2608
2689
|
ctx.appendBaselineCss(sheet);
|
|
2609
2690
|
ctx.appendParserCss(sheet);
|
|
2610
|
-
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []));
|
|
2691
|
+
if (ctx.config.pruneUnusedTokens) ctx.pruneTokens(sheet, collectTokenReferences(ctx, []), tokensReachableFromJs(ctx));
|
|
2692
|
+
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2611
2693
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2612
2694
|
const css = ctx.getCss(sheet);
|
|
2613
2695
|
await ctx.runtime.fs.writeFile(outfile, css);
|
|
2614
|
-
await ctx.writeGroupRegistry();
|
|
2615
2696
|
done();
|
|
2616
2697
|
};
|
|
2617
2698
|
ctx.watchFiles(async (event, file) => {
|
|
@@ -2710,7 +2791,7 @@ export default defineConfig({
|
|
|
2710
2791
|
exclude: [],
|
|
2711
2792
|
|
|
2712
2793
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2713
|
-
// by default — worth
|
|
2794
|
+
// by default — worth 36-78% of a new project's stylesheet. Set \`pruneUnusedTokens: false\`
|
|
2714
2795
|
// if you read tokens from somewhere the build cannot see them — \`token.var()\` with a
|
|
2715
2796
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2716
2797
|
// \`staticCss\` to keep them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bamboocss/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.0",
|
|
4
4
|
"description": "The core css bamboo library",
|
|
5
5
|
"homepage": "https://bamboocss.com",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,18 +52,18 @@
|
|
|
52
52
|
"postcss": "8.5.26",
|
|
53
53
|
"ts-morph": "28.0.0",
|
|
54
54
|
"ts-pattern": "5.9.0",
|
|
55
|
-
"@bamboocss/config": "1.
|
|
56
|
-
"@bamboocss/core": "1.
|
|
57
|
-
"@bamboocss/generator": "1.
|
|
58
|
-
"@bamboocss/logger": "1.
|
|
59
|
-
"@bamboocss/parser": "1.
|
|
60
|
-
"@bamboocss/plugin-lightningcss": "1.
|
|
61
|
-
"@bamboocss/plugin-svelte": "1.
|
|
62
|
-
"@bamboocss/plugin-vue": "1.
|
|
63
|
-
"@bamboocss/
|
|
64
|
-
"@bamboocss/
|
|
65
|
-
"@bamboocss/
|
|
66
|
-
"@bamboocss/
|
|
55
|
+
"@bamboocss/config": "1.22.0",
|
|
56
|
+
"@bamboocss/core": "1.22.0",
|
|
57
|
+
"@bamboocss/generator": "1.22.0",
|
|
58
|
+
"@bamboocss/logger": "1.22.0",
|
|
59
|
+
"@bamboocss/parser": "1.22.0",
|
|
60
|
+
"@bamboocss/plugin-lightningcss": "1.22.0",
|
|
61
|
+
"@bamboocss/plugin-svelte": "1.22.0",
|
|
62
|
+
"@bamboocss/plugin-vue": "1.22.0",
|
|
63
|
+
"@bamboocss/reporter": "1.22.0",
|
|
64
|
+
"@bamboocss/shared": "1.22.0",
|
|
65
|
+
"@bamboocss/token-dictionary": "1.22.0",
|
|
66
|
+
"@bamboocss/types": "1.22.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/fs-extra": "11.0.4",
|