@homepages/concrete 10.0.0 → 12.0.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.
Files changed (121) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/codemods/manifest.json +7 -154
  3. package/dist/artifact/playground/client/assets/index-BcLGpWLR.js +1266 -0
  4. package/dist/artifact/playground/client/assets/index-CxWPYOH2.css +1 -0
  5. package/dist/artifact/playground/client/index.html +2 -2
  6. package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
  7. package/dist/artifact/playground/host.mjs +39 -40
  8. package/dist/base.css +18 -65
  9. package/dist/cli/build/artifact.js +1 -1
  10. package/dist/cli/build/fixtures.js +3 -2
  11. package/dist/cli/capture/capture.js +8 -6
  12. package/dist/cli/check/stages/sections.js +1 -1
  13. package/dist/cli/dev/workspace-reader.js +4 -2
  14. package/dist/css/compile.js +3 -6
  15. package/dist/css/rules.js +1 -51
  16. package/dist/design-system/format-variants.js +1 -1
  17. package/dist/fixtures/resolve.js +5 -1
  18. package/dist/fixtures/rows.js +2 -2
  19. package/dist/index.d.ts +5 -5
  20. package/dist/packages/external/concrete/package.js +1 -1
  21. package/dist/packages/internal/fill-bake/dist/compute-reads.js +1 -1
  22. package/dist/packages/internal/fill-bake/dist/held-rows.js +2 -0
  23. package/dist/packages/internal/fill-bake/dist/index.js +1 -0
  24. package/dist/packages/internal/fill-vocabulary/dist/holder.js +3 -14
  25. package/dist/packages/internal/format-geometry/dist/capture.js +3 -11
  26. package/dist/packages/internal/format-geometry/dist/format.d.ts +2 -8
  27. package/dist/packages/internal/format-geometry/dist/geometry.js +2 -13
  28. package/dist/packages/internal/media-resolution/dist/document.js +3 -3
  29. package/dist/packages/internal/media-resolution/dist/image.js +3 -3
  30. package/dist/packages/internal/media-resolution/dist/tour.js +6 -6
  31. package/dist/packages/internal/media-resolution/dist/video.js +9 -9
  32. package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +1 -0
  33. package/dist/packages/internal/section-contracts/dist/runners.js +1 -3
  34. package/dist/packages/internal/section-contracts/dist/source-contract.js +3 -3
  35. package/dist/packages/internal/section-contracts/dist/sources-contract.js +0 -2
  36. package/dist/packages/internal/template-artifact/dist/index.js +2 -2
  37. package/dist/packages/internal/template-artifact/dist/layout.js +11 -11
  38. package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
  39. package/dist/packages/internal/template-artifact/dist/verify.js +13 -17
  40. package/dist/packages/internal/template-contracts/dist/document-value.d.ts +3 -5
  41. package/dist/packages/internal/template-contracts/dist/fact-registry.js +1 -1
  42. package/dist/packages/internal/template-contracts/dist/fill-builders.js +2 -6
  43. package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +4 -1
  44. package/dist/packages/internal/template-contracts/dist/list.js +0 -14
  45. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +1 -1
  46. package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +6 -7
  47. package/dist/packages/internal/template-contracts/dist/slot-builders.js +0 -2
  48. package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +6 -1
  49. package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +2 -4
  50. package/dist/packages/internal/template-contracts/dist/video-value.d.ts +3 -4
  51. package/dist/primitives/FrameShell.d.ts +5 -2
  52. package/dist/primitives/FrameShell.js +2 -1
  53. package/dist/primitives/Image.d.ts +2 -2
  54. package/dist/primitives/Tour.d.ts +2 -2
  55. package/dist/primitives/Video.d.ts +2 -2
  56. package/dist/primitives/slot-bindings.js +6 -13
  57. package/dist/primitives/slot-document.d.ts +1 -1
  58. package/dist/primitives/slot-download.d.ts +3 -1
  59. package/dist/primitives/slot-media.d.ts +5 -2
  60. package/dist/primitives/slot-media.js +1 -1
  61. package/dist/rules/registry.js +1 -7
  62. package/dist/schema/format-presets.d.ts +1 -1
  63. package/dist/schema/format.d.ts +0 -1
  64. package/dist/schema/format.js +1 -2
  65. package/dist/schema/manifest.js +18 -24
  66. package/dist/schema/section-schema.d.ts +5 -5
  67. package/dist/site-analytics.js +6 -1
  68. package/dist/site-runtime/window-height.js +19 -0
  69. package/dist/styles.css +4 -4
  70. package/dist/template/define-template.d.ts +11 -0
  71. package/dist/template/define-template.js +1 -1
  72. package/dist/template/materialize.js +2 -0
  73. package/guide/reference/cli/INDEX.md +2 -2
  74. package/guide/reference/cli/build.md +1 -1
  75. package/guide/reference/cli/check.md +1 -1
  76. package/guide/reference/cli/dev.md +1 -1
  77. package/guide/reference/cli/property-add.md +1 -1
  78. package/guide/reference/cli/property-list.md +1 -1
  79. package/guide/reference/cli/publish.md +1 -1
  80. package/guide/reference/cli/upgrade.md +1 -1
  81. package/guide/reference/rules/INDEX.md +2 -3
  82. package/package.json +2 -2
  83. package/codemods/fill-is-a-source.mjs +0 -533
  84. package/codemods/framework-namespace.mjs +0 -107
  85. package/codemods/list-grammar.mjs +0 -923
  86. package/codemods/object-form-only.mjs +0 -39
  87. package/codemods/platform-namespace.mjs +0 -83
  88. package/codemods/process-builders.mjs +0 -198
  89. package/codemods/reads-replace-reconcile.mjs +0 -285
  90. package/codemods/registry-keys-only.mjs +0 -113
  91. package/codemods/slot-object.mjs +0 -1097
  92. package/codemods/steps/content-object-fields.mjs +0 -324
  93. package/codemods/steps/list-object-form.mjs +0 -686
  94. package/codemods/steps/registry-key-reads.mjs +0 -276
  95. package/codemods/steps/select-object-form.mjs +0 -210
  96. package/codemods/table-rows.mjs +0 -568
  97. package/codemods/theme-collapse.mjs +0 -396
  98. package/codemods/unlayer-author-css.mjs +0 -194
  99. package/codemods/unwrap-object-fields.mjs +0 -310
  100. package/codemods/viewer-slot-document.mjs +0 -131
  101. package/codemods/write-property-as-facts-document.mjs +0 -716
  102. package/codemods/write-the-address-line.mjs +0 -255
  103. package/codemods/write-the-registry-members.mjs +0 -653
  104. package/codemods/write-the-select-from.mjs +0 -141
  105. package/codemods/write-the-sole-from.mjs +0 -369
  106. package/codemods/write-the-table-from.mjs +0 -267
  107. package/codemods/write-the-whole-item.mjs +0 -512
  108. package/codemods/writeback-drop.mjs +0 -168
  109. package/codemods/written-contact-address.mjs +0 -162
  110. package/codemods/written-media-category.mjs +0 -227
  111. package/codemods/written-ref-kinds-name-the-layer.mjs +0 -172
  112. package/codemods/written-uploads-replace-pool-reads.mjs +0 -454
  113. package/codemods/written-uploads-sort-into-lists.mjs +0 -405
  114. package/codemods/written-uploads-stated-in-sorted-lists.mjs +0 -295
  115. package/codemods/written-video-sources-drop.mjs +0 -144
  116. package/codemods/written-without-pick-none.mjs +0 -146
  117. package/codemods/written-without-retired-knowledge.mjs +0 -223
  118. package/codemods/written-without-the-media-key.mjs +0 -202
  119. package/codemods/written-without-the-property-type.mjs +0 -249
  120. package/dist/artifact/playground/client/assets/index-BFNHB7VM.css +0 -1
  121. package/dist/artifact/playground/client/assets/index-Div1yo6s.js +0 -1259
