@bamboocss/node 1.28.1 → 1.29.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 +331 -92
- package/dist/index.mjs +331 -92
- package/package.json +13 -13
package/dist/index.cjs
CHANGED
|
@@ -59,6 +59,8 @@ let is_glob = require("is-glob");
|
|
|
59
59
|
is_glob = __toESM(is_glob);
|
|
60
60
|
let glob_parent = require("glob-parent");
|
|
61
61
|
glob_parent = __toESM(glob_parent);
|
|
62
|
+
let _bamboocss_config_ts_path = require("@bamboocss/config/ts-path");
|
|
63
|
+
let ts_morph = require("ts-morph");
|
|
62
64
|
let node_readline = require("node:readline");
|
|
63
65
|
node_readline = __toESM(node_readline);
|
|
64
66
|
let look_it_up = require("look-it-up");
|
|
@@ -1455,6 +1457,25 @@ function globDirname(globs) {
|
|
|
1455
1457
|
}
|
|
1456
1458
|
//#endregion
|
|
1457
1459
|
//#region src/node-runtime.ts
|
|
1460
|
+
/**
|
|
1461
|
+
* What the source glob ignores.
|
|
1462
|
+
*
|
|
1463
|
+
* `**\/*.d.ts` is unconditional. It used to be a *default* that a user's own `exclude`
|
|
1464
|
+
* replaced, so declaration files were scanned by whether the project happened to set that
|
|
1465
|
+
* option at all — excluded for `exclude: []`, included for `exclude: ['**\/*.stories.tsx']`.
|
|
1466
|
+
* Nothing chose that; the default was simply appended when nothing else was there.
|
|
1467
|
+
*
|
|
1468
|
+
* A declaration file carries no runtime code and can emit no styles. It is still read by the
|
|
1469
|
+
* reference scans, which are deliberately over-inclusive, so scanning one could keep a token
|
|
1470
|
+
* named in a doc comment or a reset rule for an element named in a JSDoc example. Those are
|
|
1471
|
+
* spurious keeps rather than a guarantee anyone relied on — and they were never available to
|
|
1472
|
+
* the projects on the other side of the same condition.
|
|
1473
|
+
*
|
|
1474
|
+
* Copied rather than appended to. `opts.exclude` is `ctx.config.exclude` itself, so pushing
|
|
1475
|
+
* onto it edited the user's resolved config in place, and the next call then saw a list it had
|
|
1476
|
+
* mutated.
|
|
1477
|
+
*/
|
|
1478
|
+
const globIgnore = (exclude) => ["**/*.d.ts", ...exclude ?? []];
|
|
1458
1479
|
const nodeRuntime = {
|
|
1459
1480
|
cwd() {
|
|
1460
1481
|
return process.cwd();
|
|
@@ -1481,11 +1502,9 @@ const nodeRuntime = {
|
|
|
1481
1502
|
},
|
|
1482
1503
|
glob(opts) {
|
|
1483
1504
|
if (!opts.include) return [];
|
|
1484
|
-
const ignore = opts.exclude ?? [];
|
|
1485
|
-
if (!ignore.length) ignore.push("**/*.d.ts");
|
|
1486
1505
|
return fast_glob.default.sync(opts.include, {
|
|
1487
1506
|
cwd: opts.cwd,
|
|
1488
|
-
ignore,
|
|
1507
|
+
ignore: globIgnore(opts.exclude),
|
|
1489
1508
|
absolute: true
|
|
1490
1509
|
});
|
|
1491
1510
|
},
|
|
@@ -2114,6 +2133,240 @@ function parseDependency(fileOrGlob) {
|
|
|
2114
2133
|
return message;
|
|
2115
2134
|
}
|
|
2116
2135
|
//#endregion
|
|
2136
|
+
//#region src/source-snapshots.ts
|
|
2137
|
+
/**
|
|
2138
|
+
* Every file `include` covers, read once.
|
|
2139
|
+
*
|
|
2140
|
+
* The scans that decide what survives pruning — the token reference set, the reachability
|
|
2141
|
+
* gate, the strict accounting — all want the same two copies of the same files, and each used
|
|
2142
|
+
* to fetch them itself. A strict build therefore read every file three times over: once to
|
|
2143
|
+
* collect references, once to account, and once more for the gate whenever the accounting
|
|
2144
|
+
* declined. Yielding a snapshot lets one walk feed all of them.
|
|
2145
|
+
*
|
|
2146
|
+
* Both copies, because neither alone is complete and the text scans are safe when over-fed. A
|
|
2147
|
+
* file the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`),
|
|
2148
|
+
* and those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2149
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2150
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2151
|
+
* like that reports no tokens and no elements at all.
|
|
2152
|
+
*
|
|
2153
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2154
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2155
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2156
|
+
*/
|
|
2157
|
+
function* sourceSnapshots(ctx) {
|
|
2158
|
+
for (const file of ctx.getFiles()) {
|
|
2159
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2160
|
+
let onDisk;
|
|
2161
|
+
try {
|
|
2162
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2163
|
+
} catch {
|
|
2164
|
+
onDisk = void 0;
|
|
2165
|
+
}
|
|
2166
|
+
const sourceFile = ctx.project.getSourceFile(filePath);
|
|
2167
|
+
yield {
|
|
2168
|
+
filePath,
|
|
2169
|
+
onDisk,
|
|
2170
|
+
parsed: sourceFile?.getFullText(),
|
|
2171
|
+
sourceFile
|
|
2172
|
+
};
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
/**
|
|
2176
|
+
* The distinct texts a snapshot carries.
|
|
2177
|
+
*
|
|
2178
|
+
* One when the parser holds the file as written, two when a transform rewrote it. Every
|
|
2179
|
+
* textual scan wants both, and wants neither twice.
|
|
2180
|
+
*/
|
|
2181
|
+
function* snapshotTexts(snapshot) {
|
|
2182
|
+
const { onDisk, parsed } = snapshot;
|
|
2183
|
+
if (onDisk != null) yield onDisk;
|
|
2184
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2185
|
+
}
|
|
2186
|
+
//#endregion
|
|
2187
|
+
//#region src/token-accounting.ts
|
|
2188
|
+
/**
|
|
2189
|
+
* One file's contribution, split out so the build can account and scan in a single walk.
|
|
2190
|
+
*
|
|
2191
|
+
* `pruneTokensForBuild` needs the keep set, the reachability answer and this accounting from
|
|
2192
|
+
* the same files; three separate passes read every file three times.
|
|
2193
|
+
*/
|
|
2194
|
+
function accountSnapshot(ctx, snapshot, accounting) {
|
|
2195
|
+
const { filePath, onDisk, parsed, sourceFile } = snapshot;
|
|
2196
|
+
const { paths, declined } = accounting;
|
|
2197
|
+
if (onDisk == null || parsed == null || parsed !== onDisk) {
|
|
2198
|
+
if (!((onDisk?.includes("token") ?? false) || (parsed?.includes("token") ?? false))) return;
|
|
2199
|
+
declined.push({
|
|
2200
|
+
filePath,
|
|
2201
|
+
line: 1,
|
|
2202
|
+
reason: onDisk == null || parsed == null ? "unreadable" : "transformed"
|
|
2203
|
+
});
|
|
2204
|
+
return;
|
|
2205
|
+
}
|
|
2206
|
+
if (parseErrorCount(sourceFile)) {
|
|
2207
|
+
if (!parsed.includes("token")) return;
|
|
2208
|
+
declined.push({
|
|
2209
|
+
filePath,
|
|
2210
|
+
line: 1,
|
|
2211
|
+
reason: "unparsed"
|
|
2212
|
+
});
|
|
2213
|
+
return;
|
|
2214
|
+
}
|
|
2215
|
+
accountFile(ctx, sourceFile, filePath, paths, declined);
|
|
2216
|
+
}
|
|
2217
|
+
/**
|
|
2218
|
+
* How many syntax errors the parse produced.
|
|
2219
|
+
*
|
|
2220
|
+
* `parseDiagnostics` is TypeScript-internal and absent from the public `SourceFile` type, so
|
|
2221
|
+
* it is reached through a cast. The alternative, `getPreEmitDiagnostics`, runs the type checker
|
|
2222
|
+
* over the whole program — orders of magnitude more work for a question about syntax, and it
|
|
2223
|
+
* would report type errors this pass has no business declining over.
|
|
2224
|
+
*/
|
|
2225
|
+
const parseErrorCount = (sourceFile) => sourceFile.compilerNode.parseDiagnostics?.length ?? 0;
|
|
2226
|
+
/** Whether a module specifier resolves to the generated tokens artifact. */
|
|
2227
|
+
const isTokensEntrypoint = (ctx, specifier) => {
|
|
2228
|
+
const mods = ctx.imports.value.tokens;
|
|
2229
|
+
if (mods.some((mod) => specifier.includes(mod))) return true;
|
|
2230
|
+
const pathMappings = ctx.conf.tsOptions?.pathMappings;
|
|
2231
|
+
if (!pathMappings) return false;
|
|
2232
|
+
const resolved = (0, _bamboocss_config_ts_path.resolveTsPathPattern)(pathMappings, specifier);
|
|
2233
|
+
if (!resolved) return false;
|
|
2234
|
+
return mods.some((mod) => resolved.includes(mod) || resolved === mod);
|
|
2235
|
+
};
|
|
2236
|
+
const lineOf = (node) => node.getSourceFile().getLineAndColumnAtPos(node.getStart()).line;
|
|
2237
|
+
function accountFile(ctx, sourceFile, filePath, paths, declined) {
|
|
2238
|
+
const decline = (node, reason) => declined.push({
|
|
2239
|
+
filePath,
|
|
2240
|
+
line: lineOf(node),
|
|
2241
|
+
reason
|
|
2242
|
+
});
|
|
2243
|
+
/** Local names bound to the artifact: the `token` export, and any namespace of it. */
|
|
2244
|
+
const bindings = /* @__PURE__ */ new Set();
|
|
2245
|
+
for (const statement of sourceFile.getStatements()) {
|
|
2246
|
+
if (ts_morph.Node.isExportDeclaration(statement)) {
|
|
2247
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2248
|
+
if (specifier && isTokensEntrypoint(ctx, specifier)) decline(statement, "re-exported");
|
|
2249
|
+
continue;
|
|
2250
|
+
}
|
|
2251
|
+
if (!ts_morph.Node.isImportDeclaration(statement)) continue;
|
|
2252
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2253
|
+
if (!specifier) continue;
|
|
2254
|
+
const clause = statement.getImportClause();
|
|
2255
|
+
if (!clause || statement.isTypeOnly()) continue;
|
|
2256
|
+
if (!isTokensEntrypoint(ctx, specifier)) {
|
|
2257
|
+
for (const named of clause.getNamedImports()) if (!named.isTypeOnly() && nameOf(named.getNameNode()) === "token") decline(named, "unclassified-import");
|
|
2258
|
+
const foreignDefault = clause.getDefaultImport();
|
|
2259
|
+
if (foreignDefault && nameOf(foreignDefault) === "token") decline(foreignDefault, "unclassified-import");
|
|
2260
|
+
const foreignNamespace = clause.getNamespaceImport();
|
|
2261
|
+
if (foreignNamespace && usesTokenMember(sourceFile, foreignNamespace.getText())) decline(foreignNamespace, "unclassified-import");
|
|
2262
|
+
continue;
|
|
2263
|
+
}
|
|
2264
|
+
const defaultImport = clause.getDefaultImport();
|
|
2265
|
+
if (defaultImport) decline(defaultImport, "unsupported-import");
|
|
2266
|
+
const namespace = clause.getNamespaceImport();
|
|
2267
|
+
if (namespace) bindings.add(namespace.getText());
|
|
2268
|
+
for (const named of clause.getNamedImports()) {
|
|
2269
|
+
if (named.isTypeOnly()) continue;
|
|
2270
|
+
if (nameOf(named.getNameNode()) !== "token") {
|
|
2271
|
+
decline(named, "unsupported-import");
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
bindings.add((named.getAliasNode() ?? named.getNameNode()).getText());
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
for (const importEquals of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.ImportEqualsDeclaration)) if (importEquals.getText().includes("token")) decline(importEquals, "import-equals");
|
|
2278
|
+
for (const call of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.CallExpression)) {
|
|
2279
|
+
const callee = call.getExpression();
|
|
2280
|
+
const isRequire = ts_morph.Node.isIdentifier(callee) && callee.getText() === "require";
|
|
2281
|
+
const isDynamicImport = callee.getKind() === ts_morph.SyntaxKind.ImportKeyword;
|
|
2282
|
+
if (!isRequire && !isDynamicImport) continue;
|
|
2283
|
+
const argument = call.getArguments()[0];
|
|
2284
|
+
if (!argument) continue;
|
|
2285
|
+
if (ts_morph.Node.isStringLiteral(argument) ? isTokensEntrypoint(ctx, argument.getLiteralValue()) : true) decline(call, isRequire ? "require" : "dynamic-import");
|
|
2286
|
+
}
|
|
2287
|
+
for (const identifier of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.Identifier)) {
|
|
2288
|
+
const text = nameOf(identifier);
|
|
2289
|
+
if (!bindings.has(text) && text !== "token") continue;
|
|
2290
|
+
const parent = identifier.getParent();
|
|
2291
|
+
if (ts_morph.Node.isPropertyAccessExpression(parent) && parent.getNameNode() === identifier) {
|
|
2292
|
+
const object = parent.getExpression();
|
|
2293
|
+
const objectName = ts_morph.Node.isIdentifier(object) ? object.getText() : void 0;
|
|
2294
|
+
if (objectName && bindings.has(objectName)) continue;
|
|
2295
|
+
decline(identifier, "unresolved-reference");
|
|
2296
|
+
continue;
|
|
2297
|
+
}
|
|
2298
|
+
if (ts_morph.Node.isPropertyAssignment(parent) && parent.getNameNode() === identifier) continue;
|
|
2299
|
+
const path = accountedPath(identifier);
|
|
2300
|
+
if (path === void 0) {
|
|
2301
|
+
decline(identifier, "unresolved-reference");
|
|
2302
|
+
continue;
|
|
2303
|
+
}
|
|
2304
|
+
paths.add(path);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
/**
|
|
2308
|
+
* Whether any `<namespace>.token` member access appears, for an unclassified namespace.
|
|
2309
|
+
*
|
|
2310
|
+
* Element access counts too: `ui['token'](k)` reaches the same export and is invisible to a
|
|
2311
|
+
* property-access-only scan.
|
|
2312
|
+
*/
|
|
2313
|
+
function usesTokenMember(sourceFile, namespace) {
|
|
2314
|
+
if (sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.PropertyAccessExpression).some((access) => access.getExpression().getText() === namespace && nameOf(access.getNameNode()) === "token")) return true;
|
|
2315
|
+
return sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.ElementAccessExpression).some((access) => {
|
|
2316
|
+
if (access.getExpression().getText() !== namespace) return false;
|
|
2317
|
+
const argument = access.getArgumentExpression();
|
|
2318
|
+
return argument != null && (!ts_morph.Node.isStringLiteral(argument) || argument.getLiteralValue() === "token");
|
|
2319
|
+
});
|
|
2320
|
+
}
|
|
2321
|
+
/**
|
|
2322
|
+
* An identifier's name as the compiler resolves it, not as it is spelled.
|
|
2323
|
+
*
|
|
2324
|
+
* `token` is the identifier `token`; reading `getText()` returns the escape and compares
|
|
2325
|
+
* unequal, which let an import of the artifact's export past the checks that key on the name.
|
|
2326
|
+
*/
|
|
2327
|
+
const nameOf = (node) => ts_morph.Node.isIdentifier(node) ? String(node.compilerNode.escapedText) : node.getText();
|
|
2328
|
+
/**
|
|
2329
|
+
* The token path one occurrence asks for, or `undefined` if it is not a call this can read.
|
|
2330
|
+
*
|
|
2331
|
+
* Accepts the callee position and nothing else — `token('x')`, `token.var('x')`,
|
|
2332
|
+
* `token.value('x')`, and the namespaced spellings. An identifier anywhere else is a value
|
|
2333
|
+
* escaping somewhere this pass cannot follow: assigned (`const t = token`), passed
|
|
2334
|
+
* (`useMemo(() => token)`), spread, or enumerated.
|
|
2335
|
+
*/
|
|
2336
|
+
function accountedPath(identifier) {
|
|
2337
|
+
const parent = identifier.getParent();
|
|
2338
|
+
if (!parent) return void 0;
|
|
2339
|
+
if (ts_morph.Node.isImportSpecifier(parent) || ts_morph.Node.isNamespaceImport(parent) || ts_morph.Node.isImportClause(parent)) return "";
|
|
2340
|
+
if (ts_morph.Node.isCallExpression(parent) && parent.getExpression() === identifier) return literalPath(parent);
|
|
2341
|
+
if (!ts_morph.Node.isPropertyAccessExpression(parent) || parent.getExpression() !== identifier) return void 0;
|
|
2342
|
+
const property = parent.getNameNode().getText();
|
|
2343
|
+
const grandParent = parent.getParent();
|
|
2344
|
+
if (property === "var" || property === "value") return ts_morph.Node.isCallExpression(grandParent) && grandParent.getExpression() === parent ? literalPath(grandParent) : void 0;
|
|
2345
|
+
if (property !== "token") return void 0;
|
|
2346
|
+
if (ts_morph.Node.isCallExpression(grandParent) && grandParent.getExpression() === parent) return literalPath(grandParent);
|
|
2347
|
+
if (ts_morph.Node.isPropertyAccessExpression(grandParent) && grandParent.getExpression() === parent) {
|
|
2348
|
+
const method = grandParent.getNameNode().getText();
|
|
2349
|
+
if (method !== "var" && method !== "value") return void 0;
|
|
2350
|
+
const call = grandParent.getParent();
|
|
2351
|
+
return ts_morph.Node.isCallExpression(call) && call.getExpression() === grandParent ? literalPath(call) : void 0;
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
/**
|
|
2355
|
+
* The path a call asks for, when the first argument is a literal.
|
|
2356
|
+
*
|
|
2357
|
+
* Read through `getLiteralValue()`, never off the source text. The text carries escapes —
|
|
2358
|
+
* `token('colors.red.300')` — and a path recorded raw looks up nothing, which
|
|
2359
|
+
* would accept a reference and keep no declaration for it. That is the exact failure this
|
|
2360
|
+
* module exists to make unrepresentable.
|
|
2361
|
+
*/
|
|
2362
|
+
function literalPath(call) {
|
|
2363
|
+
if (!ts_morph.Node.isCallExpression(call)) return void 0;
|
|
2364
|
+
const argument = call.getArguments()[0];
|
|
2365
|
+
if (!argument) return void 0;
|
|
2366
|
+
if (ts_morph.Node.isStringLiteral(argument)) return argument.getLiteralValue();
|
|
2367
|
+
if (ts_morph.Node.isNoSubstitutionTemplateLiteral(argument)) return argument.getLiteralValue();
|
|
2368
|
+
}
|
|
2369
|
+
//#endregion
|
|
2117
2370
|
//#region src/token-references.ts
|
|
2118
2371
|
/**
|
|
2119
2372
|
* `token.var('colors.red.300')` and `token('spacing.4')`, including the whitespace a
|
|
@@ -2123,7 +2376,15 @@ function parseDependency(fileOrGlob) {
|
|
|
2123
2376
|
* earns its place twice over: it covers a path built somewhere the extractor cannot
|
|
2124
2377
|
* follow, and it covers the callers below that supply no `results` at all.
|
|
2125
2378
|
*/
|
|
2126
|
-
const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2379
|
+
const TOKEN_CALL = /\btoken(?:\s*\.\s*(?:var|value))?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2380
|
+
/**
|
|
2381
|
+
* A token reached from javascript at all — a call of any shape, or an import of the artifact.
|
|
2382
|
+
*
|
|
2383
|
+
* Has to stay a strict superset of `TOKEN_CALL`: it must match wherever that one matches, and
|
|
2384
|
+
* also wherever that one gives up. See `collectTokenReferences` for why the redundancy holds.
|
|
2385
|
+
* No `g` flag, so `test` carries no state between files.
|
|
2386
|
+
*/
|
|
2387
|
+
const REACHABLE_FROM_JS = /\btoken(\s*\.\s*(?:var|value))?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2127
2388
|
/**
|
|
2128
2389
|
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2129
2390
|
* parser understands it.
|
|
@@ -2144,61 +2405,58 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2144
2405
|
* is an order of magnitude more.
|
|
2145
2406
|
*/
|
|
2146
2407
|
function* sourceTexts(ctx) {
|
|
2147
|
-
for (const
|
|
2148
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2149
|
-
let onDisk;
|
|
2150
|
-
try {
|
|
2151
|
-
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2152
|
-
} catch {
|
|
2153
|
-
onDisk = void 0;
|
|
2154
|
-
}
|
|
2155
|
-
const parsed = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2156
|
-
if (onDisk != null) yield onDisk;
|
|
2157
|
-
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2158
|
-
}
|
|
2408
|
+
for (const snapshot of sourceSnapshots(ctx)) yield* snapshotTexts(snapshot);
|
|
2159
2409
|
}
|
|
2160
2410
|
/**
|
|
2161
|
-
*
|
|
2411
|
+
* The token prune, as all three build paths need to run it.
|
|
2162
2412
|
*
|
|
2163
|
-
*
|
|
2164
|
-
*
|
|
2165
|
-
*
|
|
2413
|
+
* One function because it was three copies of one conditional, and the `false` branch went
|
|
2414
|
+
* missing from a copy: a watch rebuild with `pruneUnusedTokens: false` skipped `pruneTokens`
|
|
2415
|
+
* altogether, so it kept `@property` registrations that a full build of the same source
|
|
2416
|
+
* strips. The other two copies carried a comment pointing at that file for the reasoning,
|
|
2417
|
+
* which is how a missing branch reads as intentional.
|
|
2166
2418
|
*
|
|
2167
|
-
*
|
|
2168
|
-
*
|
|
2169
|
-
*
|
|
2170
|
-
*
|
|
2171
|
-
* encode every style a second time — pass none, and stay correct only because a path the
|
|
2172
|
-
* scan misses can lose nothing: `token()` hands javascript a literal for exactly the
|
|
2173
|
-
* tokens this cannot see, and hands it a `var()` only for virtual, conditional and
|
|
2174
|
-
* negative tokens, which `getAlwaysKeptTokenVars` keeps.
|
|
2175
|
-
*
|
|
2176
|
-
* Those blanket keeps are no longer unconditional — `tokensReachableFromJs` gates them — and
|
|
2177
|
-
* this comment used to warn that narrowing them would break the argument above. It does not,
|
|
2178
|
-
* for a reason worth stating rather than rediscovering: the gate matches `token(` regardless
|
|
2179
|
-
* of what follows, where `TOKEN_CALL` below needs a string literal. So the one shape this
|
|
2180
|
-
* scan cannot resolve, a path built from a constant, is exactly the shape that turns the
|
|
2181
|
-
* gate on and restores every blanket keep. The two failures line up, which is what makes the
|
|
2182
|
-
* redundancy hold.
|
|
2183
|
-
*
|
|
2184
|
-
* That alignment is a real coupling, not an observation: the gate's call pattern has to stay
|
|
2185
|
-
* a superset of this one, matching wherever `TOKEN_CALL` matches and also wherever it gives
|
|
2186
|
-
* up. It briefly was not — this allowed whitespace around the `.` of `token.var` and the gate
|
|
2187
|
-
* did not, so a formatter wrapping `token\n .var(SOME_CONST)` slipped past both. Change one
|
|
2188
|
-
* of the two and change the other; `token-references.test.ts` pins the property directly.
|
|
2189
|
-
*
|
|
2190
|
-
* Where it stops holding is a binding renamed away from `token` — `const t = token`, then
|
|
2191
|
-
* `t('spacing.4')` — which matches neither. `token-references.test.ts` pins both halves.
|
|
2419
|
+
* Opting out still drops the registrations. Those are not tokens — nothing hands one to
|
|
2420
|
+
* javascript, and none appear in the `token()` surface — so the reachability problem the flag
|
|
2421
|
+
* exists for does not apply to them, and opting out of token pruning should not mean shipping
|
|
2422
|
+
* a preset's whole filter and gradient set for nothing.
|
|
2192
2423
|
*/
|
|
2193
|
-
function
|
|
2424
|
+
function pruneTokensForBuild(ctx, sheet, results) {
|
|
2425
|
+
if (!ctx.config.pruneUnusedTokens) {
|
|
2426
|
+
ctx.pruneTokens(sheet);
|
|
2427
|
+
return;
|
|
2428
|
+
}
|
|
2429
|
+
const strict = ctx.config.pruneUnusedTokens === "strict";
|
|
2194
2430
|
const paths = /* @__PURE__ */ new Set();
|
|
2195
2431
|
const vars = /* @__PURE__ */ new Set();
|
|
2432
|
+
const accounting = {
|
|
2433
|
+
paths: /* @__PURE__ */ new Set(),
|
|
2434
|
+
declined: []
|
|
2435
|
+
};
|
|
2436
|
+
let reachable = false;
|
|
2196
2437
|
for (const result of results) for (const item of result.token) for (const value of item.data ?? []) if (typeof value === "string") paths.add(value);
|
|
2197
|
-
for (const
|
|
2198
|
-
for (const
|
|
2199
|
-
|
|
2438
|
+
for (const snapshot of sourceSnapshots(ctx)) {
|
|
2439
|
+
for (const text of snapshotTexts(snapshot)) {
|
|
2440
|
+
for (const match of text.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2441
|
+
for (const name of (0, _bamboocss_shared.cssVarRefs)(text)) vars.add(name);
|
|
2442
|
+
if (!reachable && REACHABLE_FROM_JS.test(text)) reachable = true;
|
|
2443
|
+
}
|
|
2444
|
+
if (strict) accountSnapshot(ctx, snapshot, accounting);
|
|
2200
2445
|
}
|
|
2446
|
+
for (const name of tokenVarsFor(ctx, paths)) vars.add(name);
|
|
2447
|
+
if (!strict) {
|
|
2448
|
+
ctx.pruneTokens(sheet, vars, reachable);
|
|
2449
|
+
return;
|
|
2450
|
+
}
|
|
2451
|
+
for (const name of tokenVarsFor(ctx, accounting.paths)) vars.add(name);
|
|
2452
|
+
if (accounting.declined.length) _bamboocss_logger.logger.warn("tokens:strict", formatDeclined(ctx, accounting.declined));
|
|
2453
|
+
ctx.pruneTokens(sheet, vars, accounting.declined.length > 0 && reachable);
|
|
2454
|
+
}
|
|
2455
|
+
/** The custom properties a set of token paths resolves to. */
|
|
2456
|
+
function tokenVarsFor(ctx, paths) {
|
|
2457
|
+
const vars = /* @__PURE__ */ new Set();
|
|
2201
2458
|
for (const path of paths) {
|
|
2459
|
+
if (!path) continue;
|
|
2202
2460
|
const ref = ctx.tokens.view.getVar(path);
|
|
2203
2461
|
if (!ref) continue;
|
|
2204
2462
|
for (const name of (0, _bamboocss_shared.cssVarRefs)(ref)) vars.add(name);
|
|
@@ -2206,6 +2464,25 @@ function collectTokenReferences(ctx, results) {
|
|
|
2206
2464
|
return vars;
|
|
2207
2465
|
}
|
|
2208
2466
|
/**
|
|
2467
|
+
* Why `strict` could not prune, grouped by file.
|
|
2468
|
+
*
|
|
2469
|
+
* Printed rather than thrown. A decline is not an error — the build falls back to keeping
|
|
2470
|
+
* every declaration, which is what would have happened anyway — it is the answer to "why is my
|
|
2471
|
+
* token layer still this size", which otherwise has no answer at all.
|
|
2472
|
+
*/
|
|
2473
|
+
function formatDeclined(ctx, declined) {
|
|
2474
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
2475
|
+
for (const entry of declined) {
|
|
2476
|
+
const list = byFile.get(entry.filePath) ?? [];
|
|
2477
|
+
list.push(entry);
|
|
2478
|
+
byFile.set(entry.filePath, list);
|
|
2479
|
+
}
|
|
2480
|
+
const detail = Array.from(byFile.entries()).map(([filePath, entries]) => {
|
|
2481
|
+
return [` ${filePath.startsWith(ctx.config.cwd) ? filePath.slice(ctx.config.cwd.length + 1) : filePath}`, ...entries.map((entry) => ` ${entry.line}: ${entry.reason}`)].join("\n");
|
|
2482
|
+
}).join("\n");
|
|
2483
|
+
return `${declined.length} token reference(s) could not be resolved, so every token declaration is kept.\n\n${detail}\n\nSpell the path as a string literal at the call, move it into \`staticCss\`, or set \`pruneUnusedTokens: true\` to stop asking.`;
|
|
2484
|
+
}
|
|
2485
|
+
/**
|
|
2209
2486
|
* Collect keyframe names that reading the generated css cannot reveal.
|
|
2210
2487
|
*
|
|
2211
2488
|
* A keyframe reached through `css({ animation: 'fade-in 1s' })` lands in the stylesheet
|
|
@@ -2263,41 +2540,6 @@ function collectRenderedElements(ctx) {
|
|
|
2263
2540
|
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2264
2541
|
return found;
|
|
2265
2542
|
}
|
|
2266
|
-
/**
|
|
2267
|
-
* Whether any source file reaches for a token from javascript.
|
|
2268
|
-
*
|
|
2269
|
-
* The tokens artifact is generated into the project rather than installed, so the import is
|
|
2270
|
-
* written in the project's own source and a scan of `include` sees it. When this comes back
|
|
2271
|
-
* false, the declarations kept purely so `token()` can answer have no caller to answer.
|
|
2272
|
-
*
|
|
2273
|
-
* Deliberately loose, and loose in a specific direction: a false positive keeps a declaration
|
|
2274
|
-
* nothing reads, a false negative returns a `var()` nothing declares.
|
|
2275
|
-
*
|
|
2276
|
-
* So the import test is any module specifier with a `/tokens` path segment, rather than the
|
|
2277
|
-
* literal `styled-system/tokens` it used to be. `outdir` is configurable, so the artifact is
|
|
2278
|
-
* only at `styled-system/` by default, and the literal missed `outdir: 'design-system'`, a
|
|
2279
|
-
* tsconfig path alias, and `styled-system/tokens/index.mjs` -- which is the only spelling
|
|
2280
|
-
* NodeNext accepts, the artifact being a directory. It is still anchored to `from`, `import`
|
|
2281
|
-
* or `require`, because without that anchor a route or a url (`fetch('/api/tokens')`) reads
|
|
2282
|
-
* as an import and quietly switches the whole optimisation off.
|
|
2283
|
-
*
|
|
2284
|
-
* The call test allows whitespace around the dot for the same reason `TOKEN_CALL` does. It
|
|
2285
|
-
* did not, which broke the alignment the note on `collectTokenReferences` rests on: a
|
|
2286
|
-
* formatter wrapping `token\n .var(SOME_CONST)` was invisible to that scan *and* to this
|
|
2287
|
-
* gate *and* to the parser, whose callee is a property access. A comment in the same position
|
|
2288
|
-
* -- `token/*x*\/.var(` -- is still invisible to all three.
|
|
2289
|
-
*
|
|
2290
|
-
* One shape it still does not see, pinned in `token-references.test.ts`: a binding renamed
|
|
2291
|
-
* away from `token`, as in `const t = token`. Also unseen is a caller outside `include`,
|
|
2292
|
-
* which scopes style extraction rather than everything that may import — a script, a config,
|
|
2293
|
-
* or a sibling workspace package. Both prune declarations the running app then asks for, and
|
|
2294
|
-
* neither reports itself; `pruneUnusedTokens: false` is the way out.
|
|
2295
|
-
*/
|
|
2296
|
-
function tokensReachableFromJs(ctx) {
|
|
2297
|
-
const pattern = /\btoken(\s*\.\s*var)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2298
|
-
for (const content of sourceTexts(ctx)) if (pattern.test(content)) return true;
|
|
2299
|
-
return false;
|
|
2300
|
-
}
|
|
2301
2543
|
//#endregion
|
|
2302
2544
|
//#region src/builder.ts
|
|
2303
2545
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2480,8 +2722,7 @@ var Builder = class {
|
|
|
2480
2722
|
const sheet = ctx.createSheet();
|
|
2481
2723
|
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2482
2724
|
ctx.appendBaselineCss(sheet);
|
|
2483
|
-
|
|
2484
|
-
else ctx.pruneTokens(sheet);
|
|
2725
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2485
2726
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2486
2727
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2487
2728
|
return ctx.getCss(sheet);
|
|
@@ -2586,8 +2827,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2586
2827
|
ctx.appendBaselineCss(sheet);
|
|
2587
2828
|
}
|
|
2588
2829
|
ctx.appendParserCss(sheet);
|
|
2589
|
-
if (!minimal
|
|
2590
|
-
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2830
|
+
if (!minimal) pruneTokensForBuild(ctx, sheet, results);
|
|
2591
2831
|
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2592
2832
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2593
2833
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
@@ -2659,8 +2899,7 @@ async function build(ctx, artifactIds) {
|
|
|
2659
2899
|
ctx.appendBaselineCss(sheet);
|
|
2660
2900
|
const parsed = ctx.parseFiles();
|
|
2661
2901
|
ctx.appendParserCss(sheet);
|
|
2662
|
-
|
|
2663
|
-
else ctx.pruneTokens(sheet);
|
|
2902
|
+
pruneTokensForBuild(ctx, sheet, parsed.results);
|
|
2664
2903
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2665
2904
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2666
2905
|
await ctx.writeCss(sheet);
|
|
@@ -2706,7 +2945,7 @@ async function generate(config, configPath) {
|
|
|
2706
2945
|
ctx.appendLayerParams(sheet);
|
|
2707
2946
|
ctx.appendBaselineCss(sheet);
|
|
2708
2947
|
ctx.appendParserCss(sheet);
|
|
2709
|
-
|
|
2948
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2710
2949
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2711
2950
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2712
2951
|
const css = ctx.getCss(sheet);
|
|
@@ -2810,7 +3049,7 @@ export default defineConfig({
|
|
|
2810
3049
|
|
|
2811
3050
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2812
3051
|
// by default — worth 36-78% of a new project's stylesheet. Set \`pruneUnusedTokens: false\`
|
|
2813
|
-
// if you read tokens from somewhere the build cannot see them — \`token
|
|
3052
|
+
// if you read tokens from somewhere the build cannot see them — \`token()\` with a
|
|
2814
3053
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2815
3054
|
// \`staticCss\` to keep them.
|
|
2816
3055
|
|
package/dist/index.mjs
CHANGED
|
@@ -23,6 +23,8 @@ import path from "node:path";
|
|
|
23
23
|
import fs, { promises } from "node:fs";
|
|
24
24
|
import isGlob from "is-glob";
|
|
25
25
|
import globParent from "glob-parent";
|
|
26
|
+
import { resolveTsPathPattern } from "@bamboocss/config/ts-path";
|
|
27
|
+
import { Node, SyntaxKind } from "ts-morph";
|
|
26
28
|
import readline from "node:readline";
|
|
27
29
|
import { lookItUpSync } from "look-it-up";
|
|
28
30
|
import outdent$1, { outdent } from "outdent";
|
|
@@ -1440,6 +1442,25 @@ function globDirname(globs) {
|
|
|
1440
1442
|
}
|
|
1441
1443
|
//#endregion
|
|
1442
1444
|
//#region src/node-runtime.ts
|
|
1445
|
+
/**
|
|
1446
|
+
* What the source glob ignores.
|
|
1447
|
+
*
|
|
1448
|
+
* `**\/*.d.ts` is unconditional. It used to be a *default* that a user's own `exclude`
|
|
1449
|
+
* replaced, so declaration files were scanned by whether the project happened to set that
|
|
1450
|
+
* option at all — excluded for `exclude: []`, included for `exclude: ['**\/*.stories.tsx']`.
|
|
1451
|
+
* Nothing chose that; the default was simply appended when nothing else was there.
|
|
1452
|
+
*
|
|
1453
|
+
* A declaration file carries no runtime code and can emit no styles. It is still read by the
|
|
1454
|
+
* reference scans, which are deliberately over-inclusive, so scanning one could keep a token
|
|
1455
|
+
* named in a doc comment or a reset rule for an element named in a JSDoc example. Those are
|
|
1456
|
+
* spurious keeps rather than a guarantee anyone relied on — and they were never available to
|
|
1457
|
+
* the projects on the other side of the same condition.
|
|
1458
|
+
*
|
|
1459
|
+
* Copied rather than appended to. `opts.exclude` is `ctx.config.exclude` itself, so pushing
|
|
1460
|
+
* onto it edited the user's resolved config in place, and the next call then saw a list it had
|
|
1461
|
+
* mutated.
|
|
1462
|
+
*/
|
|
1463
|
+
const globIgnore = (exclude) => ["**/*.d.ts", ...exclude ?? []];
|
|
1443
1464
|
const nodeRuntime = {
|
|
1444
1465
|
cwd() {
|
|
1445
1466
|
return process.cwd();
|
|
@@ -1466,11 +1487,9 @@ const nodeRuntime = {
|
|
|
1466
1487
|
},
|
|
1467
1488
|
glob(opts) {
|
|
1468
1489
|
if (!opts.include) return [];
|
|
1469
|
-
const ignore = opts.exclude ?? [];
|
|
1470
|
-
if (!ignore.length) ignore.push("**/*.d.ts");
|
|
1471
1490
|
return glob.sync(opts.include, {
|
|
1472
1491
|
cwd: opts.cwd,
|
|
1473
|
-
ignore,
|
|
1492
|
+
ignore: globIgnore(opts.exclude),
|
|
1474
1493
|
absolute: true
|
|
1475
1494
|
});
|
|
1476
1495
|
},
|
|
@@ -2099,6 +2118,240 @@ function parseDependency(fileOrGlob) {
|
|
|
2099
2118
|
return message;
|
|
2100
2119
|
}
|
|
2101
2120
|
//#endregion
|
|
2121
|
+
//#region src/source-snapshots.ts
|
|
2122
|
+
/**
|
|
2123
|
+
* Every file `include` covers, read once.
|
|
2124
|
+
*
|
|
2125
|
+
* The scans that decide what survives pruning — the token reference set, the reachability
|
|
2126
|
+
* gate, the strict accounting — all want the same two copies of the same files, and each used
|
|
2127
|
+
* to fetch them itself. A strict build therefore read every file three times over: once to
|
|
2128
|
+
* collect references, once to account, and once more for the gate whenever the accounting
|
|
2129
|
+
* declined. Yielding a snapshot lets one walk feed all of them.
|
|
2130
|
+
*
|
|
2131
|
+
* Both copies, because neither alone is complete and the text scans are safe when over-fed. A
|
|
2132
|
+
* file the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`),
|
|
2133
|
+
* and those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2134
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2135
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2136
|
+
* like that reports no tokens and no elements at all.
|
|
2137
|
+
*
|
|
2138
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2139
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2140
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2141
|
+
*/
|
|
2142
|
+
function* sourceSnapshots(ctx) {
|
|
2143
|
+
for (const file of ctx.getFiles()) {
|
|
2144
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2145
|
+
let onDisk;
|
|
2146
|
+
try {
|
|
2147
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2148
|
+
} catch {
|
|
2149
|
+
onDisk = void 0;
|
|
2150
|
+
}
|
|
2151
|
+
const sourceFile = ctx.project.getSourceFile(filePath);
|
|
2152
|
+
yield {
|
|
2153
|
+
filePath,
|
|
2154
|
+
onDisk,
|
|
2155
|
+
parsed: sourceFile?.getFullText(),
|
|
2156
|
+
sourceFile
|
|
2157
|
+
};
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
/**
|
|
2161
|
+
* The distinct texts a snapshot carries.
|
|
2162
|
+
*
|
|
2163
|
+
* One when the parser holds the file as written, two when a transform rewrote it. Every
|
|
2164
|
+
* textual scan wants both, and wants neither twice.
|
|
2165
|
+
*/
|
|
2166
|
+
function* snapshotTexts(snapshot) {
|
|
2167
|
+
const { onDisk, parsed } = snapshot;
|
|
2168
|
+
if (onDisk != null) yield onDisk;
|
|
2169
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2170
|
+
}
|
|
2171
|
+
//#endregion
|
|
2172
|
+
//#region src/token-accounting.ts
|
|
2173
|
+
/**
|
|
2174
|
+
* One file's contribution, split out so the build can account and scan in a single walk.
|
|
2175
|
+
*
|
|
2176
|
+
* `pruneTokensForBuild` needs the keep set, the reachability answer and this accounting from
|
|
2177
|
+
* the same files; three separate passes read every file three times.
|
|
2178
|
+
*/
|
|
2179
|
+
function accountSnapshot(ctx, snapshot, accounting) {
|
|
2180
|
+
const { filePath, onDisk, parsed, sourceFile } = snapshot;
|
|
2181
|
+
const { paths, declined } = accounting;
|
|
2182
|
+
if (onDisk == null || parsed == null || parsed !== onDisk) {
|
|
2183
|
+
if (!((onDisk?.includes("token") ?? false) || (parsed?.includes("token") ?? false))) return;
|
|
2184
|
+
declined.push({
|
|
2185
|
+
filePath,
|
|
2186
|
+
line: 1,
|
|
2187
|
+
reason: onDisk == null || parsed == null ? "unreadable" : "transformed"
|
|
2188
|
+
});
|
|
2189
|
+
return;
|
|
2190
|
+
}
|
|
2191
|
+
if (parseErrorCount(sourceFile)) {
|
|
2192
|
+
if (!parsed.includes("token")) return;
|
|
2193
|
+
declined.push({
|
|
2194
|
+
filePath,
|
|
2195
|
+
line: 1,
|
|
2196
|
+
reason: "unparsed"
|
|
2197
|
+
});
|
|
2198
|
+
return;
|
|
2199
|
+
}
|
|
2200
|
+
accountFile(ctx, sourceFile, filePath, paths, declined);
|
|
2201
|
+
}
|
|
2202
|
+
/**
|
|
2203
|
+
* How many syntax errors the parse produced.
|
|
2204
|
+
*
|
|
2205
|
+
* `parseDiagnostics` is TypeScript-internal and absent from the public `SourceFile` type, so
|
|
2206
|
+
* it is reached through a cast. The alternative, `getPreEmitDiagnostics`, runs the type checker
|
|
2207
|
+
* over the whole program — orders of magnitude more work for a question about syntax, and it
|
|
2208
|
+
* would report type errors this pass has no business declining over.
|
|
2209
|
+
*/
|
|
2210
|
+
const parseErrorCount = (sourceFile) => sourceFile.compilerNode.parseDiagnostics?.length ?? 0;
|
|
2211
|
+
/** Whether a module specifier resolves to the generated tokens artifact. */
|
|
2212
|
+
const isTokensEntrypoint = (ctx, specifier) => {
|
|
2213
|
+
const mods = ctx.imports.value.tokens;
|
|
2214
|
+
if (mods.some((mod) => specifier.includes(mod))) return true;
|
|
2215
|
+
const pathMappings = ctx.conf.tsOptions?.pathMappings;
|
|
2216
|
+
if (!pathMappings) return false;
|
|
2217
|
+
const resolved = resolveTsPathPattern(pathMappings, specifier);
|
|
2218
|
+
if (!resolved) return false;
|
|
2219
|
+
return mods.some((mod) => resolved.includes(mod) || resolved === mod);
|
|
2220
|
+
};
|
|
2221
|
+
const lineOf = (node) => node.getSourceFile().getLineAndColumnAtPos(node.getStart()).line;
|
|
2222
|
+
function accountFile(ctx, sourceFile, filePath, paths, declined) {
|
|
2223
|
+
const decline = (node, reason) => declined.push({
|
|
2224
|
+
filePath,
|
|
2225
|
+
line: lineOf(node),
|
|
2226
|
+
reason
|
|
2227
|
+
});
|
|
2228
|
+
/** Local names bound to the artifact: the `token` export, and any namespace of it. */
|
|
2229
|
+
const bindings = /* @__PURE__ */ new Set();
|
|
2230
|
+
for (const statement of sourceFile.getStatements()) {
|
|
2231
|
+
if (Node.isExportDeclaration(statement)) {
|
|
2232
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2233
|
+
if (specifier && isTokensEntrypoint(ctx, specifier)) decline(statement, "re-exported");
|
|
2234
|
+
continue;
|
|
2235
|
+
}
|
|
2236
|
+
if (!Node.isImportDeclaration(statement)) continue;
|
|
2237
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2238
|
+
if (!specifier) continue;
|
|
2239
|
+
const clause = statement.getImportClause();
|
|
2240
|
+
if (!clause || statement.isTypeOnly()) continue;
|
|
2241
|
+
if (!isTokensEntrypoint(ctx, specifier)) {
|
|
2242
|
+
for (const named of clause.getNamedImports()) if (!named.isTypeOnly() && nameOf(named.getNameNode()) === "token") decline(named, "unclassified-import");
|
|
2243
|
+
const foreignDefault = clause.getDefaultImport();
|
|
2244
|
+
if (foreignDefault && nameOf(foreignDefault) === "token") decline(foreignDefault, "unclassified-import");
|
|
2245
|
+
const foreignNamespace = clause.getNamespaceImport();
|
|
2246
|
+
if (foreignNamespace && usesTokenMember(sourceFile, foreignNamespace.getText())) decline(foreignNamespace, "unclassified-import");
|
|
2247
|
+
continue;
|
|
2248
|
+
}
|
|
2249
|
+
const defaultImport = clause.getDefaultImport();
|
|
2250
|
+
if (defaultImport) decline(defaultImport, "unsupported-import");
|
|
2251
|
+
const namespace = clause.getNamespaceImport();
|
|
2252
|
+
if (namespace) bindings.add(namespace.getText());
|
|
2253
|
+
for (const named of clause.getNamedImports()) {
|
|
2254
|
+
if (named.isTypeOnly()) continue;
|
|
2255
|
+
if (nameOf(named.getNameNode()) !== "token") {
|
|
2256
|
+
decline(named, "unsupported-import");
|
|
2257
|
+
continue;
|
|
2258
|
+
}
|
|
2259
|
+
bindings.add((named.getAliasNode() ?? named.getNameNode()).getText());
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
for (const importEquals of sourceFile.getDescendantsOfKind(SyntaxKind.ImportEqualsDeclaration)) if (importEquals.getText().includes("token")) decline(importEquals, "import-equals");
|
|
2263
|
+
for (const call of sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
2264
|
+
const callee = call.getExpression();
|
|
2265
|
+
const isRequire = Node.isIdentifier(callee) && callee.getText() === "require";
|
|
2266
|
+
const isDynamicImport = callee.getKind() === SyntaxKind.ImportKeyword;
|
|
2267
|
+
if (!isRequire && !isDynamicImport) continue;
|
|
2268
|
+
const argument = call.getArguments()[0];
|
|
2269
|
+
if (!argument) continue;
|
|
2270
|
+
if (Node.isStringLiteral(argument) ? isTokensEntrypoint(ctx, argument.getLiteralValue()) : true) decline(call, isRequire ? "require" : "dynamic-import");
|
|
2271
|
+
}
|
|
2272
|
+
for (const identifier of sourceFile.getDescendantsOfKind(SyntaxKind.Identifier)) {
|
|
2273
|
+
const text = nameOf(identifier);
|
|
2274
|
+
if (!bindings.has(text) && text !== "token") continue;
|
|
2275
|
+
const parent = identifier.getParent();
|
|
2276
|
+
if (Node.isPropertyAccessExpression(parent) && parent.getNameNode() === identifier) {
|
|
2277
|
+
const object = parent.getExpression();
|
|
2278
|
+
const objectName = Node.isIdentifier(object) ? object.getText() : void 0;
|
|
2279
|
+
if (objectName && bindings.has(objectName)) continue;
|
|
2280
|
+
decline(identifier, "unresolved-reference");
|
|
2281
|
+
continue;
|
|
2282
|
+
}
|
|
2283
|
+
if (Node.isPropertyAssignment(parent) && parent.getNameNode() === identifier) continue;
|
|
2284
|
+
const path = accountedPath(identifier);
|
|
2285
|
+
if (path === void 0) {
|
|
2286
|
+
decline(identifier, "unresolved-reference");
|
|
2287
|
+
continue;
|
|
2288
|
+
}
|
|
2289
|
+
paths.add(path);
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
/**
|
|
2293
|
+
* Whether any `<namespace>.token` member access appears, for an unclassified namespace.
|
|
2294
|
+
*
|
|
2295
|
+
* Element access counts too: `ui['token'](k)` reaches the same export and is invisible to a
|
|
2296
|
+
* property-access-only scan.
|
|
2297
|
+
*/
|
|
2298
|
+
function usesTokenMember(sourceFile, namespace) {
|
|
2299
|
+
if (sourceFile.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression).some((access) => access.getExpression().getText() === namespace && nameOf(access.getNameNode()) === "token")) return true;
|
|
2300
|
+
return sourceFile.getDescendantsOfKind(SyntaxKind.ElementAccessExpression).some((access) => {
|
|
2301
|
+
if (access.getExpression().getText() !== namespace) return false;
|
|
2302
|
+
const argument = access.getArgumentExpression();
|
|
2303
|
+
return argument != null && (!Node.isStringLiteral(argument) || argument.getLiteralValue() === "token");
|
|
2304
|
+
});
|
|
2305
|
+
}
|
|
2306
|
+
/**
|
|
2307
|
+
* An identifier's name as the compiler resolves it, not as it is spelled.
|
|
2308
|
+
*
|
|
2309
|
+
* `token` is the identifier `token`; reading `getText()` returns the escape and compares
|
|
2310
|
+
* unequal, which let an import of the artifact's export past the checks that key on the name.
|
|
2311
|
+
*/
|
|
2312
|
+
const nameOf = (node) => Node.isIdentifier(node) ? String(node.compilerNode.escapedText) : node.getText();
|
|
2313
|
+
/**
|
|
2314
|
+
* The token path one occurrence asks for, or `undefined` if it is not a call this can read.
|
|
2315
|
+
*
|
|
2316
|
+
* Accepts the callee position and nothing else — `token('x')`, `token.var('x')`,
|
|
2317
|
+
* `token.value('x')`, and the namespaced spellings. An identifier anywhere else is a value
|
|
2318
|
+
* escaping somewhere this pass cannot follow: assigned (`const t = token`), passed
|
|
2319
|
+
* (`useMemo(() => token)`), spread, or enumerated.
|
|
2320
|
+
*/
|
|
2321
|
+
function accountedPath(identifier) {
|
|
2322
|
+
const parent = identifier.getParent();
|
|
2323
|
+
if (!parent) return void 0;
|
|
2324
|
+
if (Node.isImportSpecifier(parent) || Node.isNamespaceImport(parent) || Node.isImportClause(parent)) return "";
|
|
2325
|
+
if (Node.isCallExpression(parent) && parent.getExpression() === identifier) return literalPath(parent);
|
|
2326
|
+
if (!Node.isPropertyAccessExpression(parent) || parent.getExpression() !== identifier) return void 0;
|
|
2327
|
+
const property = parent.getNameNode().getText();
|
|
2328
|
+
const grandParent = parent.getParent();
|
|
2329
|
+
if (property === "var" || property === "value") return Node.isCallExpression(grandParent) && grandParent.getExpression() === parent ? literalPath(grandParent) : void 0;
|
|
2330
|
+
if (property !== "token") return void 0;
|
|
2331
|
+
if (Node.isCallExpression(grandParent) && grandParent.getExpression() === parent) return literalPath(grandParent);
|
|
2332
|
+
if (Node.isPropertyAccessExpression(grandParent) && grandParent.getExpression() === parent) {
|
|
2333
|
+
const method = grandParent.getNameNode().getText();
|
|
2334
|
+
if (method !== "var" && method !== "value") return void 0;
|
|
2335
|
+
const call = grandParent.getParent();
|
|
2336
|
+
return Node.isCallExpression(call) && call.getExpression() === grandParent ? literalPath(call) : void 0;
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
/**
|
|
2340
|
+
* The path a call asks for, when the first argument is a literal.
|
|
2341
|
+
*
|
|
2342
|
+
* Read through `getLiteralValue()`, never off the source text. The text carries escapes —
|
|
2343
|
+
* `token('colors.red.300')` — and a path recorded raw looks up nothing, which
|
|
2344
|
+
* would accept a reference and keep no declaration for it. That is the exact failure this
|
|
2345
|
+
* module exists to make unrepresentable.
|
|
2346
|
+
*/
|
|
2347
|
+
function literalPath(call) {
|
|
2348
|
+
if (!Node.isCallExpression(call)) return void 0;
|
|
2349
|
+
const argument = call.getArguments()[0];
|
|
2350
|
+
if (!argument) return void 0;
|
|
2351
|
+
if (Node.isStringLiteral(argument)) return argument.getLiteralValue();
|
|
2352
|
+
if (Node.isNoSubstitutionTemplateLiteral(argument)) return argument.getLiteralValue();
|
|
2353
|
+
}
|
|
2354
|
+
//#endregion
|
|
2102
2355
|
//#region src/token-references.ts
|
|
2103
2356
|
/**
|
|
2104
2357
|
* `token.var('colors.red.300')` and `token('spacing.4')`, including the whitespace a
|
|
@@ -2108,7 +2361,15 @@ function parseDependency(fileOrGlob) {
|
|
|
2108
2361
|
* earns its place twice over: it covers a path built somewhere the extractor cannot
|
|
2109
2362
|
* follow, and it covers the callers below that supply no `results` at all.
|
|
2110
2363
|
*/
|
|
2111
|
-
const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2364
|
+
const TOKEN_CALL = /\btoken(?:\s*\.\s*(?:var|value))?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2365
|
+
/**
|
|
2366
|
+
* A token reached from javascript at all — a call of any shape, or an import of the artifact.
|
|
2367
|
+
*
|
|
2368
|
+
* Has to stay a strict superset of `TOKEN_CALL`: it must match wherever that one matches, and
|
|
2369
|
+
* also wherever that one gives up. See `collectTokenReferences` for why the redundancy holds.
|
|
2370
|
+
* No `g` flag, so `test` carries no state between files.
|
|
2371
|
+
*/
|
|
2372
|
+
const REACHABLE_FROM_JS = /\btoken(\s*\.\s*(?:var|value))?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2112
2373
|
/**
|
|
2113
2374
|
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2114
2375
|
* parser understands it.
|
|
@@ -2129,61 +2390,58 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2129
2390
|
* is an order of magnitude more.
|
|
2130
2391
|
*/
|
|
2131
2392
|
function* sourceTexts(ctx) {
|
|
2132
|
-
for (const
|
|
2133
|
-
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2134
|
-
let onDisk;
|
|
2135
|
-
try {
|
|
2136
|
-
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2137
|
-
} catch {
|
|
2138
|
-
onDisk = void 0;
|
|
2139
|
-
}
|
|
2140
|
-
const parsed = ctx.project.getSourceFile(filePath)?.getFullText();
|
|
2141
|
-
if (onDisk != null) yield onDisk;
|
|
2142
|
-
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2143
|
-
}
|
|
2393
|
+
for (const snapshot of sourceSnapshots(ctx)) yield* snapshotTexts(snapshot);
|
|
2144
2394
|
}
|
|
2145
2395
|
/**
|
|
2146
|
-
*
|
|
2396
|
+
* The token prune, as all three build paths need to run it.
|
|
2147
2397
|
*
|
|
2148
|
-
*
|
|
2149
|
-
*
|
|
2150
|
-
*
|
|
2398
|
+
* One function because it was three copies of one conditional, and the `false` branch went
|
|
2399
|
+
* missing from a copy: a watch rebuild with `pruneUnusedTokens: false` skipped `pruneTokens`
|
|
2400
|
+
* altogether, so it kept `@property` registrations that a full build of the same source
|
|
2401
|
+
* strips. The other two copies carried a comment pointing at that file for the reasoning,
|
|
2402
|
+
* which is how a missing branch reads as intentional.
|
|
2151
2403
|
*
|
|
2152
|
-
*
|
|
2153
|
-
*
|
|
2154
|
-
*
|
|
2155
|
-
*
|
|
2156
|
-
* encode every style a second time — pass none, and stay correct only because a path the
|
|
2157
|
-
* scan misses can lose nothing: `token()` hands javascript a literal for exactly the
|
|
2158
|
-
* tokens this cannot see, and hands it a `var()` only for virtual, conditional and
|
|
2159
|
-
* negative tokens, which `getAlwaysKeptTokenVars` keeps.
|
|
2160
|
-
*
|
|
2161
|
-
* Those blanket keeps are no longer unconditional — `tokensReachableFromJs` gates them — and
|
|
2162
|
-
* this comment used to warn that narrowing them would break the argument above. It does not,
|
|
2163
|
-
* for a reason worth stating rather than rediscovering: the gate matches `token(` regardless
|
|
2164
|
-
* of what follows, where `TOKEN_CALL` below needs a string literal. So the one shape this
|
|
2165
|
-
* scan cannot resolve, a path built from a constant, is exactly the shape that turns the
|
|
2166
|
-
* gate on and restores every blanket keep. The two failures line up, which is what makes the
|
|
2167
|
-
* redundancy hold.
|
|
2168
|
-
*
|
|
2169
|
-
* That alignment is a real coupling, not an observation: the gate's call pattern has to stay
|
|
2170
|
-
* a superset of this one, matching wherever `TOKEN_CALL` matches and also wherever it gives
|
|
2171
|
-
* up. It briefly was not — this allowed whitespace around the `.` of `token.var` and the gate
|
|
2172
|
-
* did not, so a formatter wrapping `token\n .var(SOME_CONST)` slipped past both. Change one
|
|
2173
|
-
* of the two and change the other; `token-references.test.ts` pins the property directly.
|
|
2174
|
-
*
|
|
2175
|
-
* Where it stops holding is a binding renamed away from `token` — `const t = token`, then
|
|
2176
|
-
* `t('spacing.4')` — which matches neither. `token-references.test.ts` pins both halves.
|
|
2404
|
+
* Opting out still drops the registrations. Those are not tokens — nothing hands one to
|
|
2405
|
+
* javascript, and none appear in the `token()` surface — so the reachability problem the flag
|
|
2406
|
+
* exists for does not apply to them, and opting out of token pruning should not mean shipping
|
|
2407
|
+
* a preset's whole filter and gradient set for nothing.
|
|
2177
2408
|
*/
|
|
2178
|
-
function
|
|
2409
|
+
function pruneTokensForBuild(ctx, sheet, results) {
|
|
2410
|
+
if (!ctx.config.pruneUnusedTokens) {
|
|
2411
|
+
ctx.pruneTokens(sheet);
|
|
2412
|
+
return;
|
|
2413
|
+
}
|
|
2414
|
+
const strict = ctx.config.pruneUnusedTokens === "strict";
|
|
2179
2415
|
const paths = /* @__PURE__ */ new Set();
|
|
2180
2416
|
const vars = /* @__PURE__ */ new Set();
|
|
2417
|
+
const accounting = {
|
|
2418
|
+
paths: /* @__PURE__ */ new Set(),
|
|
2419
|
+
declined: []
|
|
2420
|
+
};
|
|
2421
|
+
let reachable = false;
|
|
2181
2422
|
for (const result of results) for (const item of result.token) for (const value of item.data ?? []) if (typeof value === "string") paths.add(value);
|
|
2182
|
-
for (const
|
|
2183
|
-
for (const
|
|
2184
|
-
|
|
2423
|
+
for (const snapshot of sourceSnapshots(ctx)) {
|
|
2424
|
+
for (const text of snapshotTexts(snapshot)) {
|
|
2425
|
+
for (const match of text.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2426
|
+
for (const name of cssVarRefs(text)) vars.add(name);
|
|
2427
|
+
if (!reachable && REACHABLE_FROM_JS.test(text)) reachable = true;
|
|
2428
|
+
}
|
|
2429
|
+
if (strict) accountSnapshot(ctx, snapshot, accounting);
|
|
2185
2430
|
}
|
|
2431
|
+
for (const name of tokenVarsFor(ctx, paths)) vars.add(name);
|
|
2432
|
+
if (!strict) {
|
|
2433
|
+
ctx.pruneTokens(sheet, vars, reachable);
|
|
2434
|
+
return;
|
|
2435
|
+
}
|
|
2436
|
+
for (const name of tokenVarsFor(ctx, accounting.paths)) vars.add(name);
|
|
2437
|
+
if (accounting.declined.length) logger.warn("tokens:strict", formatDeclined(ctx, accounting.declined));
|
|
2438
|
+
ctx.pruneTokens(sheet, vars, accounting.declined.length > 0 && reachable);
|
|
2439
|
+
}
|
|
2440
|
+
/** The custom properties a set of token paths resolves to. */
|
|
2441
|
+
function tokenVarsFor(ctx, paths) {
|
|
2442
|
+
const vars = /* @__PURE__ */ new Set();
|
|
2186
2443
|
for (const path of paths) {
|
|
2444
|
+
if (!path) continue;
|
|
2187
2445
|
const ref = ctx.tokens.view.getVar(path);
|
|
2188
2446
|
if (!ref) continue;
|
|
2189
2447
|
for (const name of cssVarRefs(ref)) vars.add(name);
|
|
@@ -2191,6 +2449,25 @@ function collectTokenReferences(ctx, results) {
|
|
|
2191
2449
|
return vars;
|
|
2192
2450
|
}
|
|
2193
2451
|
/**
|
|
2452
|
+
* Why `strict` could not prune, grouped by file.
|
|
2453
|
+
*
|
|
2454
|
+
* Printed rather than thrown. A decline is not an error — the build falls back to keeping
|
|
2455
|
+
* every declaration, which is what would have happened anyway — it is the answer to "why is my
|
|
2456
|
+
* token layer still this size", which otherwise has no answer at all.
|
|
2457
|
+
*/
|
|
2458
|
+
function formatDeclined(ctx, declined) {
|
|
2459
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
2460
|
+
for (const entry of declined) {
|
|
2461
|
+
const list = byFile.get(entry.filePath) ?? [];
|
|
2462
|
+
list.push(entry);
|
|
2463
|
+
byFile.set(entry.filePath, list);
|
|
2464
|
+
}
|
|
2465
|
+
const detail = Array.from(byFile.entries()).map(([filePath, entries]) => {
|
|
2466
|
+
return [` ${filePath.startsWith(ctx.config.cwd) ? filePath.slice(ctx.config.cwd.length + 1) : filePath}`, ...entries.map((entry) => ` ${entry.line}: ${entry.reason}`)].join("\n");
|
|
2467
|
+
}).join("\n");
|
|
2468
|
+
return `${declined.length} token reference(s) could not be resolved, so every token declaration is kept.\n\n${detail}\n\nSpell the path as a string literal at the call, move it into \`staticCss\`, or set \`pruneUnusedTokens: true\` to stop asking.`;
|
|
2469
|
+
}
|
|
2470
|
+
/**
|
|
2194
2471
|
* Collect keyframe names that reading the generated css cannot reveal.
|
|
2195
2472
|
*
|
|
2196
2473
|
* A keyframe reached through `css({ animation: 'fade-in 1s' })` lands in the stylesheet
|
|
@@ -2248,41 +2525,6 @@ function collectRenderedElements(ctx) {
|
|
|
2248
2525
|
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2249
2526
|
return found;
|
|
2250
2527
|
}
|
|
2251
|
-
/**
|
|
2252
|
-
* Whether any source file reaches for a token from javascript.
|
|
2253
|
-
*
|
|
2254
|
-
* The tokens artifact is generated into the project rather than installed, so the import is
|
|
2255
|
-
* written in the project's own source and a scan of `include` sees it. When this comes back
|
|
2256
|
-
* false, the declarations kept purely so `token()` can answer have no caller to answer.
|
|
2257
|
-
*
|
|
2258
|
-
* Deliberately loose, and loose in a specific direction: a false positive keeps a declaration
|
|
2259
|
-
* nothing reads, a false negative returns a `var()` nothing declares.
|
|
2260
|
-
*
|
|
2261
|
-
* So the import test is any module specifier with a `/tokens` path segment, rather than the
|
|
2262
|
-
* literal `styled-system/tokens` it used to be. `outdir` is configurable, so the artifact is
|
|
2263
|
-
* only at `styled-system/` by default, and the literal missed `outdir: 'design-system'`, a
|
|
2264
|
-
* tsconfig path alias, and `styled-system/tokens/index.mjs` -- which is the only spelling
|
|
2265
|
-
* NodeNext accepts, the artifact being a directory. It is still anchored to `from`, `import`
|
|
2266
|
-
* or `require`, because without that anchor a route or a url (`fetch('/api/tokens')`) reads
|
|
2267
|
-
* as an import and quietly switches the whole optimisation off.
|
|
2268
|
-
*
|
|
2269
|
-
* The call test allows whitespace around the dot for the same reason `TOKEN_CALL` does. It
|
|
2270
|
-
* did not, which broke the alignment the note on `collectTokenReferences` rests on: a
|
|
2271
|
-
* formatter wrapping `token\n .var(SOME_CONST)` was invisible to that scan *and* to this
|
|
2272
|
-
* gate *and* to the parser, whose callee is a property access. A comment in the same position
|
|
2273
|
-
* -- `token/*x*\/.var(` -- is still invisible to all three.
|
|
2274
|
-
*
|
|
2275
|
-
* One shape it still does not see, pinned in `token-references.test.ts`: a binding renamed
|
|
2276
|
-
* away from `token`, as in `const t = token`. Also unseen is a caller outside `include`,
|
|
2277
|
-
* which scopes style extraction rather than everything that may import — a script, a config,
|
|
2278
|
-
* or a sibling workspace package. Both prune declarations the running app then asks for, and
|
|
2279
|
-
* neither reports itself; `pruneUnusedTokens: false` is the way out.
|
|
2280
|
-
*/
|
|
2281
|
-
function tokensReachableFromJs(ctx) {
|
|
2282
|
-
const pattern = /\btoken(\s*\.\s*var)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2283
|
-
for (const content of sourceTexts(ctx)) if (pattern.test(content)) return true;
|
|
2284
|
-
return false;
|
|
2285
|
-
}
|
|
2286
2528
|
//#endregion
|
|
2287
2529
|
//#region src/builder.ts
|
|
2288
2530
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2465,8 +2707,7 @@ var Builder = class {
|
|
|
2465
2707
|
const sheet = ctx.createSheet();
|
|
2466
2708
|
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2467
2709
|
ctx.appendBaselineCss(sheet);
|
|
2468
|
-
|
|
2469
|
-
else ctx.pruneTokens(sheet);
|
|
2710
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2470
2711
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2471
2712
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2472
2713
|
return ctx.getCss(sheet);
|
|
@@ -2571,8 +2812,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2571
2812
|
ctx.appendBaselineCss(sheet);
|
|
2572
2813
|
}
|
|
2573
2814
|
ctx.appendParserCss(sheet);
|
|
2574
|
-
if (!minimal
|
|
2575
|
-
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2815
|
+
if (!minimal) pruneTokensForBuild(ctx, sheet, results);
|
|
2576
2816
|
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2577
2817
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2578
2818
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
@@ -2644,8 +2884,7 @@ async function build(ctx, artifactIds) {
|
|
|
2644
2884
|
ctx.appendBaselineCss(sheet);
|
|
2645
2885
|
const parsed = ctx.parseFiles();
|
|
2646
2886
|
ctx.appendParserCss(sheet);
|
|
2647
|
-
|
|
2648
|
-
else ctx.pruneTokens(sheet);
|
|
2887
|
+
pruneTokensForBuild(ctx, sheet, parsed.results);
|
|
2649
2888
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2650
2889
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2651
2890
|
await ctx.writeCss(sheet);
|
|
@@ -2691,7 +2930,7 @@ async function generate(config, configPath) {
|
|
|
2691
2930
|
ctx.appendLayerParams(sheet);
|
|
2692
2931
|
ctx.appendBaselineCss(sheet);
|
|
2693
2932
|
ctx.appendParserCss(sheet);
|
|
2694
|
-
|
|
2933
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2695
2934
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2696
2935
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2697
2936
|
const css = ctx.getCss(sheet);
|
|
@@ -2795,7 +3034,7 @@ export default defineConfig({
|
|
|
2795
3034
|
|
|
2796
3035
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2797
3036
|
// by default — worth 36-78% of a new project's stylesheet. Set \`pruneUnusedTokens: false\`
|
|
2798
|
-
// if you read tokens from somewhere the build cannot see them — \`token
|
|
3037
|
+
// if you read tokens from somewhere the build cannot see them — \`token()\` with a
|
|
2799
3038
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2800
3039
|
// \`staticCss\` to keep them.
|
|
2801
3040
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bamboocss/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.29.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/reporter": "1.
|
|
64
|
-
"@bamboocss/shared": "1.
|
|
65
|
-
"@bamboocss/token-dictionary": "1.
|
|
66
|
-
"@bamboocss/types": "1.
|
|
55
|
+
"@bamboocss/config": "1.29.0",
|
|
56
|
+
"@bamboocss/core": "1.29.0",
|
|
57
|
+
"@bamboocss/generator": "1.29.0",
|
|
58
|
+
"@bamboocss/logger": "1.29.0",
|
|
59
|
+
"@bamboocss/parser": "1.29.0",
|
|
60
|
+
"@bamboocss/plugin-lightningcss": "1.29.0",
|
|
61
|
+
"@bamboocss/plugin-svelte": "1.29.0",
|
|
62
|
+
"@bamboocss/plugin-vue": "1.29.0",
|
|
63
|
+
"@bamboocss/reporter": "1.29.0",
|
|
64
|
+
"@bamboocss/shared": "1.29.0",
|
|
65
|
+
"@bamboocss/token-dictionary": "1.29.0",
|
|
66
|
+
"@bamboocss/types": "1.29.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/fs-extra": "11.0.4",
|