@homepages/concrete 2.0.0 → 3.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.
- package/CHANGELOG.md +34 -0
- package/codemods/framework-namespace.mjs +107 -0
- package/codemods/manifest.json +9 -0
- package/codemods/theme-collapse.mjs +394 -0
- package/dist/artifact/playground/client/assets/{index-CW7co-u9.js → index-B3eNADCe.js} +41 -35
- package/dist/artifact/playground/client/index.html +1 -1
- package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
- package/dist/base.css +46 -29
- package/dist/cli/check/render/class-conflicts.js +1 -1
- package/dist/cli/dev/islands.js +2 -2
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/primitives/FrameShell.d.ts +1 -1
- package/dist/primitives/FrameShell.js +1 -1
- package/dist/primitives/Image.d.ts +1 -1
- package/dist/primitives/Image.js +3 -3
- package/dist/primitives/Section.d.ts +3 -3
- package/dist/primitives/Section.js +4 -4
- package/dist/primitives/Tour.d.ts +1 -1
- package/dist/primitives/Tour.js +3 -3
- package/dist/primitives/Video.d.ts +1 -1
- package/dist/primitives/Video.js +3 -3
- package/dist/styles.css +4 -3
- package/guide/reference/cli/INDEX.md +2 -2
- package/guide/reference/cli/build.md +1 -1
- package/guide/reference/cli/check.md +1 -1
- package/guide/reference/cli/dev.md +1 -1
- package/guide/reference/cli/property-add.md +1 -1
- package/guide/reference/cli/property-list.md +1 -1
- package/guide/reference/cli/publish.md +1 -1
- package/guide/reference/cli/upgrade.md +1 -1
- package/guide/reference/rules/INDEX.md +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# @homepages/concrete
|
|
2
2
|
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dd83c66: The framework's own CSS names move onto the `concrete-` namespace: the tokens `--tr-window-h`, `--tr-ease`, `--tr-duration-fast`, `--tr-duration-base` and `--tr-image-frame-bg`, and the classes `.tr-section`, `.tr-image-block` and `.tr-image-frame`. Nothing here is yours to do by hand — `concrete upgrade` applies exactly this map for you, through the `framework-namespace` codemod this release ships.
|
|
8
|
+
|
|
9
|
+
BREAKING: `base.css` no longer declares a `--tr-*` token or emits a `.tr-*` class, and `<Section>`, `<Image>`, `<Video>` and `<Tour>` emit the `concrete-` spellings. A stylesheet still reading `var(--tr-duration-fast)` or selecting `.tr-section` now matches nothing and renders a missing value.
|
|
10
|
+
|
|
11
|
+
The framework is called concrete and its names say so. `--tr-` came from the old kit, where a vendor prefix kept generated names clear of Tailwind's — and templates ported from that kit spell their _own_ tokens with it too, so one prefix had two owners and nothing told them apart. After this change a name is the framework's if and only if it reads `concrete-`, on both sides of the colon: `.concrete-section` is the section box model, `var(--concrete-window-h)` is the window the page is being viewed in, `--concrete-ease` and `--concrete-duration-*` are the motion constants `styles.css` aliases onto Tailwind's transition defaults.
|
|
12
|
+
|
|
13
|
+
`--concrete-image-frame-bg` is the one name in the set you declare rather than only read: it retints the `<Image>`/`<Video>` placeholder fill, and falls back to a neutral grey. Every other `--concrete-*` is declared in `base.css` and read from there.
|
|
14
|
+
|
|
15
|
+
Your own tokens are a separate move. They belong in one plain `@theme` under the Tailwind namespace whose utility spends them — `--color-ink`, `--spacing-gutter`, `--container-page` — with no prefix and no `:root` mirror; the `theme-collapse` codemod collapses the doubled shape and reports the layout tokens whose new name is a judgment only you can make.
|
|
16
|
+
|
|
17
|
+
One `tr-` name is deliberately untouched: `.tr-frame`, the frame root every capture surface stamps. It is the platform's class rather than the framework's — `base.css` styles it, nothing here derives it — and it renames with the rest of the platform's names, in its own release, so that a rendered artifact and the page that previewed it never disagree about the class they share.
|
|
18
|
+
|
|
19
|
+
- 2a73297: `concrete upgrade` now carries a workspace across the `concrete-` namespace rename on its own: this major ships the `framework-namespace` codemod.
|
|
20
|
+
|
|
21
|
+
It applies the whole map and only the map — the tokens `--tr-window-h`, `--tr-ease`, `--tr-duration-fast`, `--tr-duration-base` and `--tr-image-frame-bg`, the classes `.tr-section`, `.tr-image-block` and `.tr-image-frame`, and `base.css`'s own frame witness — to every `.css`, `.ts` and `.tsx` in the workspace, in declarations, `var()` reads, selectors, `className` strings and comments alike. It is deterministic and reports nothing as yours, so whatever it rewrote is not yours to check afterwards.
|
|
22
|
+
|
|
23
|
+
Your own tokens are untouched even where they share the old prefix: a `--tr-color-ink` of yours keeps its spelling here, and the `theme-collapse` codemod in this same release is what moves it. That separation is the whole point of the rename — one prefix had two owners and nothing told them apart, and after this a name is the framework's if and only if it reads `concrete-`.
|
|
24
|
+
|
|
25
|
+
`.tr-frame`, `.tr-root` and `#tr-site-config` are deliberately left alone. Those are the platform's names, stamped onto every rendered artifact rather than derived by concrete, and they move together in a release of their own; a stylesheet selecting `.tr-frame` keeps working across this one.
|
|
26
|
+
|
|
27
|
+
- 2a73297: Manual: name the layout tokens the report lists in the Tailwind namespace whose utility spends them, then respell their uses as utilities.
|
|
28
|
+
|
|
29
|
+
BREAKING: a template declares each of its tokens ONCE, in one plain `@theme`, under the Tailwind namespace whose utility spends it — `--color-ink`, `--spacing-gutter`, `--container-page` — with no prefix and no `:root` mirror. The doubled shape a ported template inherited is retired: a `:root` of values under a `--tr-` prefix, plus an `@theme inline` block aliasing each onto the Tailwind name that yields a utility.
|
|
30
|
+
|
|
31
|
+
`concrete upgrade` runs the `theme-collapse` codemod this major ships, which does the mechanical half. Every `@theme inline { --X: var(--tr-Y) }` entry whose `:root { --tr-Y: value }` stands in the same stylesheet becomes `--X: value` in a plain `@theme`, both originals go, and every read of it is respelled — `var(--tr-Y)` to `var(--X)` in a stylesheet, and in a `className` onto the utility the namespace generates, so `bg-[var(--tr-color-ink)]` becomes `bg-ink`. A `@media` `:root` override is respelled and kept, which is the honest shape for a responsive token: `@theme` emits the token on `:root`, so the override wins by source order and every utility spending it follows.
|
|
32
|
+
|
|
33
|
+
**What it hands back.** A `:root --tr-*` with no mirror is a layout token — the old kit generated an alias only for the namespaces it knew, so `--tr-container-max`, `--tr-container-pad`, `--tr-header-height` and their neighbours never had one. Each is reported to you with the rule for naming it, and its declaration and every read of it are left exactly as they stand. The name is a judgment nothing can make for you: it has to say what the value is FOR in this template (`gutter`, `header`, `page`) rather than where it came from (`m`, `container-pad`), it belongs to the namespace whose utility spends it — a length spent through `p-*`/`h-*`/`gap-*` is `--spacing-*`, a page or column max-width is `--container-*` — and it must not collide with a stock utility, since `--container-max` would make `max-w-max` ambiguous with `max-content`.
|
|
34
|
+
|
|
35
|
+
Why the doubled shape was residue rather than design: the alias layer's only purpose in the wider world is a runtime theme swap (`.dark { --primary: … }`) that no template has, and a vendor prefix exists to keep a third-party stylesheet clear of its host's names, which your own stylesheet never needs.
|
|
36
|
+
|
|
3
37
|
## 2.0.0
|
|
4
38
|
|
|
5
39
|
### Major Changes
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// 3.0.0 — the framework's own CSS names spell `concrete-`.
|
|
2
|
+
//
|
|
3
|
+
// `base.css` used to declare its tokens under `--tr-` and emit its classes as `.tr-*`, a
|
|
4
|
+
// prefix inherited from the old kit. Templates ported from that kit spell their OWN
|
|
5
|
+
// tokens with it too, so one prefix had two owners and nothing told them apart. After
|
|
6
|
+
// this break a name is the framework's if and only if it reads `concrete-`, and
|
|
7
|
+
// `base.css` declares no `--tr-*` and emits no `.tr-*` at all — so a stylesheet still
|
|
8
|
+
// reading `var(--tr-duration-fast)` or selecting `.tr-section` matches nothing and
|
|
9
|
+
// renders a missing value.
|
|
10
|
+
//
|
|
11
|
+
// This codemod applies exactly that map, and only it: the sixteen names concrete itself
|
|
12
|
+
// emits, in every `.css`, `.ts` and `.tsx` under the workspace, in declarations, `var()`
|
|
13
|
+
// reads, selectors, `className` strings and comments alike. Everything else keeps its
|
|
14
|
+
// spelling, which is the point — `--tr-color-ink` in the same file is YOUR token, and
|
|
15
|
+
// `theme-collapse` is what moves it.
|
|
16
|
+
//
|
|
17
|
+
// THREE `tr-` NAMES ARE DELIBERATELY UNTOUCHED: `.tr-frame`, `.tr-root` and
|
|
18
|
+
// `#tr-site-config`. They are the platform's, stamped onto every rendered artifact by
|
|
19
|
+
// the publish and capture surfaces, not derived by concrete — `base.css` only styles
|
|
20
|
+
// them. They rename together in a release of their own, so that an artifact and the page
|
|
21
|
+
// that previewed it never disagree about the class they share. A workspace selecting
|
|
22
|
+
// `.tr-frame` keeps working here and moves then.
|
|
23
|
+
//
|
|
24
|
+
// Nothing is ever reported as yours: the map is total and mechanical, so whatever this
|
|
25
|
+
// rewrote is not yours to touch.
|
|
26
|
+
|
|
27
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
28
|
+
import { join, relative } from "node:path";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The §4.1 map, whole. A custom property carries its `--`; a class, id or counter name
|
|
32
|
+
* does not, which is also what keeps `--tr-image-frame-bg` and the `.tr-image-frame`
|
|
33
|
+
* class two entries rather than one prefix match.
|
|
34
|
+
*/
|
|
35
|
+
const RENAMES = new Map([
|
|
36
|
+
// Declared in base.css, read by templates.
|
|
37
|
+
["--tr-window-h", "--concrete-window-h"],
|
|
38
|
+
["--tr-ease", "--concrete-ease"],
|
|
39
|
+
["--tr-duration-fast", "--concrete-duration-fast"],
|
|
40
|
+
["--tr-duration-base", "--concrete-duration-base"],
|
|
41
|
+
// The one name in the set a template DECLARES rather than only reads.
|
|
42
|
+
["--tr-image-frame-bg", "--concrete-image-frame-bg"],
|
|
43
|
+
// base.css's frame witness, private to it: its properties and its counters.
|
|
44
|
+
["--tr-frame-window-w", "--concrete-frame-window-w"],
|
|
45
|
+
["--tr-frame-window-h", "--concrete-frame-window-h"],
|
|
46
|
+
["--tr-frame-window-mismatch", "--concrete-frame-window-mismatch"],
|
|
47
|
+
["tr-frame-owed-w", "concrete-frame-owed-w"],
|
|
48
|
+
["tr-frame-owed-h", "concrete-frame-owed-h"],
|
|
49
|
+
["tr-frame-got-w", "concrete-frame-got-w"],
|
|
50
|
+
["tr-frame-got-h", "concrete-frame-got-h"],
|
|
51
|
+
// Classes the primitives emit, and the dev canvas's own style element.
|
|
52
|
+
["tr-section", "concrete-section"],
|
|
53
|
+
["tr-image-block", "concrete-image-block"],
|
|
54
|
+
["tr-image-frame", "concrete-image-frame"],
|
|
55
|
+
["tr-canvas-shield-style", "concrete-canvas-shield-style"],
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const escaped = (name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* One name from the map, bounded.
|
|
62
|
+
*
|
|
63
|
+
* Longest alternative first, so `--tr-frame-window-w` is never read as the untouched
|
|
64
|
+
* `tr-frame` plus a suffix. The two boundaries are what make `.tr-frame` and
|
|
65
|
+
* `.tr-image-frame-bg` miss `tr-image-frame`, and what keeps `--tr-ease` clear of a
|
|
66
|
+
* template's own `--tr-ease-out`.
|
|
67
|
+
*/
|
|
68
|
+
const NAME = new RegExp(
|
|
69
|
+
`(?<![\\w-])(?:${[...RENAMES.keys()]
|
|
70
|
+
.sort((a, b) => b.length - a.length)
|
|
71
|
+
.map(escaped)
|
|
72
|
+
.join("|")})(?![\\w-])`,
|
|
73
|
+
"g",
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** The file kinds a workspace spells these names in. */
|
|
77
|
+
const EXTENSIONS = [".css", ".ts", ".tsx"];
|
|
78
|
+
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
79
|
+
|
|
80
|
+
/** Every source file under `root`, absolute, skipping build output and dependencies. */
|
|
81
|
+
async function sourceFiles(root) {
|
|
82
|
+
const found = [];
|
|
83
|
+
async function walk(dir) {
|
|
84
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
85
|
+
if (entry.isDirectory()) {
|
|
86
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
87
|
+
} else if (EXTENSIONS.some((extension) => entry.name.endsWith(extension))) {
|
|
88
|
+
found.push(join(dir, entry.name));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
await walk(root);
|
|
93
|
+
return found.sort();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
97
|
+
export default async function frameworkNamespace(root) {
|
|
98
|
+
const rewritten = [];
|
|
99
|
+
for (const file of await sourceFiles(root)) {
|
|
100
|
+
const before = await readFile(file, "utf8");
|
|
101
|
+
const after = before.replace(NAME, (name) => RENAMES.get(name) ?? name);
|
|
102
|
+
if (after === before) continue;
|
|
103
|
+
await writeFile(file, after);
|
|
104
|
+
rewritten.push(relative(root, file).split("\\").join("/"));
|
|
105
|
+
}
|
|
106
|
+
return { rewritten, notRewritten: [] };
|
|
107
|
+
}
|
package/codemods/manifest.json
CHANGED
|
@@ -24,6 +24,15 @@
|
|
|
24
24
|
"manual": [
|
|
25
25
|
"replace every `useId()` in a `\"use client\"` module with an id composed from `useIslandId()` \u2014 `` const bodyId = `${useIslandId()}-body`; `` \u2014 adding your own suffix per id, and a discriminator from your own data (a row key, a facet name) wherever one component renders more than once inside a single island."
|
|
26
26
|
]
|
|
27
|
+
},
|
|
28
|
+
"3.0.0": {
|
|
29
|
+
"codemods": [
|
|
30
|
+
"framework-namespace",
|
|
31
|
+
"theme-collapse"
|
|
32
|
+
],
|
|
33
|
+
"manual": [
|
|
34
|
+
"name the layout tokens the report lists in the Tailwind namespace whose utility spends them, then respell their uses as utilities."
|
|
35
|
+
]
|
|
27
36
|
}
|
|
28
37
|
}
|
|
29
38
|
}
|
|
@@ -0,0 +1,394 @@
|
|
|
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
|
+
const DECLARATION = /^\s*(--[A-Za-z0-9_-]+)\s*:\s*(.+?);\s*$/;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* A block body read as declarations, each carrying the lines that introduce it — its
|
|
175
|
+
* comments and the blank lines around them, so prose written about a value travels with
|
|
176
|
+
* the value when it moves. Null when a line is neither blank, nor part of a comment, nor
|
|
177
|
+
* one whole declaration.
|
|
178
|
+
*/
|
|
179
|
+
function declarations(body) {
|
|
180
|
+
const lines = body.split("\n");
|
|
181
|
+
if (lines.length > 0 && lines[0].trim() === "") lines.shift();
|
|
182
|
+
if (lines.length > 0 && lines[lines.length - 1].trim() === "") lines.pop();
|
|
183
|
+
|
|
184
|
+
const entries = [];
|
|
185
|
+
let lead = [];
|
|
186
|
+
let inComment = false;
|
|
187
|
+
for (const line of lines) {
|
|
188
|
+
const trimmed = line.trim();
|
|
189
|
+
if (inComment) {
|
|
190
|
+
lead.push(line);
|
|
191
|
+
if (trimmed.includes("*/")) inComment = false;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (trimmed === "") {
|
|
195
|
+
lead.push(line);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (trimmed.startsWith("/*")) {
|
|
199
|
+
lead.push(line);
|
|
200
|
+
if (!trimmed.includes("*/")) inComment = true;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const matched = DECLARATION.exec(line);
|
|
204
|
+
if (matched === null) return null;
|
|
205
|
+
entries.push({ lead, property: matched[1], value: matched[2].trim(), line });
|
|
206
|
+
lead = [];
|
|
207
|
+
}
|
|
208
|
+
return inComment ? null : { entries, trailing: lead };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The namespace and bare name of a Tailwind token, or null for a name outside them all. */
|
|
212
|
+
function namespaceOf(token) {
|
|
213
|
+
const namespace = NAMESPACES.find((name) => token.startsWith(`--${name}-`));
|
|
214
|
+
return namespace === undefined ? null : { namespace, name: token.slice(namespace.length + 3) };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Why an unmirrored `:root --tr-*` is the author's to name, in one sentence. */
|
|
218
|
+
function judgment(property, value) {
|
|
219
|
+
const bare = property.slice("--tr-".length);
|
|
220
|
+
const lead = `\`${property}\` has no \`@theme inline\` mirror, so its name is a judgment: `;
|
|
221
|
+
if (/(?:^|-)max(?:-|$)/.test(bare)) {
|
|
222
|
+
return `${lead}a page or column max-width -> \`--container-<name>\`, spent as \`max-w-<name>\`.`;
|
|
223
|
+
}
|
|
224
|
+
if (LENGTH.test(value) || COMPUTED.test(value)) {
|
|
225
|
+
return `${lead}a length -> \`--spacing-<name>\`, spent as \`px-<name>\`, \`h-<name>\`, \`gap-<name>\`.`;
|
|
226
|
+
}
|
|
227
|
+
return `${lead}name it under the namespace whose utility spends it (\`--color-*\`, \`--font-*\`, \`--text-*\`, \`--radius-*\`, \`--shadow-*\`, \`--spacing-*\`, \`--container-*\`).`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Start of the line holding `at`, when only whitespace precedes it there. */
|
|
231
|
+
function lineStart(css, at) {
|
|
232
|
+
const bol = css.lastIndexOf("\n", at - 1) + 1;
|
|
233
|
+
return css.slice(bol, at).trim() === "" ? bol : at;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** End of the line holding `at`, when only whitespace follows it there. */
|
|
237
|
+
function lineEnd(css, at) {
|
|
238
|
+
const eol = css.indexOf("\n", at);
|
|
239
|
+
if (eol === -1) return css.length;
|
|
240
|
+
return css.slice(at, eol).trim() === "" ? eol + 1 : at;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A block's replacement text: its prelude, the lines given, and the closing brace. */
|
|
244
|
+
function block(prelude, lines) {
|
|
245
|
+
return `${prelude} {\n${lines.join("\n")}\n}`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const MIRROR = /^var\(\s*(--tr-[A-Za-z0-9_-]+)\s*\)$/;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* One stylesheet's collapse: the edits to apply, the `--tr-*` -> `--X` map it establishes,
|
|
252
|
+
* and what it hands back to the author. `edits` is empty when the file holds no
|
|
253
|
+
* `@theme inline`; `refused` is the sentence that says the file is being left alone.
|
|
254
|
+
*/
|
|
255
|
+
function planFile(css) {
|
|
256
|
+
const rules = topLevelRules(css);
|
|
257
|
+
if (rules === null) return { refused: "its braces do not balance, so no block could be read" };
|
|
258
|
+
|
|
259
|
+
const inlineBlocks = rules.filter((rule) => /^@theme\s+inline$/.test(rule.prelude));
|
|
260
|
+
if (inlineBlocks.length === 0) return { edits: [], rename: new Map(), yours: [] };
|
|
261
|
+
const rootBlocks = rules.filter((rule) => rule.prelude === ":root");
|
|
262
|
+
|
|
263
|
+
/** Every top-level `:root` declaration, by property: the values the mirrors point at. */
|
|
264
|
+
const declared = new Map();
|
|
265
|
+
const parsedRoots = [];
|
|
266
|
+
for (const rule of rootBlocks) {
|
|
267
|
+
const parsed = declarations(css.slice(rule.open + 1, rule.close));
|
|
268
|
+
if (parsed === null) return { refused: "a `:root` block holds a line this cannot read as one declaration" };
|
|
269
|
+
parsedRoots.push({ rule, parsed });
|
|
270
|
+
for (const entry of parsed.entries) declared.set(entry.property, entry);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const rename = new Map();
|
|
274
|
+
const consumed = new Set();
|
|
275
|
+
const edits = [];
|
|
276
|
+
for (const rule of inlineBlocks) {
|
|
277
|
+
const parsed = declarations(css.slice(rule.open + 1, rule.close));
|
|
278
|
+
if (parsed === null) return { refused: "an `@theme inline` block holds a line this cannot read as one declaration" };
|
|
279
|
+
const lines = [];
|
|
280
|
+
for (const entry of parsed.entries) {
|
|
281
|
+
const mirrored = MIRROR.exec(entry.value);
|
|
282
|
+
if (mirrored === null) {
|
|
283
|
+
return { refused: `\`@theme inline\` declares \`${entry.property}: ${entry.value}\`, which is not a mirror of a \`--tr-*\` value` };
|
|
284
|
+
}
|
|
285
|
+
const source = declared.get(mirrored[1]);
|
|
286
|
+
if (source === undefined) {
|
|
287
|
+
return { refused: `\`${entry.property}\` mirrors \`${mirrored[1]}\`, which no \`:root\` in this file declares` };
|
|
288
|
+
}
|
|
289
|
+
rename.set(mirrored[1], entry.property);
|
|
290
|
+
consumed.add(mirrored[1]);
|
|
291
|
+
lines.push(...entry.lead, ...source.lead, entry.line.replace(entry.value, () => source.value));
|
|
292
|
+
}
|
|
293
|
+
// An `@theme inline` holding nothing mirrors nothing, so there is no collapse to
|
|
294
|
+
// make and rewriting it would only reformat a block nobody asked about.
|
|
295
|
+
if (parsed.entries.length === 0) continue;
|
|
296
|
+
lines.push(...parsed.trailing);
|
|
297
|
+
edits.push({ start: rule.start, end: rule.close + 1, text: block("@theme", lines) });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const yours = [];
|
|
301
|
+
for (const { rule, parsed } of parsedRoots) {
|
|
302
|
+
const kept = parsed.entries.filter((entry) => !consumed.has(entry.property));
|
|
303
|
+
for (const entry of kept) {
|
|
304
|
+
if (entry.property.startsWith("--tr-")) yours.push(judgment(entry.property, entry.value));
|
|
305
|
+
}
|
|
306
|
+
if (kept.length === 0 && parsed.trailing.every((line) => line.trim() === "")) {
|
|
307
|
+
// Nothing of the author's is left in it, so the rule goes with its blank line
|
|
308
|
+
// rather than standing empty where a reader would look for the theme.
|
|
309
|
+
const start = lineStart(css, rule.start);
|
|
310
|
+
let end = lineEnd(css, rule.close + 1);
|
|
311
|
+
if (css.slice(end).startsWith("\n")) end += 1;
|
|
312
|
+
edits.push({ start, end, text: "" });
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
// Nothing was taken out of this one, so it is rewritten as it stands — and a block
|
|
316
|
+
// written on one line would come back on four. Leave it; the read-respelling pass
|
|
317
|
+
// still reaches every declaration in it.
|
|
318
|
+
if (kept.length === parsed.entries.length) continue;
|
|
319
|
+
const lines = kept.flatMap((entry) => [...entry.lead, entry.line]);
|
|
320
|
+
edits.push({ start: rule.start, end: rule.close + 1, text: block(":root", [...lines, ...parsed.trailing]) });
|
|
321
|
+
}
|
|
322
|
+
return { edits, rename, yours };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** `text` with `edits` applied, last first so every earlier index still stands. */
|
|
326
|
+
function applyEdits(text, edits) {
|
|
327
|
+
let out = text;
|
|
328
|
+
for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
|
|
329
|
+
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** One bounded pattern over every `--tr-*` this run renamed: declarations and reads alike. */
|
|
335
|
+
function renamePattern(rename) {
|
|
336
|
+
const names = [...rename.keys()].sort((a, b) => b.length - a.length).map(escaped);
|
|
337
|
+
return new RegExp(`(?<![\\w-])(?:${names.join("|")})(?![\\w-])`, "g");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** `foo-[var(--X)]`, with the variant prefix (`hover:`) and the `!` suffix left outside. */
|
|
341
|
+
const ARBITRARY = /(?<![\w-])([a-z-]+)-\[var\(\s*(--[A-Za-z0-9_-]+)\s*\)\]/g;
|
|
342
|
+
|
|
343
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
344
|
+
export default async function themeCollapse(root) {
|
|
345
|
+
const files = await sourceFiles(root);
|
|
346
|
+
const before = new Map();
|
|
347
|
+
for (const file of files) before.set(file, await readFile(file, "utf8"));
|
|
348
|
+
|
|
349
|
+
const name = (file) => relative(root, file).split("\\").join("/");
|
|
350
|
+
const after = new Map(before);
|
|
351
|
+
const notRewritten = [];
|
|
352
|
+
const rename = new Map();
|
|
353
|
+
|
|
354
|
+
// Collapse each stylesheet that carries the doubled shape, collecting the renames the
|
|
355
|
+
// whole workspace is then respelled by. A file this cannot read is reported and left.
|
|
356
|
+
for (const file of files) {
|
|
357
|
+
if (!file.endsWith(".css")) continue;
|
|
358
|
+
const plan = planFile(before.get(file));
|
|
359
|
+
if (plan.refused !== undefined) {
|
|
360
|
+
notRewritten.push({ file: name(file), reason: plan.refused });
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
for (const reason of plan.yours) notRewritten.push({ file: name(file), reason });
|
|
364
|
+
for (const [from, to] of plan.rename) rename.set(from, to);
|
|
365
|
+
if (plan.edits.length > 0) after.set(file, applyEdits(before.get(file), plan.edits));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (rename.size > 0) {
|
|
369
|
+
const pattern = renamePattern(rename);
|
|
370
|
+
const collapsible = new Set(rename.values());
|
|
371
|
+
for (const file of files) {
|
|
372
|
+
// Every read, wherever it stands: a `var()` in a stylesheet, the property of a
|
|
373
|
+
// `@media :root` override, an arbitrary value in a `className`.
|
|
374
|
+
let text = after.get(file).replace(pattern, (token) => rename.get(token) ?? token);
|
|
375
|
+
if (file.endsWith(".ts") || file.endsWith(".tsx")) {
|
|
376
|
+
text = text.replace(ARBITRARY, (whole, prefix, token) => {
|
|
377
|
+
if (!collapsible.has(token)) return whole;
|
|
378
|
+
const parsed = namespaceOf(token);
|
|
379
|
+
if (parsed === null) return whole;
|
|
380
|
+
return (UTILITIES.get(parsed.namespace) ?? []).includes(prefix) ? `${prefix}-${parsed.name}` : whole;
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
after.set(file, text);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const rewritten = [];
|
|
388
|
+
for (const file of files) {
|
|
389
|
+
if (after.get(file) === before.get(file)) continue;
|
|
390
|
+
await writeFile(file, after.get(file));
|
|
391
|
+
rewritten.push(name(file));
|
|
392
|
+
}
|
|
393
|
+
return { rewritten, notRewritten };
|
|
394
|
+
}
|