@bamboocss/node 1.28.1 → 1.30.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 +430 -96
- package/dist/index.mjs +430 -96
- 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
|
},
|
|
@@ -1791,7 +1810,7 @@ var BambooContext = class extends _bamboocss_generator.Generator {
|
|
|
1791
1810
|
poll
|
|
1792
1811
|
}).on("change", (0, perfect_debounce.debounce)(async (file) => {
|
|
1793
1812
|
_bamboocss_logger.logger.info("ctx:change", "config changed, rebuilding...");
|
|
1794
|
-
await cb(file);
|
|
1813
|
+
await reportRebuildFailure(() => cb(file));
|
|
1795
1814
|
}));
|
|
1796
1815
|
};
|
|
1797
1816
|
watchFiles = (cb, opts) => {
|
|
@@ -1805,10 +1824,30 @@ var BambooContext = class extends _bamboocss_generator.Generator {
|
|
|
1805
1824
|
cwd
|
|
1806
1825
|
}).on("all", (0, perfect_debounce.debounce)(async (event, file) => {
|
|
1807
1826
|
_bamboocss_logger.logger.info(`file:${event}`, file);
|
|
1808
|
-
await cb(event, file);
|
|
1827
|
+
await reportRebuildFailure(() => cb(event, file));
|
|
1809
1828
|
}));
|
|
1810
1829
|
};
|
|
1811
1830
|
};
|
|
1831
|
+
/**
|
|
1832
|
+
* Report a failed rebuild as a failed rebuild.
|
|
1833
|
+
*
|
|
1834
|
+
* Chokidar is an `EventEmitter`, so it discards whatever a listener returns, and the debounce
|
|
1835
|
+
* wrapper attaches no rejection handler — a throw from inside a rebuild became a dangling
|
|
1836
|
+
* promise. `node-runtime.ts` then catches it as `Unhandled rejection`, which labels a config
|
|
1837
|
+
* error as an internal crash, leaves the exit code at 0, and is suppressed entirely at
|
|
1838
|
+
* `logLevel: 'silent'`. The initial build is caught by the CLI and prints properly; only
|
|
1839
|
+
* rebuilds of the identical source were silent, which is the worse half of the asymmetry.
|
|
1840
|
+
*
|
|
1841
|
+
* Caught rather than rethrown: a watcher's job is to survive a bad intermediate state and
|
|
1842
|
+
* rebuild when the next edit fixes it. What it must not do is claim success.
|
|
1843
|
+
*/
|
|
1844
|
+
async function reportRebuildFailure(run) {
|
|
1845
|
+
try {
|
|
1846
|
+
await run();
|
|
1847
|
+
} catch (error) {
|
|
1848
|
+
_bamboocss_logger.logger.error("ctx:rebuild", error instanceof Error ? error.message : String(error));
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1812
1851
|
//#endregion
|
|
1813
1852
|
//#region src/tsconfig-utils.ts
|
|
1814
1853
|
/**
|
|
@@ -2114,16 +2153,330 @@ function parseDependency(fileOrGlob) {
|
|
|
2114
2153
|
return message;
|
|
2115
2154
|
}
|
|
2116
2155
|
//#endregion
|
|
2156
|
+
//#region src/source-snapshots.ts
|
|
2157
|
+
/**
|
|
2158
|
+
* Every file `include` covers, read once.
|
|
2159
|
+
*
|
|
2160
|
+
* The scans that decide what survives pruning — the token reference set, the reachability
|
|
2161
|
+
* gate, the strict accounting — all want the same two copies of the same files, and each used
|
|
2162
|
+
* to fetch them itself. A strict build therefore read every file three times over: once to
|
|
2163
|
+
* collect references, once to account, and once more for the gate whenever the accounting
|
|
2164
|
+
* declined. Yielding a snapshot lets one walk feed all of them.
|
|
2165
|
+
*
|
|
2166
|
+
* Both copies, because neither alone is complete and the text scans are safe when over-fed. A
|
|
2167
|
+
* file the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`),
|
|
2168
|
+
* and those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2169
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2170
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2171
|
+
* like that reports no tokens and no elements at all.
|
|
2172
|
+
*
|
|
2173
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2174
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2175
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2176
|
+
*/
|
|
2177
|
+
function* sourceSnapshots(ctx) {
|
|
2178
|
+
for (const file of ctx.getFiles()) {
|
|
2179
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2180
|
+
let onDisk;
|
|
2181
|
+
try {
|
|
2182
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2183
|
+
} catch {
|
|
2184
|
+
onDisk = void 0;
|
|
2185
|
+
}
|
|
2186
|
+
const sourceFile = ctx.project.getSourceFile(filePath);
|
|
2187
|
+
yield {
|
|
2188
|
+
filePath,
|
|
2189
|
+
onDisk,
|
|
2190
|
+
parsed: sourceFile?.getFullText(),
|
|
2191
|
+
sourceFile
|
|
2192
|
+
};
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* The distinct texts a snapshot carries.
|
|
2197
|
+
*
|
|
2198
|
+
* One when the parser holds the file as written, two when a transform rewrote it. Every
|
|
2199
|
+
* textual scan wants both, and wants neither twice.
|
|
2200
|
+
*/
|
|
2201
|
+
function* snapshotTexts(snapshot) {
|
|
2202
|
+
const { onDisk, parsed } = snapshot;
|
|
2203
|
+
if (onDisk != null) yield onDisk;
|
|
2204
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2205
|
+
}
|
|
2206
|
+
//#endregion
|
|
2207
|
+
//#region src/token-accounting.ts
|
|
2208
|
+
/**
|
|
2209
|
+
* The one decline `strict` fails the build on.
|
|
2210
|
+
*
|
|
2211
|
+
* `strict` asserts that every token path resolves, and this is the reason that says otherwise:
|
|
2212
|
+
* a token binding used in a way the build cannot follow — a path built at runtime, a binding
|
|
2213
|
+
* assigned away, a namespace enumerated. It is about the author's *token usage*, and it has a
|
|
2214
|
+
* fix at the call site.
|
|
2215
|
+
*
|
|
2216
|
+
* Nothing else may throw, because nothing else is necessarily about tokens at all. The other
|
|
2217
|
+
* reasons were written under a premise this would break: declining was free, so every branch
|
|
2218
|
+
* that could not prove a shape declined, and the accepted set was kept deliberately small.
|
|
2219
|
+
* `import(`./pages/${name}`)` declines as `dynamic-import` because the specifier is not a
|
|
2220
|
+
* literal and *could* be the artifact; `import lexer = require('./tokenizer')` declines because
|
|
2221
|
+
* the statement contains the substring `token`. Both are routine code with nothing to do with
|
|
2222
|
+
* design tokens, and failing a build over them would be indefensible. They keep every
|
|
2223
|
+
* declaration, which is what they always did, and say so.
|
|
2224
|
+
*/
|
|
2225
|
+
const failsStrict = (entry) => entry.reason === "unresolved-reference";
|
|
2226
|
+
/**
|
|
2227
|
+
* One file's contribution, split out so the build can account and scan in a single walk.
|
|
2228
|
+
*
|
|
2229
|
+
* `pruneTokensForBuild` needs the keep set, the reachability answer and this accounting from
|
|
2230
|
+
* the same files; three separate passes read every file three times.
|
|
2231
|
+
*/
|
|
2232
|
+
function accountSnapshot(ctx, snapshot, accounting) {
|
|
2233
|
+
const { filePath, onDisk, parsed, sourceFile } = snapshot;
|
|
2234
|
+
const { paths, prefixes, declined } = accounting;
|
|
2235
|
+
if (onDisk == null || parsed == null || parsed !== onDisk) {
|
|
2236
|
+
if (!((onDisk?.includes("token") ?? false) || (parsed?.includes("token") ?? false))) return;
|
|
2237
|
+
declined.push({
|
|
2238
|
+
filePath,
|
|
2239
|
+
line: 1,
|
|
2240
|
+
reason: onDisk == null || parsed == null ? "unreadable" : "transformed"
|
|
2241
|
+
});
|
|
2242
|
+
return;
|
|
2243
|
+
}
|
|
2244
|
+
if (parseErrorCount(sourceFile)) {
|
|
2245
|
+
if (!parsed.includes("token")) return;
|
|
2246
|
+
declined.push({
|
|
2247
|
+
filePath,
|
|
2248
|
+
line: 1,
|
|
2249
|
+
reason: "unparsed"
|
|
2250
|
+
});
|
|
2251
|
+
return;
|
|
2252
|
+
}
|
|
2253
|
+
accountFile(ctx, sourceFile, filePath, paths, prefixes, declined);
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* How many syntax errors the parse produced.
|
|
2257
|
+
*
|
|
2258
|
+
* `parseDiagnostics` is TypeScript-internal and absent from the public `SourceFile` type, so
|
|
2259
|
+
* it is reached through a cast. The alternative, `getPreEmitDiagnostics`, runs the type checker
|
|
2260
|
+
* over the whole program — orders of magnitude more work for a question about syntax, and it
|
|
2261
|
+
* would report type errors this pass has no business declining over.
|
|
2262
|
+
*/
|
|
2263
|
+
const parseErrorCount = (sourceFile) => sourceFile.compilerNode.parseDiagnostics?.length ?? 0;
|
|
2264
|
+
/** Whether a module specifier resolves to the generated tokens artifact. */
|
|
2265
|
+
const isTokensEntrypoint = (ctx, specifier) => {
|
|
2266
|
+
const mods = ctx.imports.value.tokens;
|
|
2267
|
+
if (mods.some((mod) => specifier.includes(mod))) return true;
|
|
2268
|
+
const pathMappings = ctx.conf.tsOptions?.pathMappings;
|
|
2269
|
+
if (!pathMappings) return false;
|
|
2270
|
+
const resolved = (0, _bamboocss_config_ts_path.resolveTsPathPattern)(pathMappings, specifier);
|
|
2271
|
+
if (!resolved) return false;
|
|
2272
|
+
return mods.some((mod) => resolved.includes(mod) || resolved === mod);
|
|
2273
|
+
};
|
|
2274
|
+
const lineOf = (node) => node.getSourceFile().getLineAndColumnAtPos(node.getStart()).line;
|
|
2275
|
+
function accountFile(ctx, sourceFile, filePath, paths, prefixes, declined) {
|
|
2276
|
+
const decline = (node, reason) => declined.push({
|
|
2277
|
+
filePath,
|
|
2278
|
+
line: lineOf(node),
|
|
2279
|
+
reason
|
|
2280
|
+
});
|
|
2281
|
+
/** Local names bound to the artifact: the `token` export, and any namespace of it. */
|
|
2282
|
+
const bindings = /* @__PURE__ */ new Set();
|
|
2283
|
+
for (const statement of sourceFile.getStatements()) {
|
|
2284
|
+
if (ts_morph.Node.isExportDeclaration(statement)) {
|
|
2285
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2286
|
+
if (specifier && isTokensEntrypoint(ctx, specifier)) decline(statement, "re-exported");
|
|
2287
|
+
continue;
|
|
2288
|
+
}
|
|
2289
|
+
if (!ts_morph.Node.isImportDeclaration(statement)) continue;
|
|
2290
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2291
|
+
if (!specifier) continue;
|
|
2292
|
+
const clause = statement.getImportClause();
|
|
2293
|
+
if (!clause || statement.isTypeOnly()) continue;
|
|
2294
|
+
if (!isTokensEntrypoint(ctx, specifier)) {
|
|
2295
|
+
for (const named of clause.getNamedImports()) if (!named.isTypeOnly() && nameOf(named.getNameNode()) === "token") decline(named, "unclassified-import");
|
|
2296
|
+
const foreignDefault = clause.getDefaultImport();
|
|
2297
|
+
if (foreignDefault && nameOf(foreignDefault) === "token") decline(foreignDefault, "unclassified-import");
|
|
2298
|
+
const foreignNamespace = clause.getNamespaceImport();
|
|
2299
|
+
if (foreignNamespace && usesTokenMember(sourceFile, foreignNamespace.getText())) decline(foreignNamespace, "unclassified-import");
|
|
2300
|
+
continue;
|
|
2301
|
+
}
|
|
2302
|
+
const defaultImport = clause.getDefaultImport();
|
|
2303
|
+
if (defaultImport) decline(defaultImport, "unsupported-import");
|
|
2304
|
+
const namespace = clause.getNamespaceImport();
|
|
2305
|
+
if (namespace) bindings.add(namespace.getText());
|
|
2306
|
+
for (const named of clause.getNamedImports()) {
|
|
2307
|
+
if (named.isTypeOnly()) continue;
|
|
2308
|
+
if (nameOf(named.getNameNode()) !== "token") {
|
|
2309
|
+
if (usedAsValue(sourceFile, (named.getAliasNode() ?? named.getNameNode()).getText(), named)) decline(named, "unsupported-import");
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2312
|
+
bindings.add((named.getAliasNode() ?? named.getNameNode()).getText());
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
for (const importEquals of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.ImportEqualsDeclaration)) if (importEquals.getText().includes("token")) decline(importEquals, "import-equals");
|
|
2316
|
+
for (const call of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.CallExpression)) {
|
|
2317
|
+
const callee = call.getExpression();
|
|
2318
|
+
const isRequire = ts_morph.Node.isIdentifier(callee) && callee.getText() === "require";
|
|
2319
|
+
const isDynamicImport = callee.getKind() === ts_morph.SyntaxKind.ImportKeyword;
|
|
2320
|
+
if (!isRequire && !isDynamicImport) continue;
|
|
2321
|
+
const argument = call.getArguments()[0];
|
|
2322
|
+
if (!argument) continue;
|
|
2323
|
+
if (ts_morph.Node.isStringLiteral(argument) ? isTokensEntrypoint(ctx, argument.getLiteralValue()) : true) decline(call, isRequire ? "require" : "dynamic-import");
|
|
2324
|
+
}
|
|
2325
|
+
for (const identifier of sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.Identifier)) {
|
|
2326
|
+
const text = nameOf(identifier);
|
|
2327
|
+
if (!bindings.has(text) && text !== "token") continue;
|
|
2328
|
+
const parent = identifier.getParent();
|
|
2329
|
+
if (ts_morph.Node.isPropertyAccessExpression(parent) && parent.getNameNode() === identifier) {
|
|
2330
|
+
const object = parent.getExpression();
|
|
2331
|
+
const objectName = ts_morph.Node.isIdentifier(object) ? object.getText() : void 0;
|
|
2332
|
+
if (objectName && bindings.has(objectName)) continue;
|
|
2333
|
+
decline(identifier, "unresolved-reference");
|
|
2334
|
+
continue;
|
|
2335
|
+
}
|
|
2336
|
+
if (ts_morph.Node.isPropertyAssignment(parent) && parent.getNameNode() === identifier) continue;
|
|
2337
|
+
const resolved = accountedPath(identifier);
|
|
2338
|
+
if (resolved === void 0) {
|
|
2339
|
+
decline(identifier, "unresolved-reference");
|
|
2340
|
+
continue;
|
|
2341
|
+
}
|
|
2342
|
+
if (resolved.kind === "binding") continue;
|
|
2343
|
+
if (resolved.kind === "prefix") {
|
|
2344
|
+
prefixes.add(resolved.value);
|
|
2345
|
+
continue;
|
|
2346
|
+
}
|
|
2347
|
+
paths.add(resolved.value);
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
/**
|
|
2351
|
+
* Whether any `<namespace>.token` member access appears, for an unclassified namespace.
|
|
2352
|
+
*
|
|
2353
|
+
* Element access counts too: `ui['token'](k)` reaches the same export and is invisible to a
|
|
2354
|
+
* property-access-only scan.
|
|
2355
|
+
*/
|
|
2356
|
+
function usesTokenMember(sourceFile, namespace) {
|
|
2357
|
+
if (sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.PropertyAccessExpression).some((access) => access.getExpression().getText() === namespace && nameOf(access.getNameNode()) === "token")) return true;
|
|
2358
|
+
return sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.ElementAccessExpression).some((access) => {
|
|
2359
|
+
if (access.getExpression().getText() !== namespace) return false;
|
|
2360
|
+
const argument = access.getArgumentExpression();
|
|
2361
|
+
return argument != null && (!ts_morph.Node.isStringLiteral(argument) || argument.getLiteralValue() === "token");
|
|
2362
|
+
});
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* An identifier's name as the compiler resolves it, not as it is spelled.
|
|
2366
|
+
*
|
|
2367
|
+
* `token` is the identifier `token`; reading `getText()` returns the escape and compares
|
|
2368
|
+
* unequal, which let an import of the artifact's export past the checks that key on the name.
|
|
2369
|
+
*/
|
|
2370
|
+
const nameOf = (node) => ts_morph.Node.isIdentifier(node) ? String(node.compilerNode.escapedText) : node.getText();
|
|
2371
|
+
/**
|
|
2372
|
+
* What one occurrence asks for, or `undefined` if it is not a call this can read.
|
|
2373
|
+
*
|
|
2374
|
+
* Accepts the callee position and nothing else — `token('x')`, `token.value('x')`, and the
|
|
2375
|
+
* namespaced spellings of each. An identifier anywhere else is a value
|
|
2376
|
+
* escaping somewhere this pass cannot follow: assigned (`const t = token`), passed
|
|
2377
|
+
* (`useMemo(() => token)`), spread, or enumerated.
|
|
2378
|
+
*/
|
|
2379
|
+
function accountedPath(identifier) {
|
|
2380
|
+
const parent = identifier.getParent();
|
|
2381
|
+
if (!parent) return void 0;
|
|
2382
|
+
if (ts_morph.Node.isImportSpecifier(parent) || ts_morph.Node.isNamespaceImport(parent) || ts_morph.Node.isImportClause(parent) || ts_morph.Node.isExportSpecifier(parent)) return { kind: "binding" };
|
|
2383
|
+
if (ts_morph.Node.isCallExpression(parent) && parent.getExpression() === identifier) return literalPath(parent);
|
|
2384
|
+
if (!ts_morph.Node.isPropertyAccessExpression(parent) || parent.getExpression() !== identifier) return void 0;
|
|
2385
|
+
const property = parent.getNameNode().getText();
|
|
2386
|
+
const grandParent = parent.getParent();
|
|
2387
|
+
if (property === "value") return ts_morph.Node.isCallExpression(grandParent) && grandParent.getExpression() === parent ? literalPath(grandParent) : void 0;
|
|
2388
|
+
if (property !== "token") return void 0;
|
|
2389
|
+
if (ts_morph.Node.isCallExpression(grandParent) && grandParent.getExpression() === parent) return literalPath(grandParent);
|
|
2390
|
+
if (ts_morph.Node.isPropertyAccessExpression(grandParent) && grandParent.getExpression() === parent) {
|
|
2391
|
+
if (grandParent.getNameNode().getText() !== "value") return void 0;
|
|
2392
|
+
const call = grandParent.getParent();
|
|
2393
|
+
return ts_morph.Node.isCallExpression(call) && call.getExpression() === grandParent ? literalPath(call) : void 0;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
/**
|
|
2397
|
+
* The path a call asks for, or the prefix it is bounded by.
|
|
2398
|
+
*
|
|
2399
|
+
* Read through `getLiteralValue()`, never off the source text. The text carries escapes —
|
|
2400
|
+
* `token('colors.red.300')` — and a path recorded raw looks up nothing, which
|
|
2401
|
+
* would accept a reference and keep no declaration for it. That is the exact failure this
|
|
2402
|
+
* module exists to make unrepresentable.
|
|
2403
|
+
*
|
|
2404
|
+
* A template literal with substitutions is not a path, but it is not unbounded either: its
|
|
2405
|
+
* head is a prefix everything it can produce begins with. Declining one cost every token
|
|
2406
|
+
* declaration in the project; bounding it costs the category. The head is the *whole* answer —
|
|
2407
|
+
* `` `colors.${a}.${b}` `` bounds no more tightly than `` `colors.${x}` `` does — and an empty
|
|
2408
|
+
* head bounds nothing at all, so that still declines.
|
|
2409
|
+
*/
|
|
2410
|
+
function literalPath(call) {
|
|
2411
|
+
if (!ts_morph.Node.isCallExpression(call)) return void 0;
|
|
2412
|
+
const argument = unwrapAssertions(call.getArguments()[0]);
|
|
2413
|
+
if (!argument) return void 0;
|
|
2414
|
+
if (ts_morph.Node.isStringLiteral(argument)) return {
|
|
2415
|
+
kind: "path",
|
|
2416
|
+
value: argument.getLiteralValue()
|
|
2417
|
+
};
|
|
2418
|
+
if (ts_morph.Node.isNoSubstitutionTemplateLiteral(argument)) return {
|
|
2419
|
+
kind: "path",
|
|
2420
|
+
value: argument.getLiteralValue()
|
|
2421
|
+
};
|
|
2422
|
+
if (ts_morph.Node.isTemplateExpression(argument)) {
|
|
2423
|
+
const head = argument.getHead().getLiteralText();
|
|
2424
|
+
return head ? {
|
|
2425
|
+
kind: "prefix",
|
|
2426
|
+
value: head
|
|
2427
|
+
} : void 0;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
/**
|
|
2431
|
+
* Whether a binding is read anywhere outside a type position.
|
|
2432
|
+
*
|
|
2433
|
+
* `import { Token, token }` brings in a type beside the value, and a type cannot produce a
|
|
2434
|
+
* `var()` — but a binding that is *called* can, whatever it is named, because a barrel
|
|
2435
|
+
* matching the specifier test could re-export `token` under it.
|
|
2436
|
+
*/
|
|
2437
|
+
function usedAsValue(sourceFile, name, declaration) {
|
|
2438
|
+
return sourceFile.getDescendantsOfKind(ts_morph.SyntaxKind.Identifier).some((identifier) => {
|
|
2439
|
+
if (nameOf(identifier) !== name) return false;
|
|
2440
|
+
if (identifier.getFirstAncestor((ancestor) => ancestor === declaration)) return false;
|
|
2441
|
+
return !identifier.getFirstAncestor((ancestor) => ts_morph.Node.isTypeNode(ancestor));
|
|
2442
|
+
});
|
|
2443
|
+
}
|
|
2444
|
+
/**
|
|
2445
|
+
* Strip the wrappers that carry no runtime meaning.
|
|
2446
|
+
*
|
|
2447
|
+
* `` token(`animations.${name}` as Token) `` is the shape a *typed* caller writes, and has to
|
|
2448
|
+
* be: the generated `Token` type is a union of template literals, so a `string`-typed
|
|
2449
|
+
* substitution does not typecheck without the assertion. Reading only the outermost node
|
|
2450
|
+
* declined exactly the call this bounding exists for — the one in this repository's own
|
|
2451
|
+
* documentation site.
|
|
2452
|
+
*
|
|
2453
|
+
* Each of these evaluates to its inner expression, so unwrapping changes nothing about what
|
|
2454
|
+
* the call receives.
|
|
2455
|
+
*/
|
|
2456
|
+
function unwrapAssertions(node) {
|
|
2457
|
+
let current = node;
|
|
2458
|
+
while (current && (ts_morph.Node.isAsExpression(current) || ts_morph.Node.isSatisfiesExpression(current) || ts_morph.Node.isNonNullExpression(current) || ts_morph.Node.isParenthesizedExpression(current) || ts_morph.Node.isTypeAssertion(current))) current = current.getExpression();
|
|
2459
|
+
return current;
|
|
2460
|
+
}
|
|
2461
|
+
//#endregion
|
|
2117
2462
|
//#region src/token-references.ts
|
|
2118
2463
|
/**
|
|
2119
|
-
* `token
|
|
2464
|
+
* `token('spacing.4')` and `token.value('colors.red.300')`, including the whitespace a
|
|
2120
2465
|
* formatter may leave behind. The parser reports both, resolving constants and template
|
|
2121
|
-
* literals through them — `token.
|
|
2466
|
+
* literals through them — `token.value()` included, since it is recorded as its own kind
|
|
2122
2467
|
* rather than dropped for having a property access as its callee. Scanning the text still
|
|
2123
2468
|
* earns its place twice over: it covers a path built somewhere the extractor cannot
|
|
2124
2469
|
* follow, and it covers the callers below that supply no `results` at all.
|
|
2125
2470
|
*/
|
|
2126
|
-
const TOKEN_CALL = /\btoken(?:\s*\.\s*
|
|
2471
|
+
const TOKEN_CALL = /\btoken(?:\s*\.\s*value)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2472
|
+
/**
|
|
2473
|
+
* A token reached from javascript at all — a call of any shape, or an import of the artifact.
|
|
2474
|
+
*
|
|
2475
|
+
* Has to stay a strict superset of `TOKEN_CALL`: it must match wherever that one matches, and
|
|
2476
|
+
* also wherever that one gives up. See `collectTokenReferences` for why the redundancy holds.
|
|
2477
|
+
* No `g` flag, so `test` carries no state between files.
|
|
2478
|
+
*/
|
|
2479
|
+
const REACHABLE_FROM_JS = /\btoken(\s*\.\s*value)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2127
2480
|
/**
|
|
2128
2481
|
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2129
2482
|
* parser understands it.
|
|
@@ -2144,67 +2497,86 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2144
2497
|
* is an order of magnitude more.
|
|
2145
2498
|
*/
|
|
2146
2499
|
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
|
-
}
|
|
2500
|
+
for (const snapshot of sourceSnapshots(ctx)) yield* snapshotTexts(snapshot);
|
|
2159
2501
|
}
|
|
2160
2502
|
/**
|
|
2161
|
-
*
|
|
2162
|
-
*
|
|
2163
|
-
* This is deliberately textual and therefore over-inclusive: a match inside a comment or
|
|
2164
|
-
* a string that is never evaluated keeps a token alive. Keeping a token that is not used
|
|
2165
|
-
* costs bytes; dropping one that is breaks the page, so the bias belongs on this side.
|
|
2503
|
+
* The token prune, as all three build paths need to run it.
|
|
2166
2504
|
*
|
|
2167
|
-
*
|
|
2168
|
-
*
|
|
2169
|
-
*
|
|
2170
|
-
*
|
|
2171
|
-
*
|
|
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.
|
|
2505
|
+
* One function because it was three copies of one conditional, and the `false` branch went
|
|
2506
|
+
* missing from a copy: a watch rebuild with `pruneUnusedTokens: false` skipped `pruneTokens`
|
|
2507
|
+
* altogether, so it kept `@property` registrations that a full build of the same source
|
|
2508
|
+
* strips. The other two copies carried a comment pointing at that file for the reasoning,
|
|
2509
|
+
* which is how a missing branch reads as intentional.
|
|
2175
2510
|
*
|
|
2176
|
-
*
|
|
2177
|
-
*
|
|
2178
|
-
* for
|
|
2179
|
-
*
|
|
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.
|
|
2511
|
+
* Opting out still drops the registrations. Those are not tokens — nothing hands one to
|
|
2512
|
+
* javascript, and none appear in the `token()` surface — so the reachability problem the flag
|
|
2513
|
+
* exists for does not apply to them, and opting out of token pruning should not mean shipping
|
|
2514
|
+
* a preset's whole filter and gradient set for nothing.
|
|
2192
2515
|
*/
|
|
2193
|
-
function
|
|
2516
|
+
function pruneTokensForBuild(ctx, sheet, results) {
|
|
2517
|
+
if (!ctx.config.pruneUnusedTokens) {
|
|
2518
|
+
ctx.pruneTokens(sheet);
|
|
2519
|
+
return;
|
|
2520
|
+
}
|
|
2521
|
+
const strict = ctx.config.pruneUnusedTokens === "strict";
|
|
2194
2522
|
const paths = /* @__PURE__ */ new Set();
|
|
2195
2523
|
const vars = /* @__PURE__ */ new Set();
|
|
2524
|
+
const accounting = {
|
|
2525
|
+
paths: /* @__PURE__ */ new Set(),
|
|
2526
|
+
prefixes: /* @__PURE__ */ new Set(),
|
|
2527
|
+
declined: []
|
|
2528
|
+
};
|
|
2529
|
+
let reachable = false;
|
|
2196
2530
|
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
|
-
|
|
2531
|
+
for (const snapshot of sourceSnapshots(ctx)) {
|
|
2532
|
+
for (const text of snapshotTexts(snapshot)) {
|
|
2533
|
+
for (const match of text.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2534
|
+
for (const name of (0, _bamboocss_shared.cssVarRefs)(text)) vars.add(name);
|
|
2535
|
+
if (!reachable && REACHABLE_FROM_JS.test(text)) reachable = true;
|
|
2536
|
+
}
|
|
2537
|
+
if (strict) accountSnapshot(ctx, snapshot, accounting);
|
|
2538
|
+
}
|
|
2539
|
+
for (const name of tokenVarsFor(ctx, paths)) vars.add(name);
|
|
2540
|
+
if (!strict) {
|
|
2541
|
+
ctx.pruneTokens(sheet, vars, reachable);
|
|
2542
|
+
return;
|
|
2200
2543
|
}
|
|
2544
|
+
for (const name of tokenVarsFor(ctx, accounting.paths)) vars.add(name);
|
|
2545
|
+
if (accounting.prefixes.size && !accounting.declined.length) {
|
|
2546
|
+
const prefixes = Array.from(accounting.prefixes);
|
|
2547
|
+
const matched = [];
|
|
2548
|
+
for (const token of ctx.tokens.allTokens) if (prefixes.some((prefix) => token.name.startsWith(prefix))) matched.push(token.name);
|
|
2549
|
+
for (const name of tokenVarsFor(ctx, matched)) vars.add(name);
|
|
2550
|
+
}
|
|
2551
|
+
const failing = accounting.declined.filter(failsStrict);
|
|
2552
|
+
const reported = accounting.declined.filter((entry) => !failsStrict(entry));
|
|
2553
|
+
if (failing.length) throw new _bamboocss_shared.BambooError("TOKEN_REFERENCE_UNRESOLVED", `${failing.length} token reference(s) could not be resolved.\n\n${formatDeclined(ctx, failing)}\n\n\`pruneUnusedTokens: 'strict'\` asserts that every token path resolves at build time, so this is an error rather than a silent fallback. Spell the path as a string literal at the call, give a template a static prefix so it can be bounded, move the token into \`staticCss\`, or set \`pruneUnusedTokens: true\` to stop asserting.`);
|
|
2554
|
+
if (reported.length) _bamboocss_logger.logger.warn("tokens:strict", `${reported.length} reference(s) could not be accounted for, so every token declaration is kept.\n\n${formatDeclined(ctx, reported)}\n\nThese are shapes the build cannot follow rather than paths you can respell — a component stored post-transform, a file it could not parse, a barrel it cannot classify, a dynamic \`import()\`. Narrow \`include\`, or accept the keeps.`);
|
|
2555
|
+
ctx.pruneTokens(sheet, vars, accounting.declined.length > 0 && reachable);
|
|
2556
|
+
}
|
|
2557
|
+
/** The custom properties a set of token paths resolves to. */
|
|
2558
|
+
function tokenVarsFor(ctx, paths) {
|
|
2559
|
+
const vars = /* @__PURE__ */ new Set();
|
|
2201
2560
|
for (const path of paths) {
|
|
2561
|
+
if (!path) continue;
|
|
2202
2562
|
const ref = ctx.tokens.view.getVar(path);
|
|
2203
2563
|
if (!ref) continue;
|
|
2204
2564
|
for (const name of (0, _bamboocss_shared.cssVarRefs)(ref)) vars.add(name);
|
|
2205
2565
|
}
|
|
2206
2566
|
return vars;
|
|
2207
2567
|
}
|
|
2568
|
+
/** Where to look, grouped by file. The surrounding text differs by whether this throws. */
|
|
2569
|
+
function formatDeclined(ctx, declined) {
|
|
2570
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
2571
|
+
for (const entry of declined) {
|
|
2572
|
+
const list = byFile.get(entry.filePath) ?? [];
|
|
2573
|
+
list.push(entry);
|
|
2574
|
+
byFile.set(entry.filePath, list);
|
|
2575
|
+
}
|
|
2576
|
+
return Array.from(byFile.entries()).map(([filePath, entries]) => {
|
|
2577
|
+
return [` ${filePath.startsWith(ctx.config.cwd) ? filePath.slice(ctx.config.cwd.length + 1) : filePath}`, ...entries.map((entry) => ` ${entry.line}: ${entry.reason}`)].join("\n");
|
|
2578
|
+
}).join("\n");
|
|
2579
|
+
}
|
|
2208
2580
|
/**
|
|
2209
2581
|
* Collect keyframe names that reading the generated css cannot reveal.
|
|
2210
2582
|
*
|
|
@@ -2263,41 +2635,6 @@ function collectRenderedElements(ctx) {
|
|
|
2263
2635
|
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2264
2636
|
return found;
|
|
2265
2637
|
}
|
|
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
2638
|
//#endregion
|
|
2302
2639
|
//#region src/builder.ts
|
|
2303
2640
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2480,8 +2817,7 @@ var Builder = class {
|
|
|
2480
2817
|
const sheet = ctx.createSheet();
|
|
2481
2818
|
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2482
2819
|
ctx.appendBaselineCss(sheet);
|
|
2483
|
-
|
|
2484
|
-
else ctx.pruneTokens(sheet);
|
|
2820
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2485
2821
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2486
2822
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2487
2823
|
return ctx.getCss(sheet);
|
|
@@ -2586,8 +2922,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2586
2922
|
ctx.appendBaselineCss(sheet);
|
|
2587
2923
|
}
|
|
2588
2924
|
ctx.appendParserCss(sheet);
|
|
2589
|
-
if (!minimal
|
|
2590
|
-
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2925
|
+
if (!minimal) pruneTokensForBuild(ctx, sheet, results);
|
|
2591
2926
|
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2592
2927
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2593
2928
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
@@ -2659,8 +2994,7 @@ async function build(ctx, artifactIds) {
|
|
|
2659
2994
|
ctx.appendBaselineCss(sheet);
|
|
2660
2995
|
const parsed = ctx.parseFiles();
|
|
2661
2996
|
ctx.appendParserCss(sheet);
|
|
2662
|
-
|
|
2663
|
-
else ctx.pruneTokens(sheet);
|
|
2997
|
+
pruneTokensForBuild(ctx, sheet, parsed.results);
|
|
2664
2998
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2665
2999
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2666
3000
|
await ctx.writeCss(sheet);
|
|
@@ -2706,7 +3040,7 @@ async function generate(config, configPath) {
|
|
|
2706
3040
|
ctx.appendLayerParams(sheet);
|
|
2707
3041
|
ctx.appendBaselineCss(sheet);
|
|
2708
3042
|
ctx.appendParserCss(sheet);
|
|
2709
|
-
|
|
3043
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2710
3044
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2711
3045
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2712
3046
|
const css = ctx.getCss(sheet);
|
|
@@ -2810,7 +3144,7 @@ export default defineConfig({
|
|
|
2810
3144
|
|
|
2811
3145
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2812
3146
|
// 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
|
|
3147
|
+
// if you read tokens from somewhere the build cannot see them — \`token()\` with a
|
|
2814
3148
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2815
3149
|
// \`staticCss\` to keep them.
|
|
2816
3150
|
|
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
|
},
|
|
@@ -1776,7 +1795,7 @@ var BambooContext = class extends Generator {
|
|
|
1776
1795
|
poll
|
|
1777
1796
|
}).on("change", debounce(async (file) => {
|
|
1778
1797
|
logger.info("ctx:change", "config changed, rebuilding...");
|
|
1779
|
-
await cb(file);
|
|
1798
|
+
await reportRebuildFailure(() => cb(file));
|
|
1780
1799
|
}));
|
|
1781
1800
|
};
|
|
1782
1801
|
watchFiles = (cb, opts) => {
|
|
@@ -1790,10 +1809,30 @@ var BambooContext = class extends Generator {
|
|
|
1790
1809
|
cwd
|
|
1791
1810
|
}).on("all", debounce(async (event, file) => {
|
|
1792
1811
|
logger.info(`file:${event}`, file);
|
|
1793
|
-
await cb(event, file);
|
|
1812
|
+
await reportRebuildFailure(() => cb(event, file));
|
|
1794
1813
|
}));
|
|
1795
1814
|
};
|
|
1796
1815
|
};
|
|
1816
|
+
/**
|
|
1817
|
+
* Report a failed rebuild as a failed rebuild.
|
|
1818
|
+
*
|
|
1819
|
+
* Chokidar is an `EventEmitter`, so it discards whatever a listener returns, and the debounce
|
|
1820
|
+
* wrapper attaches no rejection handler — a throw from inside a rebuild became a dangling
|
|
1821
|
+
* promise. `node-runtime.ts` then catches it as `Unhandled rejection`, which labels a config
|
|
1822
|
+
* error as an internal crash, leaves the exit code at 0, and is suppressed entirely at
|
|
1823
|
+
* `logLevel: 'silent'`. The initial build is caught by the CLI and prints properly; only
|
|
1824
|
+
* rebuilds of the identical source were silent, which is the worse half of the asymmetry.
|
|
1825
|
+
*
|
|
1826
|
+
* Caught rather than rethrown: a watcher's job is to survive a bad intermediate state and
|
|
1827
|
+
* rebuild when the next edit fixes it. What it must not do is claim success.
|
|
1828
|
+
*/
|
|
1829
|
+
async function reportRebuildFailure(run) {
|
|
1830
|
+
try {
|
|
1831
|
+
await run();
|
|
1832
|
+
} catch (error) {
|
|
1833
|
+
logger.error("ctx:rebuild", error instanceof Error ? error.message : String(error));
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1797
1836
|
//#endregion
|
|
1798
1837
|
//#region src/tsconfig-utils.ts
|
|
1799
1838
|
/**
|
|
@@ -2099,16 +2138,330 @@ function parseDependency(fileOrGlob) {
|
|
|
2099
2138
|
return message;
|
|
2100
2139
|
}
|
|
2101
2140
|
//#endregion
|
|
2141
|
+
//#region src/source-snapshots.ts
|
|
2142
|
+
/**
|
|
2143
|
+
* Every file `include` covers, read once.
|
|
2144
|
+
*
|
|
2145
|
+
* The scans that decide what survives pruning — the token reference set, the reachability
|
|
2146
|
+
* gate, the strict accounting — all want the same two copies of the same files, and each used
|
|
2147
|
+
* to fetch them itself. A strict build therefore read every file three times over: once to
|
|
2148
|
+
* collect references, once to account, and once more for the gate whenever the accounting
|
|
2149
|
+
* declined. Yielding a snapshot lets one walk feed all of them.
|
|
2150
|
+
*
|
|
2151
|
+
* Both copies, because neither alone is complete and the text scans are safe when over-fed. A
|
|
2152
|
+
* file the parser transformed is stored rewritten (`parseSourceFile` calls `replaceWithText`),
|
|
2153
|
+
* and those transforms lose things the scans want: `svelteToTsx` and `vueToTsx` each swallow a
|
|
2154
|
+
* throw and return an empty string, and a Vue SFC with a render function and no `<template>`
|
|
2155
|
+
* becomes the literal `<template>undefined</template>`. Read only the parsed copy and a file
|
|
2156
|
+
* like that reports no tokens and no elements at all.
|
|
2157
|
+
*
|
|
2158
|
+
* The parsed copy still has to be read as well, because `parser:before` is the documented way
|
|
2159
|
+
* to teach bamboo a format it does not know. A template compiled to jsx by such a hook holds
|
|
2160
|
+
* nothing a scan of the raw file would recognise, and that is the hook working as intended.
|
|
2161
|
+
*/
|
|
2162
|
+
function* sourceSnapshots(ctx) {
|
|
2163
|
+
for (const file of ctx.getFiles()) {
|
|
2164
|
+
const filePath = ctx.runtime.path.abs(ctx.config.cwd, file);
|
|
2165
|
+
let onDisk;
|
|
2166
|
+
try {
|
|
2167
|
+
onDisk = ctx.runtime.fs.readFileSync(filePath);
|
|
2168
|
+
} catch {
|
|
2169
|
+
onDisk = void 0;
|
|
2170
|
+
}
|
|
2171
|
+
const sourceFile = ctx.project.getSourceFile(filePath);
|
|
2172
|
+
yield {
|
|
2173
|
+
filePath,
|
|
2174
|
+
onDisk,
|
|
2175
|
+
parsed: sourceFile?.getFullText(),
|
|
2176
|
+
sourceFile
|
|
2177
|
+
};
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* The distinct texts a snapshot carries.
|
|
2182
|
+
*
|
|
2183
|
+
* One when the parser holds the file as written, two when a transform rewrote it. Every
|
|
2184
|
+
* textual scan wants both, and wants neither twice.
|
|
2185
|
+
*/
|
|
2186
|
+
function* snapshotTexts(snapshot) {
|
|
2187
|
+
const { onDisk, parsed } = snapshot;
|
|
2188
|
+
if (onDisk != null) yield onDisk;
|
|
2189
|
+
if (parsed != null && parsed !== onDisk) yield parsed;
|
|
2190
|
+
}
|
|
2191
|
+
//#endregion
|
|
2192
|
+
//#region src/token-accounting.ts
|
|
2193
|
+
/**
|
|
2194
|
+
* The one decline `strict` fails the build on.
|
|
2195
|
+
*
|
|
2196
|
+
* `strict` asserts that every token path resolves, and this is the reason that says otherwise:
|
|
2197
|
+
* a token binding used in a way the build cannot follow — a path built at runtime, a binding
|
|
2198
|
+
* assigned away, a namespace enumerated. It is about the author's *token usage*, and it has a
|
|
2199
|
+
* fix at the call site.
|
|
2200
|
+
*
|
|
2201
|
+
* Nothing else may throw, because nothing else is necessarily about tokens at all. The other
|
|
2202
|
+
* reasons were written under a premise this would break: declining was free, so every branch
|
|
2203
|
+
* that could not prove a shape declined, and the accepted set was kept deliberately small.
|
|
2204
|
+
* `import(`./pages/${name}`)` declines as `dynamic-import` because the specifier is not a
|
|
2205
|
+
* literal and *could* be the artifact; `import lexer = require('./tokenizer')` declines because
|
|
2206
|
+
* the statement contains the substring `token`. Both are routine code with nothing to do with
|
|
2207
|
+
* design tokens, and failing a build over them would be indefensible. They keep every
|
|
2208
|
+
* declaration, which is what they always did, and say so.
|
|
2209
|
+
*/
|
|
2210
|
+
const failsStrict = (entry) => entry.reason === "unresolved-reference";
|
|
2211
|
+
/**
|
|
2212
|
+
* One file's contribution, split out so the build can account and scan in a single walk.
|
|
2213
|
+
*
|
|
2214
|
+
* `pruneTokensForBuild` needs the keep set, the reachability answer and this accounting from
|
|
2215
|
+
* the same files; three separate passes read every file three times.
|
|
2216
|
+
*/
|
|
2217
|
+
function accountSnapshot(ctx, snapshot, accounting) {
|
|
2218
|
+
const { filePath, onDisk, parsed, sourceFile } = snapshot;
|
|
2219
|
+
const { paths, prefixes, declined } = accounting;
|
|
2220
|
+
if (onDisk == null || parsed == null || parsed !== onDisk) {
|
|
2221
|
+
if (!((onDisk?.includes("token") ?? false) || (parsed?.includes("token") ?? false))) return;
|
|
2222
|
+
declined.push({
|
|
2223
|
+
filePath,
|
|
2224
|
+
line: 1,
|
|
2225
|
+
reason: onDisk == null || parsed == null ? "unreadable" : "transformed"
|
|
2226
|
+
});
|
|
2227
|
+
return;
|
|
2228
|
+
}
|
|
2229
|
+
if (parseErrorCount(sourceFile)) {
|
|
2230
|
+
if (!parsed.includes("token")) return;
|
|
2231
|
+
declined.push({
|
|
2232
|
+
filePath,
|
|
2233
|
+
line: 1,
|
|
2234
|
+
reason: "unparsed"
|
|
2235
|
+
});
|
|
2236
|
+
return;
|
|
2237
|
+
}
|
|
2238
|
+
accountFile(ctx, sourceFile, filePath, paths, prefixes, declined);
|
|
2239
|
+
}
|
|
2240
|
+
/**
|
|
2241
|
+
* How many syntax errors the parse produced.
|
|
2242
|
+
*
|
|
2243
|
+
* `parseDiagnostics` is TypeScript-internal and absent from the public `SourceFile` type, so
|
|
2244
|
+
* it is reached through a cast. The alternative, `getPreEmitDiagnostics`, runs the type checker
|
|
2245
|
+
* over the whole program — orders of magnitude more work for a question about syntax, and it
|
|
2246
|
+
* would report type errors this pass has no business declining over.
|
|
2247
|
+
*/
|
|
2248
|
+
const parseErrorCount = (sourceFile) => sourceFile.compilerNode.parseDiagnostics?.length ?? 0;
|
|
2249
|
+
/** Whether a module specifier resolves to the generated tokens artifact. */
|
|
2250
|
+
const isTokensEntrypoint = (ctx, specifier) => {
|
|
2251
|
+
const mods = ctx.imports.value.tokens;
|
|
2252
|
+
if (mods.some((mod) => specifier.includes(mod))) return true;
|
|
2253
|
+
const pathMappings = ctx.conf.tsOptions?.pathMappings;
|
|
2254
|
+
if (!pathMappings) return false;
|
|
2255
|
+
const resolved = resolveTsPathPattern(pathMappings, specifier);
|
|
2256
|
+
if (!resolved) return false;
|
|
2257
|
+
return mods.some((mod) => resolved.includes(mod) || resolved === mod);
|
|
2258
|
+
};
|
|
2259
|
+
const lineOf = (node) => node.getSourceFile().getLineAndColumnAtPos(node.getStart()).line;
|
|
2260
|
+
function accountFile(ctx, sourceFile, filePath, paths, prefixes, declined) {
|
|
2261
|
+
const decline = (node, reason) => declined.push({
|
|
2262
|
+
filePath,
|
|
2263
|
+
line: lineOf(node),
|
|
2264
|
+
reason
|
|
2265
|
+
});
|
|
2266
|
+
/** Local names bound to the artifact: the `token` export, and any namespace of it. */
|
|
2267
|
+
const bindings = /* @__PURE__ */ new Set();
|
|
2268
|
+
for (const statement of sourceFile.getStatements()) {
|
|
2269
|
+
if (Node.isExportDeclaration(statement)) {
|
|
2270
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2271
|
+
if (specifier && isTokensEntrypoint(ctx, specifier)) decline(statement, "re-exported");
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
if (!Node.isImportDeclaration(statement)) continue;
|
|
2275
|
+
const specifier = statement.getModuleSpecifierValue();
|
|
2276
|
+
if (!specifier) continue;
|
|
2277
|
+
const clause = statement.getImportClause();
|
|
2278
|
+
if (!clause || statement.isTypeOnly()) continue;
|
|
2279
|
+
if (!isTokensEntrypoint(ctx, specifier)) {
|
|
2280
|
+
for (const named of clause.getNamedImports()) if (!named.isTypeOnly() && nameOf(named.getNameNode()) === "token") decline(named, "unclassified-import");
|
|
2281
|
+
const foreignDefault = clause.getDefaultImport();
|
|
2282
|
+
if (foreignDefault && nameOf(foreignDefault) === "token") decline(foreignDefault, "unclassified-import");
|
|
2283
|
+
const foreignNamespace = clause.getNamespaceImport();
|
|
2284
|
+
if (foreignNamespace && usesTokenMember(sourceFile, foreignNamespace.getText())) decline(foreignNamespace, "unclassified-import");
|
|
2285
|
+
continue;
|
|
2286
|
+
}
|
|
2287
|
+
const defaultImport = clause.getDefaultImport();
|
|
2288
|
+
if (defaultImport) decline(defaultImport, "unsupported-import");
|
|
2289
|
+
const namespace = clause.getNamespaceImport();
|
|
2290
|
+
if (namespace) bindings.add(namespace.getText());
|
|
2291
|
+
for (const named of clause.getNamedImports()) {
|
|
2292
|
+
if (named.isTypeOnly()) continue;
|
|
2293
|
+
if (nameOf(named.getNameNode()) !== "token") {
|
|
2294
|
+
if (usedAsValue(sourceFile, (named.getAliasNode() ?? named.getNameNode()).getText(), named)) decline(named, "unsupported-import");
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
bindings.add((named.getAliasNode() ?? named.getNameNode()).getText());
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
for (const importEquals of sourceFile.getDescendantsOfKind(SyntaxKind.ImportEqualsDeclaration)) if (importEquals.getText().includes("token")) decline(importEquals, "import-equals");
|
|
2301
|
+
for (const call of sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
2302
|
+
const callee = call.getExpression();
|
|
2303
|
+
const isRequire = Node.isIdentifier(callee) && callee.getText() === "require";
|
|
2304
|
+
const isDynamicImport = callee.getKind() === SyntaxKind.ImportKeyword;
|
|
2305
|
+
if (!isRequire && !isDynamicImport) continue;
|
|
2306
|
+
const argument = call.getArguments()[0];
|
|
2307
|
+
if (!argument) continue;
|
|
2308
|
+
if (Node.isStringLiteral(argument) ? isTokensEntrypoint(ctx, argument.getLiteralValue()) : true) decline(call, isRequire ? "require" : "dynamic-import");
|
|
2309
|
+
}
|
|
2310
|
+
for (const identifier of sourceFile.getDescendantsOfKind(SyntaxKind.Identifier)) {
|
|
2311
|
+
const text = nameOf(identifier);
|
|
2312
|
+
if (!bindings.has(text) && text !== "token") continue;
|
|
2313
|
+
const parent = identifier.getParent();
|
|
2314
|
+
if (Node.isPropertyAccessExpression(parent) && parent.getNameNode() === identifier) {
|
|
2315
|
+
const object = parent.getExpression();
|
|
2316
|
+
const objectName = Node.isIdentifier(object) ? object.getText() : void 0;
|
|
2317
|
+
if (objectName && bindings.has(objectName)) continue;
|
|
2318
|
+
decline(identifier, "unresolved-reference");
|
|
2319
|
+
continue;
|
|
2320
|
+
}
|
|
2321
|
+
if (Node.isPropertyAssignment(parent) && parent.getNameNode() === identifier) continue;
|
|
2322
|
+
const resolved = accountedPath(identifier);
|
|
2323
|
+
if (resolved === void 0) {
|
|
2324
|
+
decline(identifier, "unresolved-reference");
|
|
2325
|
+
continue;
|
|
2326
|
+
}
|
|
2327
|
+
if (resolved.kind === "binding") continue;
|
|
2328
|
+
if (resolved.kind === "prefix") {
|
|
2329
|
+
prefixes.add(resolved.value);
|
|
2330
|
+
continue;
|
|
2331
|
+
}
|
|
2332
|
+
paths.add(resolved.value);
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Whether any `<namespace>.token` member access appears, for an unclassified namespace.
|
|
2337
|
+
*
|
|
2338
|
+
* Element access counts too: `ui['token'](k)` reaches the same export and is invisible to a
|
|
2339
|
+
* property-access-only scan.
|
|
2340
|
+
*/
|
|
2341
|
+
function usesTokenMember(sourceFile, namespace) {
|
|
2342
|
+
if (sourceFile.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression).some((access) => access.getExpression().getText() === namespace && nameOf(access.getNameNode()) === "token")) return true;
|
|
2343
|
+
return sourceFile.getDescendantsOfKind(SyntaxKind.ElementAccessExpression).some((access) => {
|
|
2344
|
+
if (access.getExpression().getText() !== namespace) return false;
|
|
2345
|
+
const argument = access.getArgumentExpression();
|
|
2346
|
+
return argument != null && (!Node.isStringLiteral(argument) || argument.getLiteralValue() === "token");
|
|
2347
|
+
});
|
|
2348
|
+
}
|
|
2349
|
+
/**
|
|
2350
|
+
* An identifier's name as the compiler resolves it, not as it is spelled.
|
|
2351
|
+
*
|
|
2352
|
+
* `token` is the identifier `token`; reading `getText()` returns the escape and compares
|
|
2353
|
+
* unequal, which let an import of the artifact's export past the checks that key on the name.
|
|
2354
|
+
*/
|
|
2355
|
+
const nameOf = (node) => Node.isIdentifier(node) ? String(node.compilerNode.escapedText) : node.getText();
|
|
2356
|
+
/**
|
|
2357
|
+
* What one occurrence asks for, or `undefined` if it is not a call this can read.
|
|
2358
|
+
*
|
|
2359
|
+
* Accepts the callee position and nothing else — `token('x')`, `token.value('x')`, and the
|
|
2360
|
+
* namespaced spellings of each. An identifier anywhere else is a value
|
|
2361
|
+
* escaping somewhere this pass cannot follow: assigned (`const t = token`), passed
|
|
2362
|
+
* (`useMemo(() => token)`), spread, or enumerated.
|
|
2363
|
+
*/
|
|
2364
|
+
function accountedPath(identifier) {
|
|
2365
|
+
const parent = identifier.getParent();
|
|
2366
|
+
if (!parent) return void 0;
|
|
2367
|
+
if (Node.isImportSpecifier(parent) || Node.isNamespaceImport(parent) || Node.isImportClause(parent) || Node.isExportSpecifier(parent)) return { kind: "binding" };
|
|
2368
|
+
if (Node.isCallExpression(parent) && parent.getExpression() === identifier) return literalPath(parent);
|
|
2369
|
+
if (!Node.isPropertyAccessExpression(parent) || parent.getExpression() !== identifier) return void 0;
|
|
2370
|
+
const property = parent.getNameNode().getText();
|
|
2371
|
+
const grandParent = parent.getParent();
|
|
2372
|
+
if (property === "value") return Node.isCallExpression(grandParent) && grandParent.getExpression() === parent ? literalPath(grandParent) : void 0;
|
|
2373
|
+
if (property !== "token") return void 0;
|
|
2374
|
+
if (Node.isCallExpression(grandParent) && grandParent.getExpression() === parent) return literalPath(grandParent);
|
|
2375
|
+
if (Node.isPropertyAccessExpression(grandParent) && grandParent.getExpression() === parent) {
|
|
2376
|
+
if (grandParent.getNameNode().getText() !== "value") return void 0;
|
|
2377
|
+
const call = grandParent.getParent();
|
|
2378
|
+
return Node.isCallExpression(call) && call.getExpression() === grandParent ? literalPath(call) : void 0;
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
/**
|
|
2382
|
+
* The path a call asks for, or the prefix it is bounded by.
|
|
2383
|
+
*
|
|
2384
|
+
* Read through `getLiteralValue()`, never off the source text. The text carries escapes —
|
|
2385
|
+
* `token('colors.red.300')` — and a path recorded raw looks up nothing, which
|
|
2386
|
+
* would accept a reference and keep no declaration for it. That is the exact failure this
|
|
2387
|
+
* module exists to make unrepresentable.
|
|
2388
|
+
*
|
|
2389
|
+
* A template literal with substitutions is not a path, but it is not unbounded either: its
|
|
2390
|
+
* head is a prefix everything it can produce begins with. Declining one cost every token
|
|
2391
|
+
* declaration in the project; bounding it costs the category. The head is the *whole* answer —
|
|
2392
|
+
* `` `colors.${a}.${b}` `` bounds no more tightly than `` `colors.${x}` `` does — and an empty
|
|
2393
|
+
* head bounds nothing at all, so that still declines.
|
|
2394
|
+
*/
|
|
2395
|
+
function literalPath(call) {
|
|
2396
|
+
if (!Node.isCallExpression(call)) return void 0;
|
|
2397
|
+
const argument = unwrapAssertions(call.getArguments()[0]);
|
|
2398
|
+
if (!argument) return void 0;
|
|
2399
|
+
if (Node.isStringLiteral(argument)) return {
|
|
2400
|
+
kind: "path",
|
|
2401
|
+
value: argument.getLiteralValue()
|
|
2402
|
+
};
|
|
2403
|
+
if (Node.isNoSubstitutionTemplateLiteral(argument)) return {
|
|
2404
|
+
kind: "path",
|
|
2405
|
+
value: argument.getLiteralValue()
|
|
2406
|
+
};
|
|
2407
|
+
if (Node.isTemplateExpression(argument)) {
|
|
2408
|
+
const head = argument.getHead().getLiteralText();
|
|
2409
|
+
return head ? {
|
|
2410
|
+
kind: "prefix",
|
|
2411
|
+
value: head
|
|
2412
|
+
} : void 0;
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
/**
|
|
2416
|
+
* Whether a binding is read anywhere outside a type position.
|
|
2417
|
+
*
|
|
2418
|
+
* `import { Token, token }` brings in a type beside the value, and a type cannot produce a
|
|
2419
|
+
* `var()` — but a binding that is *called* can, whatever it is named, because a barrel
|
|
2420
|
+
* matching the specifier test could re-export `token` under it.
|
|
2421
|
+
*/
|
|
2422
|
+
function usedAsValue(sourceFile, name, declaration) {
|
|
2423
|
+
return sourceFile.getDescendantsOfKind(SyntaxKind.Identifier).some((identifier) => {
|
|
2424
|
+
if (nameOf(identifier) !== name) return false;
|
|
2425
|
+
if (identifier.getFirstAncestor((ancestor) => ancestor === declaration)) return false;
|
|
2426
|
+
return !identifier.getFirstAncestor((ancestor) => Node.isTypeNode(ancestor));
|
|
2427
|
+
});
|
|
2428
|
+
}
|
|
2429
|
+
/**
|
|
2430
|
+
* Strip the wrappers that carry no runtime meaning.
|
|
2431
|
+
*
|
|
2432
|
+
* `` token(`animations.${name}` as Token) `` is the shape a *typed* caller writes, and has to
|
|
2433
|
+
* be: the generated `Token` type is a union of template literals, so a `string`-typed
|
|
2434
|
+
* substitution does not typecheck without the assertion. Reading only the outermost node
|
|
2435
|
+
* declined exactly the call this bounding exists for — the one in this repository's own
|
|
2436
|
+
* documentation site.
|
|
2437
|
+
*
|
|
2438
|
+
* Each of these evaluates to its inner expression, so unwrapping changes nothing about what
|
|
2439
|
+
* the call receives.
|
|
2440
|
+
*/
|
|
2441
|
+
function unwrapAssertions(node) {
|
|
2442
|
+
let current = node;
|
|
2443
|
+
while (current && (Node.isAsExpression(current) || Node.isSatisfiesExpression(current) || Node.isNonNullExpression(current) || Node.isParenthesizedExpression(current) || Node.isTypeAssertion(current))) current = current.getExpression();
|
|
2444
|
+
return current;
|
|
2445
|
+
}
|
|
2446
|
+
//#endregion
|
|
2102
2447
|
//#region src/token-references.ts
|
|
2103
2448
|
/**
|
|
2104
|
-
* `token
|
|
2449
|
+
* `token('spacing.4')` and `token.value('colors.red.300')`, including the whitespace a
|
|
2105
2450
|
* formatter may leave behind. The parser reports both, resolving constants and template
|
|
2106
|
-
* literals through them — `token.
|
|
2451
|
+
* literals through them — `token.value()` included, since it is recorded as its own kind
|
|
2107
2452
|
* rather than dropped for having a property access as its callee. Scanning the text still
|
|
2108
2453
|
* earns its place twice over: it covers a path built somewhere the extractor cannot
|
|
2109
2454
|
* follow, and it covers the callers below that supply no `results` at all.
|
|
2110
2455
|
*/
|
|
2111
|
-
const TOKEN_CALL = /\btoken(?:\s*\.\s*
|
|
2456
|
+
const TOKEN_CALL = /\btoken(?:\s*\.\s*value)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
2457
|
+
/**
|
|
2458
|
+
* A token reached from javascript at all — a call of any shape, or an import of the artifact.
|
|
2459
|
+
*
|
|
2460
|
+
* Has to stay a strict superset of `TOKEN_CALL`: it must match wherever that one matches, and
|
|
2461
|
+
* also wherever that one gives up. See `collectTokenReferences` for why the redundancy holds.
|
|
2462
|
+
* No `g` flag, so `test` carries no state between files.
|
|
2463
|
+
*/
|
|
2464
|
+
const REACHABLE_FROM_JS = /\btoken(\s*\.\s*value)?\s*\(|\b(?:from|import|require)\s*\(?\s*['"][^'"]*\/tokens(\/[^'"]*|\.[cm]?[jt]sx?)?['"]/;
|
|
2112
2465
|
/**
|
|
2113
2466
|
* The text of every file `include` covers — as written on disk, and, when they differ, as the
|
|
2114
2467
|
* parser understands it.
|
|
@@ -2129,67 +2482,86 @@ const TOKEN_CALL = /\btoken(?:\s*\.\s*var)?\s*\(\s*['"`]([^'"`]+)['"`]/g;
|
|
|
2129
2482
|
* is an order of magnitude more.
|
|
2130
2483
|
*/
|
|
2131
2484
|
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
|
-
}
|
|
2485
|
+
for (const snapshot of sourceSnapshots(ctx)) yield* snapshotTexts(snapshot);
|
|
2144
2486
|
}
|
|
2145
2487
|
/**
|
|
2146
|
-
*
|
|
2147
|
-
*
|
|
2148
|
-
* This is deliberately textual and therefore over-inclusive: a match inside a comment or
|
|
2149
|
-
* a string that is never evaluated keeps a token alive. Keeping a token that is not used
|
|
2150
|
-
* costs bytes; dropping one that is breaks the page, so the bias belongs on this side.
|
|
2488
|
+
* The token prune, as all three build paths need to run it.
|
|
2151
2489
|
*
|
|
2152
|
-
*
|
|
2153
|
-
*
|
|
2154
|
-
*
|
|
2155
|
-
*
|
|
2156
|
-
*
|
|
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.
|
|
2490
|
+
* One function because it was three copies of one conditional, and the `false` branch went
|
|
2491
|
+
* missing from a copy: a watch rebuild with `pruneUnusedTokens: false` skipped `pruneTokens`
|
|
2492
|
+
* altogether, so it kept `@property` registrations that a full build of the same source
|
|
2493
|
+
* strips. The other two copies carried a comment pointing at that file for the reasoning,
|
|
2494
|
+
* which is how a missing branch reads as intentional.
|
|
2160
2495
|
*
|
|
2161
|
-
*
|
|
2162
|
-
*
|
|
2163
|
-
* for
|
|
2164
|
-
*
|
|
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.
|
|
2496
|
+
* Opting out still drops the registrations. Those are not tokens — nothing hands one to
|
|
2497
|
+
* javascript, and none appear in the `token()` surface — so the reachability problem the flag
|
|
2498
|
+
* exists for does not apply to them, and opting out of token pruning should not mean shipping
|
|
2499
|
+
* a preset's whole filter and gradient set for nothing.
|
|
2177
2500
|
*/
|
|
2178
|
-
function
|
|
2501
|
+
function pruneTokensForBuild(ctx, sheet, results) {
|
|
2502
|
+
if (!ctx.config.pruneUnusedTokens) {
|
|
2503
|
+
ctx.pruneTokens(sheet);
|
|
2504
|
+
return;
|
|
2505
|
+
}
|
|
2506
|
+
const strict = ctx.config.pruneUnusedTokens === "strict";
|
|
2179
2507
|
const paths = /* @__PURE__ */ new Set();
|
|
2180
2508
|
const vars = /* @__PURE__ */ new Set();
|
|
2509
|
+
const accounting = {
|
|
2510
|
+
paths: /* @__PURE__ */ new Set(),
|
|
2511
|
+
prefixes: /* @__PURE__ */ new Set(),
|
|
2512
|
+
declined: []
|
|
2513
|
+
};
|
|
2514
|
+
let reachable = false;
|
|
2181
2515
|
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
|
-
|
|
2516
|
+
for (const snapshot of sourceSnapshots(ctx)) {
|
|
2517
|
+
for (const text of snapshotTexts(snapshot)) {
|
|
2518
|
+
for (const match of text.matchAll(TOKEN_CALL)) paths.add(match[1]);
|
|
2519
|
+
for (const name of cssVarRefs(text)) vars.add(name);
|
|
2520
|
+
if (!reachable && REACHABLE_FROM_JS.test(text)) reachable = true;
|
|
2521
|
+
}
|
|
2522
|
+
if (strict) accountSnapshot(ctx, snapshot, accounting);
|
|
2523
|
+
}
|
|
2524
|
+
for (const name of tokenVarsFor(ctx, paths)) vars.add(name);
|
|
2525
|
+
if (!strict) {
|
|
2526
|
+
ctx.pruneTokens(sheet, vars, reachable);
|
|
2527
|
+
return;
|
|
2185
2528
|
}
|
|
2529
|
+
for (const name of tokenVarsFor(ctx, accounting.paths)) vars.add(name);
|
|
2530
|
+
if (accounting.prefixes.size && !accounting.declined.length) {
|
|
2531
|
+
const prefixes = Array.from(accounting.prefixes);
|
|
2532
|
+
const matched = [];
|
|
2533
|
+
for (const token of ctx.tokens.allTokens) if (prefixes.some((prefix) => token.name.startsWith(prefix))) matched.push(token.name);
|
|
2534
|
+
for (const name of tokenVarsFor(ctx, matched)) vars.add(name);
|
|
2535
|
+
}
|
|
2536
|
+
const failing = accounting.declined.filter(failsStrict);
|
|
2537
|
+
const reported = accounting.declined.filter((entry) => !failsStrict(entry));
|
|
2538
|
+
if (failing.length) throw new BambooError("TOKEN_REFERENCE_UNRESOLVED", `${failing.length} token reference(s) could not be resolved.\n\n${formatDeclined(ctx, failing)}\n\n\`pruneUnusedTokens: 'strict'\` asserts that every token path resolves at build time, so this is an error rather than a silent fallback. Spell the path as a string literal at the call, give a template a static prefix so it can be bounded, move the token into \`staticCss\`, or set \`pruneUnusedTokens: true\` to stop asserting.`);
|
|
2539
|
+
if (reported.length) logger.warn("tokens:strict", `${reported.length} reference(s) could not be accounted for, so every token declaration is kept.\n\n${formatDeclined(ctx, reported)}\n\nThese are shapes the build cannot follow rather than paths you can respell — a component stored post-transform, a file it could not parse, a barrel it cannot classify, a dynamic \`import()\`. Narrow \`include\`, or accept the keeps.`);
|
|
2540
|
+
ctx.pruneTokens(sheet, vars, accounting.declined.length > 0 && reachable);
|
|
2541
|
+
}
|
|
2542
|
+
/** The custom properties a set of token paths resolves to. */
|
|
2543
|
+
function tokenVarsFor(ctx, paths) {
|
|
2544
|
+
const vars = /* @__PURE__ */ new Set();
|
|
2186
2545
|
for (const path of paths) {
|
|
2546
|
+
if (!path) continue;
|
|
2187
2547
|
const ref = ctx.tokens.view.getVar(path);
|
|
2188
2548
|
if (!ref) continue;
|
|
2189
2549
|
for (const name of cssVarRefs(ref)) vars.add(name);
|
|
2190
2550
|
}
|
|
2191
2551
|
return vars;
|
|
2192
2552
|
}
|
|
2553
|
+
/** Where to look, grouped by file. The surrounding text differs by whether this throws. */
|
|
2554
|
+
function formatDeclined(ctx, declined) {
|
|
2555
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
2556
|
+
for (const entry of declined) {
|
|
2557
|
+
const list = byFile.get(entry.filePath) ?? [];
|
|
2558
|
+
list.push(entry);
|
|
2559
|
+
byFile.set(entry.filePath, list);
|
|
2560
|
+
}
|
|
2561
|
+
return Array.from(byFile.entries()).map(([filePath, entries]) => {
|
|
2562
|
+
return [` ${filePath.startsWith(ctx.config.cwd) ? filePath.slice(ctx.config.cwd.length + 1) : filePath}`, ...entries.map((entry) => ` ${entry.line}: ${entry.reason}`)].join("\n");
|
|
2563
|
+
}).join("\n");
|
|
2564
|
+
}
|
|
2193
2565
|
/**
|
|
2194
2566
|
* Collect keyframe names that reading the generated css cannot reveal.
|
|
2195
2567
|
*
|
|
@@ -2248,41 +2620,6 @@ function collectRenderedElements(ctx) {
|
|
|
2248
2620
|
for (const content of sourceTexts(ctx)) for (const match of content.matchAll(/<\s*([a-z][\w-]*)(?=[\s/>]|$)/g)) found.add(match[1].toLowerCase());
|
|
2249
2621
|
return found;
|
|
2250
2622
|
}
|
|
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
2623
|
//#endregion
|
|
2287
2624
|
//#region src/builder.ts
|
|
2288
2625
|
const fileModifiedMap = /* @__PURE__ */ new Map();
|
|
@@ -2465,8 +2802,7 @@ var Builder = class {
|
|
|
2465
2802
|
const sheet = ctx.createSheet();
|
|
2466
2803
|
if (layerParams) ctx.appendLayerParams(sheet);
|
|
2467
2804
|
ctx.appendBaselineCss(sheet);
|
|
2468
|
-
|
|
2469
|
-
else ctx.pruneTokens(sheet);
|
|
2805
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2470
2806
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2471
2807
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2472
2808
|
return ctx.getCss(sheet);
|
|
@@ -2571,8 +2907,7 @@ const cssgen = async (ctx, options) => {
|
|
|
2571
2907
|
ctx.appendBaselineCss(sheet);
|
|
2572
2908
|
}
|
|
2573
2909
|
ctx.appendParserCss(sheet);
|
|
2574
|
-
if (!minimal
|
|
2575
|
-
else if (!minimal) ctx.pruneTokens(sheet);
|
|
2910
|
+
if (!minimal) pruneTokensForBuild(ctx, sheet, results);
|
|
2576
2911
|
if (!minimal && ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2577
2912
|
if (!minimal && ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2578
2913
|
if (splitting) await ctx.writeSplitCss(sheet);
|
|
@@ -2644,8 +2979,7 @@ async function build(ctx, artifactIds) {
|
|
|
2644
2979
|
ctx.appendBaselineCss(sheet);
|
|
2645
2980
|
const parsed = ctx.parseFiles();
|
|
2646
2981
|
ctx.appendParserCss(sheet);
|
|
2647
|
-
|
|
2648
|
-
else ctx.pruneTokens(sheet);
|
|
2982
|
+
pruneTokensForBuild(ctx, sheet, parsed.results);
|
|
2649
2983
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2650
2984
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2651
2985
|
await ctx.writeCss(sheet);
|
|
@@ -2691,7 +3025,7 @@ async function generate(config, configPath) {
|
|
|
2691
3025
|
ctx.appendLayerParams(sheet);
|
|
2692
3026
|
ctx.appendBaselineCss(sheet);
|
|
2693
3027
|
ctx.appendParserCss(sheet);
|
|
2694
|
-
|
|
3028
|
+
pruneTokensForBuild(ctx, sheet, []);
|
|
2695
3029
|
if (ctx.config.prunePreflight) ctx.prunePreflight(sheet, collectRenderedElements(ctx));
|
|
2696
3030
|
if (ctx.config.pruneUnusedKeyframes) ctx.pruneKeyframes(sheet, collectKeyframeReferences(ctx, keyframeNames(ctx)));
|
|
2697
3031
|
const css = ctx.getCss(sheet);
|
|
@@ -2795,7 +3129,7 @@ export default defineConfig({
|
|
|
2795
3129
|
|
|
2796
3130
|
// Tokens, keyframes and @property rules nothing in the emitted css reaches are dropped
|
|
2797
3131
|
// 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
|
|
3132
|
+
// if you read tokens from somewhere the build cannot see them — \`token()\` with a
|
|
2799
3133
|
// computed path, or a hand-written stylesheet outside \`include\` — or list those under
|
|
2800
3134
|
// \`staticCss\` to keep them.
|
|
2801
3135
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bamboocss/node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.30.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.30.0",
|
|
56
|
+
"@bamboocss/core": "1.30.0",
|
|
57
|
+
"@bamboocss/generator": "1.30.0",
|
|
58
|
+
"@bamboocss/logger": "1.30.0",
|
|
59
|
+
"@bamboocss/parser": "1.30.0",
|
|
60
|
+
"@bamboocss/plugin-lightningcss": "1.30.0",
|
|
61
|
+
"@bamboocss/plugin-svelte": "1.30.0",
|
|
62
|
+
"@bamboocss/plugin-vue": "1.30.0",
|
|
63
|
+
"@bamboocss/reporter": "1.30.0",
|
|
64
|
+
"@bamboocss/shared": "1.30.0",
|
|
65
|
+
"@bamboocss/token-dictionary": "1.30.0",
|
|
66
|
+
"@bamboocss/types": "1.30.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/fs-extra": "11.0.4",
|