@@ -1,396 +0,0 @@
1
- // 3.0.0 — a template's own tokens are declared once, in one plain `@theme`.
2
- //
3
- // The old kit compiled a `theme.ts` object into two blocks that said the same thing
4
- // twice: a `:root` of values under its `--tr-` prefix, and an `@theme inline` block
5
- // aliasing each onto the Tailwind name that yields a utility. Ported templates wrote
6
- // that output by hand and inherited both. Neither half earns its keep — the alias layer
7
- // exists for a runtime theme swap (`.dark { --primary: … }`) no template has, and a
8
- // vendor prefix exists to keep a third-party stylesheet clear of its host's names, which
9
- // a template's own stylesheet never needs.
10
- //
11
- // So, per stylesheet: every `@theme inline { --X: var(--tr-Y) }` entry whose
12
- // `:root { --tr-Y: value }` stands in the same file becomes `--X: value` in a plain
13
- // `@theme`, both originals go, and every `var(--tr-Y)` in the workspace is respelled
14
- // `var(--X)`. In a `className` the read then collapses onto the utility the token's
15
- // namespace generates — `bg-[var(--tr-color-ink)]` becomes `bg-ink` — for the namespaces
16
- // whose utility is unambiguous. A `@media` `:root` override is respelled and kept: that
17
- // is the honest shape for a responsive token, since `@theme` emits the token on `:root`
18
- // and source order settles the override.
19
- //
20
- // WHAT IS YOURS. A `:root --tr-*` with no mirror is a layout token — `--tr-container-max`,
21
- // `--tr-container-pad`, `--tr-header-height`, `--tr-m` — and those never had an alias,
22
- // because the old kit generated one only for the namespaces it knew. Naming them is a
23
- // judgment: the name has to say what the value is FOR in this template (`gutter`,
24
- // `header`, `page`) under the Tailwind namespace whose utility spends it, and it must not
25
- // collide with a stock utility (`--container-max` would make `max-w-max` ambiguous with
26
- // `max-content`). A codemod that guessed would be inventing your vocabulary, so each one
27
- // is reported with the rule instead, and its declaration and every read of it are left
28
- // exactly as they are.
29
- //
30
- // A FILE IT WILL NOT EDIT BLIND. An `@theme inline` entry that is not a plain
31
- // `var(--tr-…)`, one whose value stands in no `:root` here, or a block holding a line
32
- // this cannot read as a whole declaration: the file is reported and left alone, rather
33
- // than half-collapsed into a stylesheet that compiles to something nobody wrote.
34
-
35
- import { readdir, readFile, writeFile } from "node:fs/promises";
36
- import { join, relative } from "node:path";
37
-
38
- const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
39
- /** The file kinds a token is declared or read in. */
40
- const EXTENSIONS = [".css", ".ts", ".tsx"];
41
-
42
- /**
43
- * Tailwind's namespaces, longest first so `--font-weight-bold` is never read as `font`.
44
- * Recognising one this codemod cannot map onto a utility is the point of listing it: the
45
- * read is still respelled, it just keeps its arbitrary-value spelling.
46
- */
47
- const NAMESPACES = [
48
- "font-weight",
49
- "breakpoint",
50
- "container",
51
- "tracking",
52
- "spacing",
53
- "leading",
54
- "shadow",
55
- "radius",
56
- "color",
57
- "text",
58
- "font",
59
- "ease",
60
- ];
61
-
62
- /**
63
- * The utility prefixes an arbitrary value collapses onto, per namespace.
64
- *
65
- * Only where the prefix names one namespace and nothing else. `h-`, `px-` and `max-w-`
66
- * are absent on purpose: the tokens they spend are the unmirrored layout ones this
67
- * codemod reports rather than renames, so there is nothing here for them to map to.
68
- */
69
- const UTILITIES = new Map([
70
- ["color", ["bg", "text", "border"]],
71
- ["font", ["font"]],
72
- ["text", ["text"]],
73
- ["radius", ["rounded"]],
74
- ["shadow", ["shadow"]],
75
- ]);
76
-
77
- /** A value that is a bare length, which is what makes an unmirrored token a spacing one. */
78
- const LENGTH = /^-?(?:\d+\.?\d*|\.\d+)(?:px|rem|em|ch|ex|vw|vh|svh|dvh|lvh|vmin|vmax|%|pt|pc|cm|mm|in|q)$/i;
79
- const COMPUTED = /^(?:calc|clamp|min|max)\(/;
80
-
81
- const escaped = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
82
-
83
- /** Every source file under `root`, absolute, skipping build output and dependencies. */
84
- async function sourceFiles(root) {
85
- const found = [];
86
- async function walk(dir) {
87
- for (const entry of await readdir(dir, { withFileTypes: true })) {
88
- if (entry.isDirectory()) {
89
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
90
- } else if (EXTENSIONS.some((extension) => entry.name.endsWith(extension))) {
91
- found.push(join(dir, entry.name));
92
- }
93
- }
94
- }
95
- await walk(root);
96
- return found.sort();
97
- }
98
-
99
- /**
100
- * `css` with every comment and string literal blanked to spaces, newlines kept, so
101
- * indices still line up with the original. Braces are counted here, which is how a `{`
102
- * inside a comment or a `content:` string never opens a block.
103
- */
104
- function masked(css) {
105
- const out = [...css];
106
- const blank = (from, to) => {
107
- for (let k = from; k < Math.min(to, css.length); k++) if (out[k] !== "\n") out[k] = " ";
108
- };
109
- for (let i = 0; i < css.length; ) {
110
- if (css[i] === "/" && css[i + 1] === "*") {
111
- const end = css.indexOf("*/", i + 2);
112
- const stop = end === -1 ? css.length : end + 2;
113
- blank(i, stop);
114
- i = stop;
115
- } else if (css[i] === '"' || css[i] === "'") {
116
- const quote = css[i];
117
- let k = i + 1;
118
- while (k < css.length && css[k] !== quote) k += css[k] === "\\" ? 2 : 1;
119
- blank(i, k + 1);
120
- i = k + 1;
121
- } else {
122
- i += 1;
123
- }
124
- }
125
- return out.join("");
126
- }
127
-
128
- /** Index of the `}` matching the `{` at `open`, or -1 if the file is unbalanced. */
129
- function matchBrace(scan, open) {
130
- let depth = 0;
131
- for (let i = open; i < scan.length; i++) {
132
- if (scan[i] === "{") depth += 1;
133
- else if (scan[i] === "}" && --depth === 0) return i;
134
- }
135
- return -1;
136
- }
137
-
138
- /**
139
- * Every TOP-LEVEL `{ … }` rule, in source order, or null when the braces do not balance.
140
- *
141
- * Top level only: a `:root` inside `@media` is an override of a value declared here, not
142
- * a second declaration of it, and reading it as one would collapse the wrong number.
143
- */
144
- function topLevelRules(css) {
145
- const scan = masked(css);
146
- const rules = [];
147
- let from = 0;
148
- for (let i = 0; i < scan.length; i++) {
149
- if (scan[i] === ";") {
150
- from = i + 1;
151
- continue;
152
- }
153
- if (scan[i] !== "{") continue;
154
- const close = matchBrace(scan, i);
155
- if (close === -1) return null;
156
- // Read from the masked text: a comment standing between two rules is blank here, so
157
- // it neither joins the prelude it precedes nor drags the block's start back over it.
158
- const prelude = scan.slice(from, i);
159
- rules.push({
160
- prelude: prelude.trim(),
161
- start: from + prelude.length - prelude.trimStart().length,
162
- open: i,
163
- close,
164
- });
165
- from = close + 1;
166
- i = close;
167
- }
168
- return rules;
169
- }
170
-
171
- /** One declaration on its line, and the one comment it may close on (`--text-xs: 0.75rem; /* 12 *\/`). */
172
- const DECLARATION = /^\s*(--[A-Za-z0-9_-]+)\s*:\s*(.+?);\s*(\/\*(?:(?!\*\/).)*\*\/)?\s*$/;
173
-
174
- /**
175
- * A block body read as declarations, each carrying the lines that introduce it — its
176
- * comments and the blank lines around them, so prose written about a value travels with
177
- * the value when it moves, and the comment closing its own line. Null when a line is
178
- * neither blank, nor part of a comment, nor one whole declaration.
179
- */
180
- function declarations(body) {
181
- const lines = body.split("\n");
182
- if (lines.length > 0 && lines[0].trim() === "") lines.shift();
183
- if (lines.length > 0 && lines[lines.length - 1].trim() === "") lines.pop();
184
-
185
- const entries = [];
186
- let lead = [];
187
- let inComment = false;
188
- for (const line of lines) {
189
- const trimmed = line.trim();
190
- if (inComment) {
191
- lead.push(line);
192
- if (trimmed.includes("*/")) inComment = false;
193
- continue;
194
- }
195
- if (trimmed === "") {
196
- lead.push(line);
197
- continue;
198
- }
199
- if (trimmed.startsWith("/*")) {
200
- lead.push(line);
201
- if (!trimmed.includes("*/")) inComment = true;
202
- continue;
203
- }
204
- const matched = DECLARATION.exec(line);
205
- if (matched === null) return null;
206
- entries.push({ lead, property: matched[1], value: matched[2].trim(), comment: matched[3], line });
207
- lead = [];
208
- }
209
- return inComment ? null : { entries, trailing: lead };
210
- }
211
-
212
- /** The namespace and bare name of a Tailwind token, or null for a name outside them all. */
213
- function namespaceOf(token) {
214
- const namespace = NAMESPACES.find((name) => token.startsWith(`--${name}-`));
215
- return namespace === undefined ? null : { namespace, name: token.slice(namespace.length + 3) };
216
- }
217
-
218
- /** Why an unmirrored `:root --tr-*` is the author's to name, in one sentence. */
219
- function judgment(property, value) {
220
- const bare = property.slice("--tr-".length);
221
- const lead = `\`${property}\` has no \`@theme inline\` mirror, so its name is a judgment: `;
222
- if (/(?:^|-)max(?:-|$)/.test(bare)) {
223
- return `${lead}a page or column max-width -> \`--container-<name>\`, spent as \`max-w-<name>\`.`;
224
- }
225
- if (LENGTH.test(value) || COMPUTED.test(value)) {
226
- return `${lead}a length -> \`--spacing-<name>\`, spent as \`px-<name>\`, \`h-<name>\`, \`gap-<name>\`.`;
227
- }
228
- return `${lead}name it under the namespace whose utility spends it (\`--color-*\`, \`--font-*\`, \`--text-*\`, \`--radius-*\`, \`--shadow-*\`, \`--spacing-*\`, \`--container-*\`).`;
229
- }
230
-
231
- /** Start of the line holding `at`, when only whitespace precedes it there. */
232
- function lineStart(css, at) {
233
- const bol = css.lastIndexOf("\n", at - 1) + 1;
234
- return css.slice(bol, at).trim() === "" ? bol : at;
235
- }
236
-
237
- /** End of the line holding `at`, when only whitespace follows it there. */
238
- function lineEnd(css, at) {
239
- const eol = css.indexOf("\n", at);
240
- if (eol === -1) return css.length;
241
- return css.slice(at, eol).trim() === "" ? eol + 1 : at;
242
- }
243
-
244
- /** A block's replacement text: its prelude, the lines given, and the closing brace. */
245
- function block(prelude, lines) {
246
- return `${prelude} {\n${lines.join("\n")}\n}`;
247
- }
248
-
249
- const MIRROR = /^var\(\s*(--tr-[A-Za-z0-9_-]+)\s*\)$/;
250
-
251
- /**
252
- * One stylesheet's collapse: the edits to apply, the `--tr-*` -> `--X` map it establishes,
253
- * and what it hands back to the author. `edits` is empty when the file holds no
254
- * `@theme inline`; `refused` is the sentence that says the file is being left alone.
255
- */
256
- function planFile(css) {
257
- const rules = topLevelRules(css);
258
- if (rules === null) return { refused: "its braces do not balance, so no block could be read" };
259
-
260
- const inlineBlocks = rules.filter((rule) => /^@theme\s+inline$/.test(rule.prelude));
261
- if (inlineBlocks.length === 0) return { edits: [], rename: new Map(), yours: [] };
262
- const rootBlocks = rules.filter((rule) => rule.prelude === ":root");
263
-
264
- /** Every top-level `:root` declaration, by property: the values the mirrors point at. */
265
- const declared = new Map();
266
- const parsedRoots = [];
267
- for (const rule of rootBlocks) {
268
- const parsed = declarations(css.slice(rule.open + 1, rule.close));
269
- if (parsed === null) return { refused: "a `:root` block holds a line this cannot read as one declaration" };
270
- parsedRoots.push({ rule, parsed });
271
- for (const entry of parsed.entries) declared.set(entry.property, entry);
272
- }
273
-
274
- const rename = new Map();
275
- const consumed = new Set();
276
- const edits = [];
277
- for (const rule of inlineBlocks) {
278
- const parsed = declarations(css.slice(rule.open + 1, rule.close));
279
- if (parsed === null) return { refused: "an `@theme inline` block holds a line this cannot read as one declaration" };
280
- const lines = [];
281
- for (const entry of parsed.entries) {
282
- const mirrored = MIRROR.exec(entry.value);
283
- if (mirrored === null) {
284
- return { refused: `\`@theme inline\` declares \`${entry.property}: ${entry.value}\`, which is not a mirror of a \`--tr-*\` value` };
285
- }
286
- const source = declared.get(mirrored[1]);
287
- if (source === undefined) {
288
- return { refused: `\`${entry.property}\` mirrors \`${mirrored[1]}\`, which no \`:root\` in this file declares` };
289
- }
290
- rename.set(mirrored[1], entry.property);
291
- consumed.add(mirrored[1]);
292
- const collapsed = entry.line.replace(entry.value, () => source.value);
293
- lines.push(...entry.lead, ...source.lead, source.comment === undefined ? collapsed : `${collapsed.trimEnd()} ${source.comment}`);
294
- }
295
- // An `@theme inline` holding nothing mirrors nothing, so there is no collapse to
296
- // make and rewriting it would only reformat a block nobody asked about.
297
- if (parsed.entries.length === 0) continue;
298
- lines.push(...parsed.trailing);
299
- edits.push({ start: rule.start, end: rule.close + 1, text: block("@theme", lines) });
300
- }
301
-
302
- const yours = [];
303
- for (const { rule, parsed } of parsedRoots) {
304
- const kept = parsed.entries.filter((entry) => !consumed.has(entry.property));
305
- for (const entry of kept) {
306
- if (entry.property.startsWith("--tr-")) yours.push(judgment(entry.property, entry.value));
307
- }
308
- if (kept.length === 0 && parsed.trailing.every((line) => line.trim() === "")) {
309
- // Nothing of the author's is left in it, so the rule goes with its blank line
310
- // rather than standing empty where a reader would look for the theme.
311
- const start = lineStart(css, rule.start);
312
- let end = lineEnd(css, rule.close + 1);
313
- if (css.slice(end).startsWith("\n")) end += 1;
314
- edits.push({ start, end, text: "" });
315
- continue;
316
- }
317
- // Nothing was taken out of this one, so it is rewritten as it stands — and a block
318
- // written on one line would come back on four. Leave it; the read-respelling pass
319
- // still reaches every declaration in it.
320
- if (kept.length === parsed.entries.length) continue;
321
- const lines = kept.flatMap((entry) => [...entry.lead, entry.line]);
322
- edits.push({ start: rule.start, end: rule.close + 1, text: block(":root", [...lines, ...parsed.trailing]) });
323
- }
324
- return { edits, rename, yours };
325
- }
326
-
327
- /** `text` with `edits` applied, last first so every earlier index still stands. */
328
- function applyEdits(text, edits) {
329
- let out = text;
330
- for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
331
- out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
332
- }
333
- return out;
334
- }
335
-
336
- /** One bounded pattern over every `--tr-*` this run renamed: declarations and reads alike. */
337
- function renamePattern(rename) {
338
- const names = [...rename.keys()].sort((a, b) => b.length - a.length).map(escaped);
339
- return new RegExp(`(?<![\\w-])(?:${names.join("|")})(?![\\w-])`, "g");
340
- }
341
-
342
- /** `foo-[var(--X)]`, with the variant prefix (`hover:`) and the `!` suffix left outside. */
343
- const ARBITRARY = /(?<![\w-])([a-z-]+)-\[var\(\s*(--[A-Za-z0-9_-]+)\s*\)\]/g;
344
-
345
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
346
- export default async function themeCollapse(root) {
347
- const files = await sourceFiles(root);
348
- const before = new Map();
349
- for (const file of files) before.set(file, await readFile(file, "utf8"));
350
-
351
- const name = (file) => relative(root, file).split("\\").join("/");
352
- const after = new Map(before);
353
- const notRewritten = [];
354
- const rename = new Map();
355
-
356
- // Collapse each stylesheet that carries the doubled shape, collecting the renames the
357
- // whole workspace is then respelled by. A file this cannot read is reported and left.
358
- for (const file of files) {
359
- if (!file.endsWith(".css")) continue;
360
- const plan = planFile(before.get(file));
361
- if (plan.refused !== undefined) {
362
- notRewritten.push({ file: name(file), reason: plan.refused });
363
- continue;
364
- }
365
- for (const reason of plan.yours) notRewritten.push({ file: name(file), reason });
366
- for (const [from, to] of plan.rename) rename.set(from, to);
367
- if (plan.edits.length > 0) after.set(file, applyEdits(before.get(file), plan.edits));
368
- }
369
-
370
- if (rename.size > 0) {
371
- const pattern = renamePattern(rename);
372
- const collapsible = new Set(rename.values());
373
- for (const file of files) {
374
- // Every read, wherever it stands: a `var()` in a stylesheet, the property of a
375
- // `@media :root` override, an arbitrary value in a `className`.
376
- let text = after.get(file).replace(pattern, (token) => rename.get(token) ?? token);
377
- if (file.endsWith(".ts") || file.endsWith(".tsx")) {
378
- text = text.replace(ARBITRARY, (whole, prefix, token) => {
379
- if (!collapsible.has(token)) return whole;
380
- const parsed = namespaceOf(token);
381
- if (parsed === null) return whole;
382
- return (UTILITIES.get(parsed.namespace) ?? []).includes(prefix) ? `${prefix}-${parsed.name}` : whole;
383
- });
384
- }
385
- after.set(file, text);
386
- }
387
- }
388
-
389
- const rewritten = [];
390
- for (const file of files) {
391
- if (after.get(file) === before.get(file)) continue;
392
- await writeFile(file, after.get(file));
393
- rewritten.push(name(file));
394
- }
395
- return { rewritten, notRewritten };
396
- }
@@ -1,194 +0,0 @@
1
- // 2.0.0 — author CSS is no longer layered.
2
- //
3
- // `base.css` used to declare `@layer theme, base, components, sections, template,
4
- // utilities`, and a workspace wrapped its own rules in `sections.<name>` / `template` to
5
- // sit in that order. Those two layers ranked BELOW `utilities`, so a declaration in one
6
- // lost to any utility setting the same property on the same element, whatever its
7
- // selector's specificity. `base.css` no longer declares them, which flips the trap
8
- // rather than closing it: an undeclared layer is sorted AFTER every declared one, so a
9
- // leftover wrapper would make the same rule start winning instead. Neither ordering is
10
- // something an author asked for, so this codemod removes the wrappers and `check`
11
- // refuses the names from here on.
12
- //
13
- // What it does, per `.css` file in the workspace:
14
- // - unwraps `@layer sections.<name> { … }` and `@layer template { … }`, de-indenting
15
- // - deletes `@layer sections.a, sections.b;` ordering statements
16
- // - rewrites `@import "pkg/x.css" layer(sections.<name>)` to `layer(components)`,
17
- // which still demotes a third-party sheet below your own now-unlayered rules
18
- //
19
- // What it does NOT do: judge the cascade. Where a rule of yours and a utility on the
20
- // same element set the same property, your rule now wins where the utility used to.
21
- // That is the point of the change, and a handful of those flips are ones you want to
22
- // look at — a utility applied conditionally (`className={hidden ? "hidden" : ""}`) that
23
- // was counting on outranking your stylesheet is the shape to check first.
24
-
25
- import { readdir, readFile, writeFile } from "node:fs/promises";
26
- import { join, relative } from "node:path";
27
-
28
- /** The layer names the old build owned. A workspace may not declare either any more. */
29
- const RETIRED = new Set(["sections", "template"]);
30
-
31
- const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
32
-
33
- /** Every `.css` file under `root`, absolute, skipping build output and dependencies. */
34
- async function cssFiles(root) {
35
- const found = [];
36
- async function walk(dir) {
37
- for (const entry of await readdir(dir, { withFileTypes: true })) {
38
- if (entry.isDirectory()) {
39
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
40
- } else if (entry.name.endsWith(".css")) {
41
- found.push(join(dir, entry.name));
42
- }
43
- }
44
- }
45
- await walk(root);
46
- return found;
47
- }
48
-
49
- /**
50
- * `css` with every comment and string literal blanked to spaces, newlines kept, so
51
- * indices still line up with the original. Searching happens here; edits apply to the
52
- * original — which is how `@layer sections.<name>` written in a comment stays prose.
53
- */
54
- function masked(css) {
55
- const out = [...css];
56
- const blank = (from, to) => {
57
- for (let k = from; k < Math.min(to, css.length); k++) if (out[k] !== "\n") out[k] = " ";
58
- };
59
- for (let i = 0; i < css.length; ) {
60
- if (css[i] === "/" && css[i + 1] === "*") {
61
- const end = css.indexOf("*/", i + 2);
62
- const stop = end === -1 ? css.length : end + 2;
63
- blank(i, stop);
64
- i = stop;
65
- } else if (css[i] === '"' || css[i] === "'") {
66
- const quote = css[i];
67
- let k = i + 1;
68
- while (k < css.length && css[k] !== quote) k += css[k] === "\\" ? 2 : 1;
69
- blank(i, k + 1);
70
- i = k + 1;
71
- } else {
72
- i += 1;
73
- }
74
- }
75
- return out.join("");
76
- }
77
-
78
- /** `sections.hero` → `sections`; the root segment is what the order is declared on. */
79
- const rootOf = (name) => name.trim().split(".")[0].trim();
80
-
81
- /** Index just past the `}` matching the `{` at `open`, or -1 if the file is unbalanced. */
82
- function matchBrace(scan, open) {
83
- let depth = 0;
84
- for (let i = open; i < scan.length; i++) {
85
- if (scan[i] === "{") depth += 1;
86
- else if (scan[i] === "}" && --depth === 0) return i;
87
- }
88
- return -1;
89
- }
90
-
91
- /** Start of the line holding `at`, when only whitespace precedes it on that line. */
92
- function lineStart(css, at) {
93
- const bol = css.lastIndexOf("\n", at - 1) + 1;
94
- return css.slice(bol, at).trim() === "" ? bol : at;
95
- }
96
-
97
- /** End of the line holding `at`, when only whitespace follows it on that line. */
98
- function lineEnd(css, at) {
99
- const eol = css.indexOf("\n", at);
100
- if (eol === -1) return css.length;
101
- return css.slice(at, eol).trim() === "" ? eol + 1 : at;
102
- }
103
-
104
- /** A layer block's body, stripped of the one indent level the wrapper added. */
105
- function dedent(body) {
106
- const lines = body.split("\n");
107
- while (lines.length > 0 && lines[0].trim() === "") lines.shift();
108
- while (lines.length > 0 && lines[lines.length - 1].trim() === "") lines.pop();
109
- const indents = lines.filter((l) => l.trim() !== "").map((l) => /^[ \t]*/.exec(l)[0].length);
110
- const strip = indents.length === 0 ? 0 : Math.min(...indents);
111
- return lines.map((l) => (l.trim() === "" ? "" : l.slice(strip))).join("\n");
112
- }
113
-
114
- /**
115
- * One retired-layer edit, or null when the file holds none left.
116
- *
117
- * Exactly one per call so the caller can re-scan between edits: every edit moves the
118
- * offsets after it, and a nested wrapper would be read against a stale index otherwise.
119
- */
120
- function nextEdit(css) {
121
- const scan = masked(css);
122
-
123
- // `@import … layer(sections.<name>)` — the third-party demotion, re-pointed.
124
- for (const m of scan.matchAll(/\blayer\(\s*([^)]*?)\s*\)/gi)) {
125
- if (!RETIRED.has(rootOf(m[1]))) continue;
126
- return { start: m.index, end: m.index + m[0].length, text: "layer(components)" };
127
- }
128
-
129
- for (const m of scan.matchAll(/@layer\b/gi)) {
130
- const at = m.index;
131
- let i = at + m[0].length;
132
- while (i < scan.length && scan[i] !== "{" && scan[i] !== ";") i += 1;
133
- if (i >= scan.length) continue;
134
- const names = css
135
- .slice(at + m[0].length, i)
136
- .split(",")
137
- .map(rootOf)
138
- .filter((n) => n !== "");
139
- if (!names.some((n) => RETIRED.has(n))) continue;
140
-
141
- // A statement: `@layer a, b;`. Whole thing goes, with its line.
142
- if (scan[i] === ";") {
143
- if (!names.every((n) => RETIRED.has(n))) {
144
- return { mixed: names.filter((n) => !RETIRED.has(n)) };
145
- }
146
- return { start: lineStart(css, at), end: lineEnd(css, i + 1), text: "" };
147
- }
148
-
149
- // A block: `@layer sections.hero { … }`. Wrapper goes, body stays, de-indented.
150
- const close = matchBrace(scan, i);
151
- if (close === -1) return { unbalanced: names[0] };
152
- return {
153
- start: lineStart(css, at),
154
- end: lineEnd(css, close + 1),
155
- text: `${dedent(css.slice(i + 1, close))}\n`,
156
- };
157
- }
158
- return null;
159
- }
160
-
161
- /** Applies every retired-layer edit to one file's text. */
162
- function unlayer(css) {
163
- let out = css;
164
- for (let pass = 0; pass < 500; pass += 1) {
165
- const edit = nextEdit(out);
166
- if (edit === null) return { css: out };
167
- if (edit.mixed !== undefined) {
168
- return { reason: `an @layer statement names both retired and live layers (${edit.mixed.join(", ")}); split it by hand` };
169
- }
170
- if (edit.unbalanced !== undefined) {
171
- return { reason: `@layer ${edit.unbalanced} is never closed; the file's braces are unbalanced` };
172
- }
173
- out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
174
- }
175
- return { reason: "more than 500 retired-layer edits in one file, which is not a shape this codemod expects" };
176
- }
177
-
178
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
179
- export default async function unlayerAuthorCss(root) {
180
- const rewritten = [];
181
- const notRewritten = [];
182
- for (const file of await cssFiles(root)) {
183
- const before = await readFile(file, "utf8");
184
- const name = relative(root, file).split("\\").join("/");
185
- const result = unlayer(before);
186
- if (result.reason !== undefined) {
187
- notRewritten.push({ file: name, reason: result.reason });
188
- } else if (result.css !== before) {
189
- await writeFile(file, result.css);
190
- rewritten.push(name);
191
- }
192
- }
193
- return { rewritten, notRewritten };
194
- }