@better-css-modules/core 0.3.0 → 0.4.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/README.md +3 -2
- package/dist/{dts-De2E7b59.mjs → dts--_fgXHCu.mjs} +14 -9
- package/dist/index.d.mts +89 -1
- package/dist/index.mjs +542 -2
- package/dist/internal.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,9 +49,10 @@ try {
|
|
|
49
49
|
- `loadConfig({ cwd?, config? })` loads the config file `config` names, relative to `cwd`, or the `better-css-modules.config.*` in `cwd` (by default the working directory), and returns it as a `ResolvedConfig`: the defaults filled in, checked, and with `root`, the directory every relative path starts from, and `file`, the config file or `null`.
|
|
50
50
|
- `generate(config)` writes the `.d.ts` files and their declaration maps, removes the ones it wrote for stylesheets that are gone, and returns a `GenerateResult`: `files`, `removed` and the syntax `diagnostics`.
|
|
51
51
|
- `check(config)` runs every check and returns a `CheckResult`: the sorted `diagnostics`, how many `modules` it checked, and the token categories (`tokens`) the global CSS restricts.
|
|
52
|
+
- `exportTokens(config)` reads the global CSS and returns its tokens as a DTCG resolver document, the `DtcgResolver` the `tokens` command prints; see [Token export](https://github.com/k35o/better-css-modules#token-export).
|
|
52
53
|
- `formatDiagnostic(diagnostic, cwd)` gives the `file:line:col error rule: message` line of the CLI, and `formatGitHubAnnotation(diagnostic, cwd)` the GitHub Actions workflow command of `--format github`.
|
|
53
|
-
- `ConfigError` is a mistake in the config or in the files it names. `loadConfig` throws it, and so does `check` when `include` matches no file, the global CSS cannot be read, or it does not declare the `layer
|
|
54
|
-
- The types `Config`, `ResolvedConfig`, `GenerateResult`, `CheckResult`, `Diagnostic` and `
|
|
54
|
+
- `ConfigError` is a mistake in the config or in the files it names. `loadConfig` throws it, and so does `check` when `include` matches no file, the global CSS cannot be read, or it does not declare the `layer`, and `exportTokens` when the global CSS cannot be read or a token has a name DTCG cannot hold.
|
|
55
|
+
- The types `Config`, `ResolvedConfig`, `GenerateResult`, `CheckResult`, `Diagnostic`, `RuleId`, `DtcgResolver` and `DtcgToken` describe these.
|
|
55
56
|
|
|
56
57
|
`@better-css-modules/core/internal` holds what the CLI and the plugins share. It is for those packages only and outside semver.
|
|
57
58
|
|
|
@@ -890,10 +890,11 @@ function globalCssFrom(files) {
|
|
|
890
890
|
for (const [name, source] of registrations) if (!sources.has(name)) sources.set(name, source);
|
|
891
891
|
const values = resolveValues(new Map([...sources].map(([name, { text }]) => [name, text])));
|
|
892
892
|
const tokens = /* @__PURE__ */ new Map();
|
|
893
|
-
for (const [name, { node, file }] of sources) tokens.set(name, {
|
|
893
|
+
for (const [name, { node, file, text }] of sources) tokens.set(name, {
|
|
894
894
|
name,
|
|
895
895
|
category: categoryOf(name),
|
|
896
896
|
value: values.get(name) ?? "",
|
|
897
|
+
text,
|
|
897
898
|
file,
|
|
898
899
|
node
|
|
899
900
|
});
|
|
@@ -927,7 +928,7 @@ function layersOf(files) {
|
|
|
927
928
|
return;
|
|
928
929
|
}
|
|
929
930
|
const imported = imports.get(node);
|
|
930
|
-
if (!imported || imported.
|
|
931
|
+
if (!imported || imported.condition !== null) return;
|
|
931
932
|
const target = byFile.get(imported.file);
|
|
932
933
|
if (imported.layer === null && target) visit(target);
|
|
933
934
|
else if (imported.layer) names.add(imported.layer);
|
|
@@ -1072,18 +1073,18 @@ var Loader = class {
|
|
|
1072
1073
|
if (node.type !== "atrule" || node.name.toLowerCase() !== "import") continue;
|
|
1073
1074
|
const prelude = readImport(node.params);
|
|
1074
1075
|
if (!prelude) fail(`${this.at(file, node)}: cannot read @import ${node.params}`);
|
|
1075
|
-
const { url, layer,
|
|
1076
|
+
const { url, layer, condition } = prelude;
|
|
1076
1077
|
if (/^([a-z][a-z\d+.-]*:|\/\/)/i.test(url)) fail(`${this.at(file, node)}: global CSS cannot import ${url}`);
|
|
1077
1078
|
const imported = this.imports.sync(path.dirname(file), url).path;
|
|
1078
1079
|
if (!imported) fail(`${this.at(file, node)}: cannot resolve "${url}"`);
|
|
1079
1080
|
entry.imports.push({
|
|
1080
1081
|
rule: node,
|
|
1081
1082
|
file: imported,
|
|
1082
|
-
|
|
1083
|
+
condition,
|
|
1083
1084
|
layer
|
|
1084
1085
|
});
|
|
1085
|
-
if (
|
|
1086
|
-
await this.load(imported, conditional || condition, [...importers, file]);
|
|
1086
|
+
if (condition === null) imports.push(imported);
|
|
1087
|
+
await this.load(imported, conditional || condition !== null, [...importers, file]);
|
|
1087
1088
|
}
|
|
1088
1089
|
this.files.push(entry);
|
|
1089
1090
|
}
|
|
@@ -1122,19 +1123,23 @@ function readImport(params) {
|
|
|
1122
1123
|
const [target, ...rest] = prelude.type === "AtrulePrelude" ? prelude.children.toArray() : [];
|
|
1123
1124
|
if (target?.type !== "String" && target?.type !== "Url" || !target.loc) return null;
|
|
1124
1125
|
let layer = null;
|
|
1125
|
-
let
|
|
1126
|
+
let supports = null;
|
|
1127
|
+
let media = null;
|
|
1128
|
+
const text = ({ loc }) => loc ? params.slice(loc.start.offset, loc.end.offset) : "";
|
|
1126
1129
|
for (const node of rest) {
|
|
1127
1130
|
const name = node.type === "Function" || node.type === "Identifier" ? node.name.toLowerCase() : "";
|
|
1128
1131
|
if (name === "layer") {
|
|
1129
1132
|
const [named] = node.type === "Function" ? node.children.toArray() : [];
|
|
1130
1133
|
layer = named?.type === "Layer" ? named.name : "";
|
|
1131
|
-
} else if (node.type === "
|
|
1134
|
+
} else if (node.type === "Function" && name === "supports") supports = `@supports (${text(node).slice(9, -1).trim()})`;
|
|
1135
|
+
else if (node.type === "MediaQueryList" && !isUnknownMediaType(node)) media = `@media ${text(node)}`;
|
|
1132
1136
|
}
|
|
1137
|
+
const conditions = [supports, media].filter((condition) => condition !== null);
|
|
1133
1138
|
return {
|
|
1134
1139
|
url: target.value,
|
|
1135
1140
|
urlEnd: target.loc.end.offset,
|
|
1136
1141
|
layer,
|
|
1137
|
-
|
|
1142
|
+
condition: conditions.length > 0 ? conditions.join(" ") : null
|
|
1138
1143
|
};
|
|
1139
1144
|
}
|
|
1140
1145
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -17,4 +17,92 @@ interface CheckResult {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function check(config: ResolvedConfig): Promise<CheckResult>;
|
|
19
19
|
//#endregion
|
|
20
|
-
|
|
20
|
+
//#region src/dtcg.d.ts
|
|
21
|
+
/** A reference to another token: its name without the leading `--`, in braces. */
|
|
22
|
+
type DtcgAlias = `{${string}}`;
|
|
23
|
+
type DtcgComponent = number | "none";
|
|
24
|
+
interface DtcgColor {
|
|
25
|
+
colorSpace: "srgb" | "srgb-linear" | "hsl" | "hwb" | "lab" | "lch" | "oklab" | "oklch" | "display-p3" | "a98-rgb" | "prophoto-rgb" | "rec2020" | "xyz-d65" | "xyz-d50";
|
|
26
|
+
components: [DtcgComponent, DtcgComponent, DtcgComponent];
|
|
27
|
+
/** Present when the color is written with one. */
|
|
28
|
+
alpha?: number;
|
|
29
|
+
}
|
|
30
|
+
interface DtcgDimension {
|
|
31
|
+
value: number;
|
|
32
|
+
unit: "px" | "rem";
|
|
33
|
+
}
|
|
34
|
+
interface DtcgDuration {
|
|
35
|
+
value: number;
|
|
36
|
+
unit: "ms" | "s";
|
|
37
|
+
}
|
|
38
|
+
interface DtcgShadow {
|
|
39
|
+
color: DtcgColor | DtcgAlias;
|
|
40
|
+
offsetX: DtcgDimension;
|
|
41
|
+
offsetY: DtcgDimension;
|
|
42
|
+
blur: DtcgDimension;
|
|
43
|
+
spread: DtcgDimension;
|
|
44
|
+
inset?: true;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A token. A value DTCG has no form for keeps its CSS text and has no type,
|
|
48
|
+
* which DTCG takes for an invalid token; so does an alias of one.
|
|
49
|
+
*/
|
|
50
|
+
type DtcgToken = {
|
|
51
|
+
$type: "color";
|
|
52
|
+
$value: DtcgColor | DtcgAlias;
|
|
53
|
+
} | {
|
|
54
|
+
$type: "dimension";
|
|
55
|
+
$value: DtcgDimension | DtcgAlias;
|
|
56
|
+
} | {
|
|
57
|
+
$type: "duration";
|
|
58
|
+
$value: DtcgDuration | DtcgAlias;
|
|
59
|
+
} | {
|
|
60
|
+
$type: "number";
|
|
61
|
+
$value: number | DtcgAlias;
|
|
62
|
+
} | {
|
|
63
|
+
$type: "fontWeight";
|
|
64
|
+
$value: number | "normal" | "bold" | DtcgAlias;
|
|
65
|
+
} | {
|
|
66
|
+
$type: "fontFamily";
|
|
67
|
+
$value: string | string[];
|
|
68
|
+
} | {
|
|
69
|
+
$type: "cubicBezier";
|
|
70
|
+
$value: [number, number, number, number] | DtcgAlias;
|
|
71
|
+
} | {
|
|
72
|
+
$type: "shadow";
|
|
73
|
+
$value: DtcgShadow | DtcgShadow[] | DtcgAlias;
|
|
74
|
+
} | {
|
|
75
|
+
$type?: undefined;
|
|
76
|
+
$value: string;
|
|
77
|
+
};
|
|
78
|
+
/** Tokens by their names without the leading `--`. */
|
|
79
|
+
type DtcgTokens = Record<string, DtcgToken>;
|
|
80
|
+
interface DtcgSet {
|
|
81
|
+
type: "set";
|
|
82
|
+
name: ":root";
|
|
83
|
+
sources: [DtcgTokens];
|
|
84
|
+
}
|
|
85
|
+
/** A place other than `:root` that overrides tokens, named by its conditions as written. */
|
|
86
|
+
interface DtcgModifier {
|
|
87
|
+
type: "modifier";
|
|
88
|
+
name: string;
|
|
89
|
+
contexts: {
|
|
90
|
+
off: [];
|
|
91
|
+
on: [DtcgTokens];
|
|
92
|
+
};
|
|
93
|
+
default: "off";
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The tokens as a DTCG resolver document (Resolver Module 2025.10): the tokens
|
|
97
|
+
* at `:root` as a set, then each mode as a modifier the input turns on, in the
|
|
98
|
+
* source order of its first place. The input turns on every mode whose
|
|
99
|
+
* conditions hold at once.
|
|
100
|
+
*/
|
|
101
|
+
interface DtcgResolver {
|
|
102
|
+
version: "2025.10";
|
|
103
|
+
resolutionOrder: [DtcgSet, ...DtcgModifier[]];
|
|
104
|
+
}
|
|
105
|
+
/** Read the global CSS a config lists and write its tokens as a DTCG resolver document. */
|
|
106
|
+
export declare function exportTokens(config: ResolvedConfig): Promise<DtcgResolver>;
|
|
107
|
+
//#endregion
|
|
108
|
+
export { type CheckResult, type Config, ConfigError, type Diagnostic, type DtcgResolver, type DtcgToken, type GenerateResult, type ResolvedConfig, type RuleId, defineConfig, formatDiagnostic, formatGitHubAnnotation, generate, loadConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as find, D as property, E as parse, O as walk$1, S as valueStart, T as lexer, _ as sortDiagnostics, a as resolveLayer, b as loadConfig, c as loadGlobalCss, d as findCssModules, f as loadCssModules, g as formatGitHubAnnotation, h as formatDiagnostic, m as tokenCategories, o as declaresToken, p as categoryOf, r as checkLayer, s as isForeign, t as generate, u as defaultIgnore, v as ConfigError, w as generate$1, x as paramsStart, y as defineConfig } from "./dts
|
|
1
|
+
import { C as find, D as property, E as parse, O as walk$1, S as valueStart, T as lexer, _ as sortDiagnostics, a as resolveLayer, b as loadConfig, c as loadGlobalCss, d as findCssModules, f as loadCssModules, g as formatGitHubAnnotation, h as formatDiagnostic, m as tokenCategories, o as declaresToken, p as categoryOf, r as checkLayer, s as isForeign, t as generate, u as defaultIgnore, v as ConfigError, w as generate$1, x as paramsStart, y as defineConfig } from "./dts--_fgXHCu.mjs";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { ResolverFactory } from "oxc-resolver";
|
|
@@ -2696,4 +2696,544 @@ function isZero(node) {
|
|
|
2696
2696
|
return Number(node.value) === 0;
|
|
2697
2697
|
}
|
|
2698
2698
|
//#endregion
|
|
2699
|
-
|
|
2699
|
+
//#region src/dtcg.ts
|
|
2700
|
+
/** Read the global CSS a config lists and write its tokens as a DTCG resolver document. */
|
|
2701
|
+
async function exportTokens(config) {
|
|
2702
|
+
return dtcgFrom(await loadGlobalCss(config));
|
|
2703
|
+
}
|
|
2704
|
+
/** Write the tokens of global CSS already read as a DTCG resolver document. Pure. */
|
|
2705
|
+
function dtcgFrom(globalCss) {
|
|
2706
|
+
const { tokens } = globalCss;
|
|
2707
|
+
for (const token of tokens.values()) checkName(token);
|
|
2708
|
+
const writer = new Writer(tokens);
|
|
2709
|
+
const write = (values) => Object.fromEntries([...values].map(([name, text]) => [name.slice(2), writer.token(text, tokens.get(name)?.category ?? null)]));
|
|
2710
|
+
const modifiers = [...modesOf(globalCss)].map(([name, values]) => ({
|
|
2711
|
+
type: "modifier",
|
|
2712
|
+
name,
|
|
2713
|
+
contexts: {
|
|
2714
|
+
off: [],
|
|
2715
|
+
on: [write(values)]
|
|
2716
|
+
},
|
|
2717
|
+
default: "off"
|
|
2718
|
+
}));
|
|
2719
|
+
return {
|
|
2720
|
+
version: "2025.10",
|
|
2721
|
+
resolutionOrder: [{
|
|
2722
|
+
type: "set",
|
|
2723
|
+
name: ":root",
|
|
2724
|
+
sources: [write([...tokens.values()].map(({ name, text }) => [name, text]))]
|
|
2725
|
+
}, ...modifiers]
|
|
2726
|
+
};
|
|
2727
|
+
}
|
|
2728
|
+
/** Refuse a name DTCG cannot hold: it forbids `.`, `{` and `}` in a name and `$` at its start. */
|
|
2729
|
+
function checkName({ name, file, node }) {
|
|
2730
|
+
if (!/[.{}]/.test(name) && !name.startsWith("--$")) return;
|
|
2731
|
+
const start = node.source?.start;
|
|
2732
|
+
throw new ConfigError(`${path.relative(process.cwd(), file)}:${start?.line ?? 1}:${start?.column ?? 1}: ${name} cannot be a DTCG token name: DTCG forbids ".", "{" and "}" in names, and "$" at their start`);
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
* The modes by name, each with the value it gives each token it overrides, in
|
|
2736
|
+
* cascade order: what the global CSS declares outside `:root` for a name
|
|
2737
|
+
* `:root` declares. A place that overrides no token is no mode.
|
|
2738
|
+
*/
|
|
2739
|
+
function modesOf({ files, tokens }) {
|
|
2740
|
+
const conditions = conditionsOf(files);
|
|
2741
|
+
const modes = /* @__PURE__ */ new Map();
|
|
2742
|
+
for (const sheet of files) sheet.root.walkDecls((decl) => {
|
|
2743
|
+
if (!tokens.has(decl.prop) || isForeign(decl) || declaresToken(decl, sheet.conditional)) return;
|
|
2744
|
+
const name = placeOf(decl, conditions.get(sheet.file) ?? "");
|
|
2745
|
+
modes.set(name, (modes.get(name) ?? /* @__PURE__ */ new Map()).set(decl.prop, decl.value));
|
|
2746
|
+
});
|
|
2747
|
+
return modes;
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* The conditions of the imports each stylesheet read only under conditions
|
|
2751
|
+
* is first reached through, outermost first.
|
|
2752
|
+
*/
|
|
2753
|
+
function conditionsOf(files) {
|
|
2754
|
+
const byFile = new Map(files.map((sheet) => [sheet.file, sheet]));
|
|
2755
|
+
const conditions = /* @__PURE__ */ new Map();
|
|
2756
|
+
const reached = /* @__PURE__ */ new Set();
|
|
2757
|
+
const visit = (sheet, outer) => {
|
|
2758
|
+
for (const { file, condition } of sheet.imports) {
|
|
2759
|
+
const target = byFile.get(file);
|
|
2760
|
+
if (!target || reached.has(file)) continue;
|
|
2761
|
+
reached.add(file);
|
|
2762
|
+
const inner = target.conditional ? joined([outer, condition ?? ""]) : "";
|
|
2763
|
+
if (target.conditional) conditions.set(file, inner);
|
|
2764
|
+
visit(target, inner);
|
|
2765
|
+
}
|
|
2766
|
+
};
|
|
2767
|
+
for (const sheet of files) if (sheet.listed) visit(sheet, "");
|
|
2768
|
+
return conditions;
|
|
2769
|
+
}
|
|
2770
|
+
/** A place by its conditions: the rules and at-rules around it, outermost first, without `@layer`. */
|
|
2771
|
+
function placeOf(node, conditions) {
|
|
2772
|
+
const parts = [];
|
|
2773
|
+
for (let parent = node.parent; parent && parent.type !== "root"; parent = parent.parent) if (parent.type === "rule") parts.unshift(parent.selector);
|
|
2774
|
+
else if (parent.type === "atrule" && parent.name.toLowerCase() !== "layer") {
|
|
2775
|
+
const { name, params } = parent;
|
|
2776
|
+
parts.unshift(`@${name} ${params}`);
|
|
2777
|
+
}
|
|
2778
|
+
return joined([conditions, ...parts]);
|
|
2779
|
+
}
|
|
2780
|
+
function joined(parts) {
|
|
2781
|
+
return parts.join(" ").replace(/\s+/g, " ").trim();
|
|
2782
|
+
}
|
|
2783
|
+
/** Writes the values of the custom properties of the global CSS as tokens. */
|
|
2784
|
+
var Writer = class {
|
|
2785
|
+
tokens;
|
|
2786
|
+
references = /* @__PURE__ */ new Map();
|
|
2787
|
+
types = /* @__PURE__ */ new Map();
|
|
2788
|
+
constructor(tokens) {
|
|
2789
|
+
this.tokens = tokens;
|
|
2790
|
+
}
|
|
2791
|
+
token(text, category) {
|
|
2792
|
+
const nodes = valueNodes(text);
|
|
2793
|
+
if (nodes) {
|
|
2794
|
+
const alias = this.alias(nodes);
|
|
2795
|
+
if (alias) return alias.$type ? alias : { $value: alias.$value };
|
|
2796
|
+
const token = (category ? READERS[category] : readInternal)(nodes, this);
|
|
2797
|
+
if (token) return token;
|
|
2798
|
+
}
|
|
2799
|
+
return { $value: text.trim() };
|
|
2800
|
+
}
|
|
2801
|
+
/** A color, or an alias of a color token. */
|
|
2802
|
+
color(node) {
|
|
2803
|
+
const written = color(node);
|
|
2804
|
+
if (written) return written;
|
|
2805
|
+
const alias = this.alias([node]);
|
|
2806
|
+
return alias?.$type === "color" ? alias.$value : null;
|
|
2807
|
+
}
|
|
2808
|
+
/**
|
|
2809
|
+
* The alias a value that is one var() of a token is, with the type of the
|
|
2810
|
+
* token its chain of aliases ends at; null when the chain loops.
|
|
2811
|
+
*/
|
|
2812
|
+
alias(nodes) {
|
|
2813
|
+
const target = this.reference(nodes);
|
|
2814
|
+
const end = target === null ? null : this.end(target);
|
|
2815
|
+
if (target === null || end === null) return null;
|
|
2816
|
+
return {
|
|
2817
|
+
$type: this.typeOf(end),
|
|
2818
|
+
$value: `{${target.slice(2)}}`
|
|
2819
|
+
};
|
|
2820
|
+
}
|
|
2821
|
+
/** The token a value that is one var() of a token names, whatever its fallback. */
|
|
2822
|
+
reference(nodes) {
|
|
2823
|
+
const [node] = nodes;
|
|
2824
|
+
if (nodes.length !== 1 || node.type !== "Function" || node.name.toLowerCase() !== "var") return null;
|
|
2825
|
+
const [name] = node.children.toArray();
|
|
2826
|
+
return name?.type === "Identifier" && this.tokens.has(name.name) ? name.name : null;
|
|
2827
|
+
}
|
|
2828
|
+
/** The token a chain of aliases from a token ends at, or null when it loops. */
|
|
2829
|
+
end(name) {
|
|
2830
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2831
|
+
let current = name;
|
|
2832
|
+
for (let next = this.referenceOf(current); next !== null; next = this.referenceOf(current)) {
|
|
2833
|
+
seen.add(current);
|
|
2834
|
+
if (seen.has(next)) return null;
|
|
2835
|
+
current = next;
|
|
2836
|
+
}
|
|
2837
|
+
return current;
|
|
2838
|
+
}
|
|
2839
|
+
/** The token the value a token is declared with is one var() of, or null. */
|
|
2840
|
+
referenceOf(name) {
|
|
2841
|
+
if (!this.references.has(name)) {
|
|
2842
|
+
const nodes = valueNodes(this.tokens.get(name)?.text ?? "");
|
|
2843
|
+
this.references.set(name, nodes && this.reference(nodes));
|
|
2844
|
+
}
|
|
2845
|
+
return this.references.get(name) ?? null;
|
|
2846
|
+
}
|
|
2847
|
+
typeOf(name) {
|
|
2848
|
+
const token = this.tokens.get(name);
|
|
2849
|
+
if (!this.types.has(name) && token) {
|
|
2850
|
+
this.types.set(name, void 0);
|
|
2851
|
+
this.types.set(name, this.token(token.text, token.category).$type);
|
|
2852
|
+
}
|
|
2853
|
+
return this.types.get(name);
|
|
2854
|
+
}
|
|
2855
|
+
};
|
|
2856
|
+
function valueNodes(text) {
|
|
2857
|
+
let value;
|
|
2858
|
+
try {
|
|
2859
|
+
value = parse(text, { context: "value" });
|
|
2860
|
+
} catch {
|
|
2861
|
+
return null;
|
|
2862
|
+
}
|
|
2863
|
+
return value.type === "Value" ? value.children.toArray() : null;
|
|
2864
|
+
}
|
|
2865
|
+
/** The token a value written in the form of `type` is, or null. */
|
|
2866
|
+
function typed(type, value) {
|
|
2867
|
+
return value === null ? null : {
|
|
2868
|
+
$type: type,
|
|
2869
|
+
$value: value
|
|
2870
|
+
};
|
|
2871
|
+
}
|
|
2872
|
+
function one(nodes, read) {
|
|
2873
|
+
return nodes.length === 1 ? read(nodes[0]) : null;
|
|
2874
|
+
}
|
|
2875
|
+
function dimensionToken(nodes) {
|
|
2876
|
+
return typed("dimension", one(nodes, dimension));
|
|
2877
|
+
}
|
|
2878
|
+
/** How each category's value is read as a token; null when DTCG has no form for it. */
|
|
2879
|
+
const READERS = {
|
|
2880
|
+
color: (nodes) => typed("color", one(nodes, color)),
|
|
2881
|
+
spacing: dimensionToken,
|
|
2882
|
+
radius: dimensionToken,
|
|
2883
|
+
shadow: (nodes, writer) => typed("shadow", shadow(nodes, writer)),
|
|
2884
|
+
"font-family": (nodes) => typed("fontFamily", fontFamily(nodes)),
|
|
2885
|
+
"font-size": dimensionToken,
|
|
2886
|
+
"font-weight": (nodes) => typed("fontWeight", one(nodes, fontWeight)),
|
|
2887
|
+
"line-height": (nodes) => typed("number", one(nodes, number)) ?? dimensionToken(nodes),
|
|
2888
|
+
"letter-spacing": dimensionToken,
|
|
2889
|
+
"z-index": (nodes) => typed("number", one(nodes, integer)),
|
|
2890
|
+
duration: (nodes) => typed("duration", one(nodes, duration)),
|
|
2891
|
+
easing: (nodes) => typed("cubicBezier", one(nodes, easing)),
|
|
2892
|
+
breakpoint: dimensionToken
|
|
2893
|
+
};
|
|
2894
|
+
/** A name without a category is typed by how its value is written. */
|
|
2895
|
+
const readInternal = (nodes) => typed("color", one(nodes, color)) ?? typed("dimension", one(nodes, (node) => node.type === "Dimension" ? dimension(node) : null)) ?? typed("duration", one(nodes, duration)) ?? typed("number", one(nodes, number)) ?? typed("cubicBezier", one(nodes, cubicBezier));
|
|
2896
|
+
function number(node) {
|
|
2897
|
+
return node.type === "Number" ? Number(node.value) : null;
|
|
2898
|
+
}
|
|
2899
|
+
function integer(node) {
|
|
2900
|
+
const value = number(node);
|
|
2901
|
+
return value !== null && Number.isInteger(value) ? value : null;
|
|
2902
|
+
}
|
|
2903
|
+
/** A length in `px` or `rem`, or zero, which is a length in any unit. */
|
|
2904
|
+
function dimension(node) {
|
|
2905
|
+
if (node.type === "Number") return Number(node.value) === 0 ? {
|
|
2906
|
+
value: 0,
|
|
2907
|
+
unit: "px"
|
|
2908
|
+
} : null;
|
|
2909
|
+
if (node.type !== "Dimension") return null;
|
|
2910
|
+
const unit = node.unit.toLowerCase();
|
|
2911
|
+
const value = Number(node.value);
|
|
2912
|
+
if (unit === "px" || unit === "rem") return {
|
|
2913
|
+
value,
|
|
2914
|
+
unit
|
|
2915
|
+
};
|
|
2916
|
+
return value === 0 && lexer.matchType("length", node).matched !== null ? {
|
|
2917
|
+
value: 0,
|
|
2918
|
+
unit: "px"
|
|
2919
|
+
} : null;
|
|
2920
|
+
}
|
|
2921
|
+
function duration(node) {
|
|
2922
|
+
if (node.type !== "Dimension") return null;
|
|
2923
|
+
const unit = node.unit.toLowerCase();
|
|
2924
|
+
return unit === "ms" || unit === "s" ? {
|
|
2925
|
+
value: Number(node.value),
|
|
2926
|
+
unit
|
|
2927
|
+
} : null;
|
|
2928
|
+
}
|
|
2929
|
+
function fontWeight(node) {
|
|
2930
|
+
if (node.type === "Identifier") {
|
|
2931
|
+
const keyword = node.name.toLowerCase();
|
|
2932
|
+
return keyword === "normal" || keyword === "bold" ? keyword : null;
|
|
2933
|
+
}
|
|
2934
|
+
const value = number(node);
|
|
2935
|
+
return value !== null && value >= 1 && value <= 1e3 ? value : null;
|
|
2936
|
+
}
|
|
2937
|
+
/** The families, each a string or the identifiers of an unquoted name. */
|
|
2938
|
+
function fontFamily(nodes) {
|
|
2939
|
+
const families = [];
|
|
2940
|
+
for (const family of items(nodes)) {
|
|
2941
|
+
const [first] = family;
|
|
2942
|
+
if (family.length === 1 && first.type === "String") families.push(first.value);
|
|
2943
|
+
else if (family.length > 0 && family.every((node) => node.type === "Identifier")) families.push(family.map((node) => node.name).join(" "));
|
|
2944
|
+
else return null;
|
|
2945
|
+
}
|
|
2946
|
+
if (families.length === 1 && CSS_WIDE_KEYWORDS.has(families[0].toLowerCase())) return null;
|
|
2947
|
+
return families.length === 1 ? families[0] : families;
|
|
2948
|
+
}
|
|
2949
|
+
const EASINGS = /* @__PURE__ */ new Map([
|
|
2950
|
+
["linear", [
|
|
2951
|
+
0,
|
|
2952
|
+
0,
|
|
2953
|
+
1,
|
|
2954
|
+
1
|
|
2955
|
+
]],
|
|
2956
|
+
["ease", [
|
|
2957
|
+
.25,
|
|
2958
|
+
.1,
|
|
2959
|
+
.25,
|
|
2960
|
+
1
|
|
2961
|
+
]],
|
|
2962
|
+
["ease-in", [
|
|
2963
|
+
.42,
|
|
2964
|
+
0,
|
|
2965
|
+
1,
|
|
2966
|
+
1
|
|
2967
|
+
]],
|
|
2968
|
+
["ease-out", [
|
|
2969
|
+
0,
|
|
2970
|
+
0,
|
|
2971
|
+
.58,
|
|
2972
|
+
1
|
|
2973
|
+
]],
|
|
2974
|
+
["ease-in-out", [
|
|
2975
|
+
.42,
|
|
2976
|
+
0,
|
|
2977
|
+
.58,
|
|
2978
|
+
1
|
|
2979
|
+
]]
|
|
2980
|
+
]);
|
|
2981
|
+
function easing(node) {
|
|
2982
|
+
return node.type === "Identifier" ? EASINGS.get(node.name.toLowerCase()) ?? null : cubicBezier(node);
|
|
2983
|
+
}
|
|
2984
|
+
function cubicBezier(node) {
|
|
2985
|
+
if (node.type !== "Function" || node.name.toLowerCase() !== "cubic-bezier") return null;
|
|
2986
|
+
const points = items(node.children.toArray()).map((item) => item.length === 1 ? number(item[0]) : null);
|
|
2987
|
+
if (points.length !== 4 || points.includes(null)) return null;
|
|
2988
|
+
const [x1, y1, x2, y2] = points;
|
|
2989
|
+
return [x1, x2].every((x) => x >= 0 && x <= 1) ? [
|
|
2990
|
+
x1,
|
|
2991
|
+
y1,
|
|
2992
|
+
x2,
|
|
2993
|
+
y2
|
|
2994
|
+
] : null;
|
|
2995
|
+
}
|
|
2996
|
+
function shadow(nodes, writer) {
|
|
2997
|
+
const shadows = [];
|
|
2998
|
+
for (const layer of items(nodes)) {
|
|
2999
|
+
const read = shadowOf(layer, writer);
|
|
3000
|
+
if (!read) return null;
|
|
3001
|
+
shadows.push(read);
|
|
3002
|
+
}
|
|
3003
|
+
return shadows.length === 1 ? shadows[0] : shadows;
|
|
3004
|
+
}
|
|
3005
|
+
/** One shadow: two to four lengths together, a color, and `inset`, in any order. */
|
|
3006
|
+
function shadowOf(layer, writer) {
|
|
3007
|
+
const read = layer.map((node) => node.type === "Number" || node.type === "Dimension" ? dimension(node) : null);
|
|
3008
|
+
const at = read.flatMap((length, index) => length ? [index] : []);
|
|
3009
|
+
const [first, last] = [at[0] ?? -1, at.at(-1) ?? -1];
|
|
3010
|
+
const lengths = read.slice(first, last + 1);
|
|
3011
|
+
if (first === -1 || lengths.length < 2 || lengths.length > 4 || lengths.includes(null)) return null;
|
|
3012
|
+
let inset = false;
|
|
3013
|
+
let color = null;
|
|
3014
|
+
for (const node of [...layer.slice(0, first), ...layer.slice(last + 1)]) if (!inset && node.type === "Identifier" && node.name.toLowerCase() === "inset") inset = true;
|
|
3015
|
+
else if (color === null) {
|
|
3016
|
+
color = writer.color(node);
|
|
3017
|
+
if (color === null) return null;
|
|
3018
|
+
} else return null;
|
|
3019
|
+
if (color === null) return null;
|
|
3020
|
+
const zero = {
|
|
3021
|
+
value: 0,
|
|
3022
|
+
unit: "px"
|
|
3023
|
+
};
|
|
3024
|
+
const [offsetX, offsetY, blur = zero, spread = zero] = lengths;
|
|
3025
|
+
return {
|
|
3026
|
+
color,
|
|
3027
|
+
offsetX,
|
|
3028
|
+
offsetY,
|
|
3029
|
+
blur,
|
|
3030
|
+
spread,
|
|
3031
|
+
...inset && { inset: true }
|
|
3032
|
+
};
|
|
3033
|
+
}
|
|
3034
|
+
/** A color written with its channels, or `transparent`. */
|
|
3035
|
+
function color(node) {
|
|
3036
|
+
if (node.type === "Hash") return hex(node.value);
|
|
3037
|
+
if (node.type === "Identifier") return node.name.toLowerCase() === "transparent" ? {
|
|
3038
|
+
colorSpace: "srgb",
|
|
3039
|
+
components: [
|
|
3040
|
+
0,
|
|
3041
|
+
0,
|
|
3042
|
+
0
|
|
3043
|
+
],
|
|
3044
|
+
alpha: 0
|
|
3045
|
+
} : null;
|
|
3046
|
+
if (node.type !== "Function") return null;
|
|
3047
|
+
let args = node.children.toArray();
|
|
3048
|
+
let space = FUNCTIONS.get(node.name.toLowerCase());
|
|
3049
|
+
if (node.name.toLowerCase() === "color") {
|
|
3050
|
+
const [named, ...rest] = args;
|
|
3051
|
+
space = named?.type === "Identifier" ? PREDEFINED.get(named.name.toLowerCase()) : void 0;
|
|
3052
|
+
args = rest;
|
|
3053
|
+
}
|
|
3054
|
+
const written = space && channelsOf(args);
|
|
3055
|
+
if (!space || !written) return null;
|
|
3056
|
+
const components = written.channels.map((value, index) => space.channels[index](value));
|
|
3057
|
+
if (components.includes(null)) return null;
|
|
3058
|
+
const result = {
|
|
3059
|
+
colorSpace: space.colorSpace,
|
|
3060
|
+
components
|
|
3061
|
+
};
|
|
3062
|
+
if (!written.alpha) return result;
|
|
3063
|
+
const alpha = alphaOf(written.alpha);
|
|
3064
|
+
return alpha === null ? null : {
|
|
3065
|
+
...result,
|
|
3066
|
+
alpha
|
|
3067
|
+
};
|
|
3068
|
+
}
|
|
3069
|
+
/** The channels and the alpha of a color function, which the legacy syntax separates with commas. */
|
|
3070
|
+
function channelsOf(args) {
|
|
3071
|
+
let channels;
|
|
3072
|
+
let alpha;
|
|
3073
|
+
if (args.some((arg) => arg.type === "Operator" && arg.value === ",")) {
|
|
3074
|
+
const values = items(args);
|
|
3075
|
+
if (values.some((value) => value.length !== 1)) return null;
|
|
3076
|
+
[channels, alpha] = [values.slice(0, 3).flat(), values.slice(3).flat()];
|
|
3077
|
+
} else {
|
|
3078
|
+
const slash = args.findIndex((arg) => arg.type === "Operator" && arg.value === "/");
|
|
3079
|
+
[channels, alpha] = slash === -1 ? [args, []] : [args.slice(0, slash), args.slice(slash + 1)];
|
|
3080
|
+
if (slash !== -1 && alpha.length === 0) return null;
|
|
3081
|
+
}
|
|
3082
|
+
return channels.length === 3 && alpha.length <= 1 ? {
|
|
3083
|
+
channels,
|
|
3084
|
+
alpha: alpha[0]
|
|
3085
|
+
} : null;
|
|
3086
|
+
}
|
|
3087
|
+
function scaled(value) {
|
|
3088
|
+
return Number(value.toPrecision(15));
|
|
3089
|
+
}
|
|
3090
|
+
/** A channel that is a number, or a percentage of `reference`. */
|
|
3091
|
+
function percent(reference) {
|
|
3092
|
+
return (node) => {
|
|
3093
|
+
if (node.type === "Number") return Number(node.value);
|
|
3094
|
+
if (node.type === "Percentage") return scaled(Number(node.value) * reference / 100);
|
|
3095
|
+
return none(node);
|
|
3096
|
+
};
|
|
3097
|
+
}
|
|
3098
|
+
/** A red, green or blue channel of rgb(): a number of 255, or a percentage. */
|
|
3099
|
+
const rgb = (node) => node.type === "Number" ? scaled(Number(node.value) / 255) : percent(1)(node);
|
|
3100
|
+
const DEGREES = /* @__PURE__ */ new Map([
|
|
3101
|
+
["deg", 1],
|
|
3102
|
+
["grad", 360 / 400],
|
|
3103
|
+
["rad", 180 / Math.PI],
|
|
3104
|
+
["turn", 360]
|
|
3105
|
+
]);
|
|
3106
|
+
/** A hue in degrees, from 0 up to 360. */
|
|
3107
|
+
const hue = (node) => {
|
|
3108
|
+
if (node.type !== "Number" && node.type !== "Dimension") return none(node);
|
|
3109
|
+
const factor = node.type === "Number" ? 1 : DEGREES.get(node.unit.toLowerCase());
|
|
3110
|
+
if (factor === void 0) return null;
|
|
3111
|
+
const degrees = scaled(Number(node.value) * factor);
|
|
3112
|
+
return degrees >= 0 && degrees < 360 ? degrees : scaled((degrees % 360 + 360) % 360);
|
|
3113
|
+
};
|
|
3114
|
+
function none(node) {
|
|
3115
|
+
return node.type === "Identifier" && node.name.toLowerCase() === "none" ? "none" : null;
|
|
3116
|
+
}
|
|
3117
|
+
function alphaOf(node) {
|
|
3118
|
+
if (node.type === "Number") return Number(node.value);
|
|
3119
|
+
if (node.type === "Percentage") return scaled(Number(node.value) / 100);
|
|
3120
|
+
return null;
|
|
3121
|
+
}
|
|
3122
|
+
const FUNCTIONS = /* @__PURE__ */ new Map([
|
|
3123
|
+
["rgb", {
|
|
3124
|
+
colorSpace: "srgb",
|
|
3125
|
+
channels: [
|
|
3126
|
+
rgb,
|
|
3127
|
+
rgb,
|
|
3128
|
+
rgb
|
|
3129
|
+
]
|
|
3130
|
+
}],
|
|
3131
|
+
["rgba", {
|
|
3132
|
+
colorSpace: "srgb",
|
|
3133
|
+
channels: [
|
|
3134
|
+
rgb,
|
|
3135
|
+
rgb,
|
|
3136
|
+
rgb
|
|
3137
|
+
]
|
|
3138
|
+
}],
|
|
3139
|
+
["hsl", {
|
|
3140
|
+
colorSpace: "hsl",
|
|
3141
|
+
channels: [
|
|
3142
|
+
hue,
|
|
3143
|
+
percent(100),
|
|
3144
|
+
percent(100)
|
|
3145
|
+
]
|
|
3146
|
+
}],
|
|
3147
|
+
["hsla", {
|
|
3148
|
+
colorSpace: "hsl",
|
|
3149
|
+
channels: [
|
|
3150
|
+
hue,
|
|
3151
|
+
percent(100),
|
|
3152
|
+
percent(100)
|
|
3153
|
+
]
|
|
3154
|
+
}],
|
|
3155
|
+
["hwb", {
|
|
3156
|
+
colorSpace: "hwb",
|
|
3157
|
+
channels: [
|
|
3158
|
+
hue,
|
|
3159
|
+
percent(100),
|
|
3160
|
+
percent(100)
|
|
3161
|
+
]
|
|
3162
|
+
}],
|
|
3163
|
+
["lab", {
|
|
3164
|
+
colorSpace: "lab",
|
|
3165
|
+
channels: [
|
|
3166
|
+
percent(100),
|
|
3167
|
+
percent(125),
|
|
3168
|
+
percent(125)
|
|
3169
|
+
]
|
|
3170
|
+
}],
|
|
3171
|
+
["lch", {
|
|
3172
|
+
colorSpace: "lch",
|
|
3173
|
+
channels: [
|
|
3174
|
+
percent(100),
|
|
3175
|
+
percent(150),
|
|
3176
|
+
hue
|
|
3177
|
+
]
|
|
3178
|
+
}],
|
|
3179
|
+
["oklab", {
|
|
3180
|
+
colorSpace: "oklab",
|
|
3181
|
+
channels: [
|
|
3182
|
+
percent(1),
|
|
3183
|
+
percent(.4),
|
|
3184
|
+
percent(.4)
|
|
3185
|
+
]
|
|
3186
|
+
}],
|
|
3187
|
+
["oklch", {
|
|
3188
|
+
colorSpace: "oklch",
|
|
3189
|
+
channels: [
|
|
3190
|
+
percent(1),
|
|
3191
|
+
percent(.4),
|
|
3192
|
+
hue
|
|
3193
|
+
]
|
|
3194
|
+
}]
|
|
3195
|
+
]);
|
|
3196
|
+
/** The spaces of color(), whose channels are all from 0 to 1. */
|
|
3197
|
+
const PREDEFINED = new Map([
|
|
3198
|
+
["srgb", "srgb"],
|
|
3199
|
+
["srgb-linear", "srgb-linear"],
|
|
3200
|
+
["display-p3", "display-p3"],
|
|
3201
|
+
["a98-rgb", "a98-rgb"],
|
|
3202
|
+
["prophoto-rgb", "prophoto-rgb"],
|
|
3203
|
+
["rec2020", "rec2020"],
|
|
3204
|
+
["xyz", "xyz-d65"],
|
|
3205
|
+
["xyz-d65", "xyz-d65"],
|
|
3206
|
+
["xyz-d50", "xyz-d50"]
|
|
3207
|
+
].map(([name, colorSpace]) => [name, {
|
|
3208
|
+
colorSpace,
|
|
3209
|
+
channels: [
|
|
3210
|
+
percent(1),
|
|
3211
|
+
percent(1),
|
|
3212
|
+
percent(1)
|
|
3213
|
+
]
|
|
3214
|
+
}]));
|
|
3215
|
+
function hex(digits) {
|
|
3216
|
+
if (!/^([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i.test(digits)) return null;
|
|
3217
|
+
const [r, g, b, alpha] = (digits.length <= 4 ? [...digits].map((digit) => digit + digit) : digits.match(/../g)).map((pair) => scaled(parseInt(pair, 16) / 255));
|
|
3218
|
+
const result = {
|
|
3219
|
+
colorSpace: "srgb",
|
|
3220
|
+
components: [
|
|
3221
|
+
r,
|
|
3222
|
+
g,
|
|
3223
|
+
b
|
|
3224
|
+
]
|
|
3225
|
+
};
|
|
3226
|
+
return alpha === void 0 ? result : {
|
|
3227
|
+
...result,
|
|
3228
|
+
alpha
|
|
3229
|
+
};
|
|
3230
|
+
}
|
|
3231
|
+
/** A comma-separated list, item by item. */
|
|
3232
|
+
function items(nodes) {
|
|
3233
|
+
const list = [[]];
|
|
3234
|
+
for (const node of nodes) if (node.type === "Operator" && node.value === ",") list.push([]);
|
|
3235
|
+
else list[list.length - 1].push(node);
|
|
3236
|
+
return list;
|
|
3237
|
+
}
|
|
3238
|
+
//#endregion
|
|
3239
|
+
export { ConfigError, check, defineConfig, exportTokens, formatDiagnostic, formatGitHubAnnotation, generate, loadConfig };
|
package/dist/internal.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h as formatDiagnostic, i as createLayerWrapper, l as createMatcher, n as regenerateDts, t as generate } from "./dts
|
|
1
|
+
import { h as formatDiagnostic, i as createLayerWrapper, l as createMatcher, n as regenerateDts, t as generate } from "./dts--_fgXHCu.mjs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { watch } from "chokidar";
|
|
4
4
|
//#region src/watcher.ts
|
package/package.json
CHANGED