@homepages/concrete 1.0.2 → 2.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 +112 -0
- package/codemods/manifest.json +12 -0
- package/codemods/unlayer-author-css.mjs +194 -0
- package/dist/artifact/playground/client/assets/index-CW7co-u9.js +1101 -0
- package/dist/artifact/playground/client/assets/index-CckS2ONm.css +1 -0
- package/dist/artifact/playground/client/index.html +2 -2
- package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
- package/dist/artifact/playground/host.mjs +24 -24
- package/dist/base.css +15 -2
- package/dist/cli/build/artifact.js +5 -2
- package/dist/cli/build/captures.js +15 -18
- package/dist/cli/build/fixtures.js +11 -3
- package/dist/cli/build/sections.js +13 -4
- package/dist/cli/build/static.js +1 -1
- package/dist/cli/check/load-workspace.js +6 -1
- package/dist/cli/check/stages/bundles.js +3 -1
- package/dist/cli/commands.js +1 -1
- package/dist/cli/dev/load-template.js +32 -25
- package/dist/cli/dev/server.js +11 -3
- package/dist/cli/dev/vite.js +78 -22
- package/dist/cli/dev/workspace-reader.js +2 -6
- package/dist/cli/media/derive.js +30 -3
- package/dist/cli/media/deriver-plugin.js +6 -2
- package/dist/cli/media/frame-rung-floor.js +18 -0
- package/dist/css/compile.js +26 -4
- package/dist/css/graph.js +2 -1
- package/dist/css/rules.js +72 -1
- package/dist/eslint/authoring.js +2 -0
- package/dist/eslint/rules/island-ids.js +39 -0
- package/dist/fixtures/load.js +6 -1
- package/dist/fixtures/media-plugin.js +32 -17
- package/dist/fixtures/media.js +41 -8
- package/dist/fixtures/package-stylesheets.js +59 -0
- package/dist/fixtures/property.js +40 -9
- package/dist/fixtures/resolve.js +58 -5
- package/dist/fixtures/rows.js +6 -1
- package/dist/fixtures.d.ts +80 -12
- package/dist/fixtures.js +7 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/islands/island-id.js +8 -0
- package/dist/islands/marker.d.ts +14 -4
- package/dist/islands/marker.js +18 -6
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/packages/internal/island-runtime/dist/index.js +4 -2
- package/dist/packages/internal/island-runtime/dist/island-id-context.js +15 -0
- package/dist/packages/internal/island-runtime/dist/use-island-id.d.ts +24 -0
- package/dist/packages/internal/island-runtime/dist/use-island-id.js +44 -0
- package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/slot-catalog.js +44 -2
- package/dist/primitives/slot-bindings.d.ts +19 -13
- package/dist/primitives/slot-bindings.js +7 -3
- package/dist/rules/registry.js +19 -0
- package/dist/section/section.d.ts +6 -1
- package/dist/template/logic-shim.d.ts +2 -1
- package/dist/template/logic-shim.js +2 -1
- package/guide/reference/cli/INDEX.md +2 -2
- package/guide/reference/cli/build.md +2 -2
- 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 +4 -1
- package/package.json +1 -1
- package/dist/artifact/playground/client/assets/index-BSA1yutg.css +0 -1
- package/dist/artifact/playground/client/assets/index-JloQyj8h.js +0 -1101
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,117 @@
|
|
|
1
1
|
# @homepages/concrete
|
|
2
2
|
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 3456711: Manual: replace every `useId()` in a `"use client"` module with an id composed from `useIslandId()` — `` const bodyId = `${useIslandId()}-body`; `` — 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.
|
|
8
|
+
|
|
9
|
+
BREAKING: `useId` in an island is now the error `concrete/islands/island-ids`, and `useIslandId` outside one is the same rule's other half.
|
|
10
|
+
|
|
11
|
+
An island renders twice — once on the server, inside its whole section, and once in the
|
|
12
|
+
browser, where hydration is rooted at the island itself. React's `useId` counts position
|
|
13
|
+
from the root of the tree it is rendering, so those two renders mint different ids, and
|
|
14
|
+
markup carrying one hydrates into an attribute mismatch reported in every visitor's
|
|
15
|
+
console. Passing a prefix cannot reconcile it; the id has to travel from one render to
|
|
16
|
+
the other as data, which is what `useIslandId` is: the id the server minted for this
|
|
17
|
+
island, read back from its marker when the browser takes over.
|
|
18
|
+
|
|
19
|
+
It is one value per island, at every depth — a nested island reads the id of the island
|
|
20
|
+
that hydrates it — so the part that distinguishes one id from another is yours to add.
|
|
21
|
+
Outside an island it is a fixed constant, which is why the rule's other half keeps
|
|
22
|
+
server-rendered code off it: a section renders once and hydrates never, so it has no id
|
|
23
|
+
to hand out and gives one to the island as a prop instead.
|
|
24
|
+
|
|
25
|
+
The rule finds the calls; most are a one-line change. Ids inside a dialog or a menu the
|
|
26
|
+
server never renders open were never wrong, but they are ported the same way.
|
|
27
|
+
|
|
28
|
+
- c3ac7da: Your CSS is no longer demoted below Tailwind's utilities. `base.css` used to declare
|
|
29
|
+
`@layer theme, base, components, sections, template, utilities`, and a workspace wrapped
|
|
30
|
+
its own rules in `sections.<name>` or `template` to sit in that order — below
|
|
31
|
+
`utilities`, where a declaration lost to any utility setting the same property on the
|
|
32
|
+
same element, however specific its selector, and only on the published page. Those two
|
|
33
|
+
layers are gone. Write a plain rule and it applies: unlayered CSS outranks every layer,
|
|
34
|
+
which is stock Tailwind v4, and where two of your stylesheets meet, specificity and
|
|
35
|
+
import order settle it as they always did.
|
|
36
|
+
|
|
37
|
+
Two things follow. A `:root` token you re-declare inside a section now takes effect
|
|
38
|
+
instead of losing to your entry stylesheet's own `:root`. And a utility you apply
|
|
39
|
+
conditionally — `className={present ? "" : "hidden"}` — no longer outranks a `display`
|
|
40
|
+
your stylesheet sets on that element, so give that property one owner: key the
|
|
41
|
+
stylesheet on a `data-*` attribute rather than toggling a class beside it. Anywhere a
|
|
42
|
+
rule of yours and a utility set the same property, your rule now wins where the utility
|
|
43
|
+
used to; that is the point of the change, and the conditional case is the one worth
|
|
44
|
+
looking at.
|
|
45
|
+
|
|
46
|
+
`concrete upgrade` runs the `unlayer-author-css` codemod, which unwraps your
|
|
47
|
+
`@layer sections.<name>` and `@layer template` blocks, deletes their ordering
|
|
48
|
+
statements, and re-points an `@import … layer(sections.<name>)` at `layer(components)`
|
|
49
|
+
so a third-party sheet stays below your own rules. It does not judge the cascade.
|
|
50
|
+
`concrete check` now refuses either layer name (`css/retired-layer`), because with them
|
|
51
|
+
undeclared a leftover wrapper would sort _after_ the utilities and flip the trap rather
|
|
52
|
+
than close it.
|
|
53
|
+
|
|
54
|
+
### Minor Changes
|
|
55
|
+
|
|
56
|
+
- d1a9bff: A fixture can now state a contact's image on an `image` slot. An image slot can fill from a contact card — `imageAssign({ …, fallback: contactAssign({ field: "logo_dark" }) })` bakes the chosen card's brand mark when no upload is one — but the card's avatar and logos live on the card rather than in a media pool, so no property entry named them and the slot could only be left empty in a fixture. `ImageEntry` now also admits a `ContactImageEntry`, the card plus the field: `content(Overview, { developerLogo: { contact: listingAgent, field: "logo_dark" } })`, where `field` is `avatar`, `logo_dark` or `logo_light`. It resolves to the same `{ asset_id }` the fill's bake writes for that field, so the fixture shows what a real fill of the property shows; a card the property does not carry, or a field the card holds `null`, is a `fixtures/media-not-in-property` check error like any other missing media.
|
|
57
|
+
- a6d2b71: A fixture can now state the fields a fill writes onto a collection-bound record list —
|
|
58
|
+
the residence descriptions, per-unit videos and tour picks a `units` list carries.
|
|
59
|
+
|
|
60
|
+
Such a list was unauthorable: its rows come from the property, so `content()` refused
|
|
61
|
+
the slot outright and the resolver took the bake and looked no further. That is only
|
|
62
|
+
half of what the platform does. A fill originates those rows from the facts and then
|
|
63
|
+
plans every field carrying no `source:` onto them in one call, and the row it stores
|
|
64
|
+
holds both halves — which is why a template that puts its residence copy, its unit
|
|
65
|
+
videos or its tour picks on the units list could not show any of them in `dev`, in the
|
|
66
|
+
playground or in a capture, however the fixture was written.
|
|
67
|
+
|
|
68
|
+
A content entry may now state such a list, and states only the enriched half: the
|
|
69
|
+
bound fields are typed away, the rows merge onto the bake by position, and a field a
|
|
70
|
+
row leaves unstated keeps the blank a fill that abstained would leave. Stating more
|
|
71
|
+
rows than the property carries is the new `fixtures/rows-not-in-property` check error,
|
|
72
|
+
which names the instance and both counts.
|
|
73
|
+
|
|
74
|
+
### Patch Changes
|
|
75
|
+
|
|
76
|
+
- 93ef972: Image crop: a crop no longer depends on the browser tab that asked for it. The crop is written onto the slot the moment you press Apply, and the image resolver keeps serving what the slot showed before until the cropped pixels exist — so a crop job slower than the editor's watch, a reload, or a closed tab no longer discards it in silence. A crop that genuinely fails is taken back off the slot and reported there; a crop the editor stops watching says it is still processing, rather than clearing as if nothing had happened.
|
|
77
|
+
- d187883: An island's asset imports now name the same URL in `concrete dev` that the server
|
|
78
|
+
render names, so a section hydrates without an attribute mismatch on its own icons.
|
|
79
|
+
`dev` runs a workspace through two module graphs — esbuild for the server render, Vite
|
|
80
|
+
for the islands — and an island is in both, so an asset import had to agree in each.
|
|
81
|
+
It did not: Vite inlined any file under its 4 KB threshold as a `data:` URI and named a
|
|
82
|
+
raster by its workspace path, where the server named both under `/fixture-media/`. React
|
|
83
|
+
reported a hydration mismatch on the element carrying the asset, once per page, in every
|
|
84
|
+
developer's console — a framework error a browser proof over a template's fixtures then
|
|
85
|
+
had to special-case to find a real defect. Both graphs now resolve an asset through one
|
|
86
|
+
function, so there is nothing for them to disagree on. A built artifact was never
|
|
87
|
+
affected: both of its graphs are esbuild's.
|
|
88
|
+
- 6aa9426: A fixture link video or tour now resolves to its provider's embed URL, as the platform's resolvers emit it, so it plays in `concrete dev`, the playground and captures. A Vimeo or YouTube page link (`https://vimeo.com/530991517`, `watch?v=…`) becomes its player URL, and a Matterport share link becomes `show/?m=<sid>&nt=1`. A link that embeds nowhere, such as a Matterport `discover/space` link, resolves with `url: ""`, the renderer's empty branch, where it used to reach the iframe unrewritten. The property entry still keeps the URL as pasted.
|
|
89
|
+
- a22b34f: A Matterport 3D tour now resolves to `show/?m=<sid>&nt=1`, so a visitor on a phone keeps the page the tour is embedded in. Matterport's mobile Showcase navigates the top-level page out of the iframe of its own accord when the visitor taps play, replacing the whole host page with the provider's; `nt=1` is Matterport's own supported setting for opening the tour alongside the page instead. The parameter is mobile-only, so on a desktop the tour still plays inside its container exactly as before. A fixture property entry still keeps the tour link as pasted.
|
|
90
|
+
- 664793f: The fact catalog now grades the property's `narrative` as optional rather than blocking. A template that reads `f.narrative` no longer makes the description a question the property must answer before it can generate — the platform still asks for one on every property, but a listing whose context arrived as uploaded documents is not held back by an empty one. Nothing about how a section reads the fact changes; it can be blank where it could only be blank-and-unfinishable before.
|
|
91
|
+
- 5994cb9: The template stylesheet now resolves `@import "tailwindcss"` from the framework's own
|
|
92
|
+
Tailwind, not from the workspace. Tailwind's compiler already came from the framework,
|
|
93
|
+
and the stylesheet it compiles must match that compiler's version. The old lookup only
|
|
94
|
+
worked when a package manager hoisted `tailwindcss` beside the framework. Where it did
|
|
95
|
+
not, as in a pnpm workspace, `concrete dev` answered `/stylesheets/template.css` with
|
|
96
|
+
an error, so every page and capture frame rendered unstyled, and `check` and `build`
|
|
97
|
+
failed the same way. A workspace installed from npm compiles the same stylesheet as
|
|
98
|
+
before, byte for byte.
|
|
99
|
+
|
|
100
|
+
## 1.0.3
|
|
101
|
+
|
|
102
|
+
### Patch Changes
|
|
103
|
+
|
|
104
|
+
- 4ee9332: A section can read every member the platform bakes onto a collection-bound row. A record list bound to a collection (`source: fact.property.comparables`, `fact.property.units`, `fact.property.contacts`) keeps each row's projection members on the bound row (`slot.comps.value[n].status`, `bindItems(slot.comps)[n].value.lat`), typed from the projection, whether or not the section declares them as row fields. Binding used to drop every undeclared member, and a render function receives no raw `slots` prop, so markup could only reach a member by declaring it as a locked row field. Declared fields still normalize by their kind, and a record list the editor stores keeps only its declared fields.
|
|
105
|
+
- df450d8: `concrete build` no longer fails a capture with "page.evaluate: Execution context was destroyed, most likely because of a navigation" when an island imports a package the Vite dependency cache under `node_modules/.vite` has not optimized yet. This hit the first build after adding such an import, and passed on the second build. Before its first capture, the capture host now loads the island modules and waits until Vite has optimized every package they import. Vite's reload for a late package then happens before any page is open. `concrete dev`'s capture downloads wait the same way. Deleting `node_modules/.vite` is no longer needed to get past the error.
|
|
106
|
+
- 8cf7828: A fixture can state a floor plan on an `image` slot without a cast. `content(Section, { floor_plan: floorPlans[0] })` no longer fails `concrete check`'s typecheck with "Type 'FloorPlanEntry' is not assignable … missing … tags, caption", so an `as unknown as PhotoEntry` written to get past it can be deleted. An image slot, alone or in a list, now takes a photo or a floor plan entry (the new `ImageEntry` type from `@homepages/concrete/fixtures`), matching what the resolver already serves as image media. Any other value on an image slot is still a type error, including a floor plan with extra keys.
|
|
107
|
+
- 7ca373c: `concrete build`, `check` and `dev` no longer stall on a template that imports a large icon package such as `lucide-react` or `lucide`. Finding the template's stylesheets used to inspect every module in the import graph, and a package with thousands of icon modules made that take a minute or more on a busy machine, long enough for `build`'s first capture to time out with `page.goto: Timeout 30000ms exceeded`. The walk now only inspects imports that can name a stylesheet (package specifiers and `.css` paths), so the same template's stylesheets are found in about the time a plain bundle takes. Package CSS imported by subpath (`swiper/css`) and relative `.css` imports are still found, in import order.
|
|
108
|
+
- fef0e5d: `concrete dev` and the playground draw a units list's floor plans again. A record-list row's `image` field that holds a list of floor-plan assignments (a unit's `floor_plans`) rendered as `null`, so every unit showed its no-plans state; each assignment now resolves to the plan a section reads (`id`, `upload_id`, `url`, `alt`, `floor_label`, `position`, and `responsive` when the file has a ladder), the same shape a published page receives. An editor change to that row keeps its plans.
|
|
109
|
+
- 19c0d76: Fixture videos and tours can carry a poster. A link video or a tour entry takes an optional `thumbnail_source_url`, the provider thumbnail, served as the value's `poster` and kept a link. An uploaded video entry takes an optional `poster`, an imported image served like any other (`poster_responsive` carries its ladder) and shipped in the artifact's assets by `build`. Both fields are stripped before the property is validated against the platform schema. An entry without either still resolves with `poster: ""`, as before. The playground's video and tour pickers show the poster too.
|
|
110
|
+
- 860afab: A section, island or component can import a package's stylesheet directly. `import "mapbox-gl/dist/mapbox-gl.css"` or `import "swiper/css"` no longer fails `concrete check`'s workspace and fixtures stages, or `concrete dev`, with `Unknown file extension ".css"` at every module that reaches it. The stylesheet still reaches `template_css` through the CSS import graph, so an `@import` of the package stylesheet in a local `.css` file is no longer needed to work around it.
|
|
111
|
+
- 98ba0ba: The playground's Download button now reads just _Download_, matching the application's editor. The _Format_ switcher now appears only when a template declares more than one format. The _Trim & bleed guides_ toggle appears only when the format on view declares a bleed; for a format without one, the trim line is the page edge, so the toggle had nothing to show. `concrete dev`'s fixture picker now lists only your authored fixtures as a single list, with no automatic _Empties_ group. To preview an empty slot, author a fixture that leaves it empty. `concrete check` still tests every slot's empty state.
|
|
112
|
+
- 79346de: The `concrete dev` playground sidebar now matches the application's editor panel more closely. The template name no longer sits at the top of the sidebar, and the section header and slot cards use the application's spacing instead of an extra 12px of padding and a white inner box. The fixture picker, format switcher and trim and bleed guides are unchanged. The playground's browser tab now reads `<template name> - Playground`, and a live page's tab reads `<template name> - <page label>`.
|
|
113
|
+
- 12e2d57: `concrete build` no longer ships image sizes that an image or document template can never display. A rendered template's pages always render in a window exactly as wide as the format, such as 1920px for `deck16x9`. So an `<Image>` never picks a size narrower than that window, unless it is the image's largest size. Those narrower sizes are no longer derived or listed in the image's `srcset`, for a section's imported images and for fixture media alike. Every size a page can pick still ships, in every format, together with the full-resolution original, so pages, captures, downloads and the playground look the same. A website template keeps the full set of sizes. For a deck template with about sixty photos, `.concrete/build/assets/` shrinks by more than half, and builds run fewer image encodes.
|
|
114
|
+
|
|
3
115
|
## 1.0.2
|
|
4
116
|
|
|
5
117
|
### Patch Changes
|
package/codemods/manifest.json
CHANGED
|
@@ -12,6 +12,18 @@
|
|
|
12
12
|
"1.0.2": {
|
|
13
13
|
"codemods": [],
|
|
14
14
|
"manual": []
|
|
15
|
+
},
|
|
16
|
+
"1.0.3": {
|
|
17
|
+
"codemods": [],
|
|
18
|
+
"manual": []
|
|
19
|
+
},
|
|
20
|
+
"2.0.0": {
|
|
21
|
+
"codemods": [
|
|
22
|
+
"unlayer-author-css"
|
|
23
|
+
],
|
|
24
|
+
"manual": [
|
|
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
|
+
]
|
|
15
27
|
}
|
|
16
28
|
}
|
|
17
29
|
}
|
|
@@ -0,0 +1,194 @@
|
|
|
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
|
+
}
|