pi-pocket 0.26.0 → 0.27.0-beta.2
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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ant Design themes (stream T): a `ThemeConfig`, `{ token: { colorPrimary, colorBgContainer, fontSize, borderRadius, … },
|
|
3
|
+
* components: { … }, algorithm: theme.darkAlgorithm }`, in a file of its own (`light.ts`, `dark.ts`), as a `theme` prop of
|
|
4
|
+
* `ConfigProvider`, or in one object with the algorithm switched at run time. The seed tokens are read as variables named
|
|
5
|
+
* for what they are (`colorBgLayout` is the page, `colorBgContainer` a surface, `colorText`, `colorTextSecondary`,
|
|
6
|
+
* `colorBorder`, `colorPrimary` the accent, `colorError` the danger), numbers are pixels, and an object whose algorithm is
|
|
7
|
+
* the dark one (or whose name or file says dark) holds the dark mode. The tokens of one component are left out: they are
|
|
8
|
+
* that component's own, not the design's.
|
|
9
|
+
*/
|
|
10
|
+
import { readLiteral, type Literal, type Scope } from "./scan-parse.ts";
|
|
11
|
+
import { addVar, type Category, type Facts } from "./scan-facts.ts";
|
|
12
|
+
import { modeOf } from "./scan-facts-lists.ts";
|
|
13
|
+
import { closeOf, openAround, topLevelValue } from "./scan-braces.ts";
|
|
14
|
+
|
|
15
|
+
/** How a seed token is read: the name of the variable, and what it is. */
|
|
16
|
+
function seed(
|
|
17
|
+
key: string,
|
|
18
|
+
value: Literal,
|
|
19
|
+
): { name: string; value: string; hint: Category } | undefined {
|
|
20
|
+
const kebab = key
|
|
21
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
22
|
+
.replace(/([A-Z])([A-Z][a-z])/g, "$1-$2")
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/^motion-(?=duration|ease)/, "");
|
|
25
|
+
const number =
|
|
26
|
+
typeof value === "number"
|
|
27
|
+
? value
|
|
28
|
+
: /^-?\d+(?:\.\d+)?$/.test(String(value))
|
|
29
|
+
? Number(value)
|
|
30
|
+
: undefined;
|
|
31
|
+
|
|
32
|
+
if (typeof value !== "string" && number === undefined) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (/^color/.test(key)) {
|
|
37
|
+
return typeof value === "string" && /^(?:#|rgba?\(|hsla?\(|oklch\(|[a-z]+$)/i.test(value)
|
|
38
|
+
? { name: kebab, value, hint: "color" }
|
|
39
|
+
: undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// `fontFamily` as a string is read with the other theme scalars; the code family is a name of its own.
|
|
43
|
+
if (key === "fontFamilyCode") {
|
|
44
|
+
return typeof value === "string" ? { name: kebab, value, hint: "font" } : undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (/^fontSize/.test(key) && number !== undefined) {
|
|
48
|
+
return { name: kebab, value: `${number}px`, hint: "text" };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// `borderRadius` itself is read with the other theme scalars; its sizes (`borderRadiusSM`) are read here.
|
|
52
|
+
if (/^borderRadius./.test(key) && number !== undefined) {
|
|
53
|
+
return { name: kebab, value: `${number}px`, hint: "radius" };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (key === "lineHeight" && number !== undefined) {
|
|
57
|
+
return { name: kebab, value: String(number), hint: "leading" };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (/^(?:boxShadow)/.test(key) && typeof value === "string") {
|
|
61
|
+
return { name: kebab, value, hint: "shadow" };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (/^motionDuration/.test(key) && typeof value === "string") {
|
|
65
|
+
return { name: kebab, value, hint: "duration" };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (/^motionEase/.test(key) && typeof value === "string") {
|
|
69
|
+
return { name: kebab, value, hint: "ease" };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Whether a file has an Ant Design theme: it uses Ant Design and sets `token`. */
|
|
76
|
+
export const hasAntdTheme = (text: string) =>
|
|
77
|
+
USES_ANTD.test(text) && /(?<![\w$.])["']?token["']?\s*:\s*\{/.test(text);
|
|
78
|
+
|
|
79
|
+
/** Whether a file uses Ant Design: an import from it, a `ThemeConfig`, or one of its algorithms. */
|
|
80
|
+
const USES_ANTD =
|
|
81
|
+
/from\s+["']antd["']|ThemeConfig|ConfigProvider|@ant-design|\b(?:dark|default|compact)Algorithm\b|["']color(?:BgContainer|BgLayout|BgElevated)["']\s*:/;
|
|
82
|
+
|
|
83
|
+
/** The spans of `components: { … }` in a file that uses Ant Design: what is in them is one component's own. */
|
|
84
|
+
export function antdComponentSpans(text: string): { at: number; end: number }[] {
|
|
85
|
+
if (!USES_ANTD.test(text)) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return [...text.matchAll(/(?<![\w$.])["']?components["']?\s*:\s*(?=\{)/g)]
|
|
90
|
+
.slice(0, 6)
|
|
91
|
+
.flatMap((match) => {
|
|
92
|
+
const end = closeOf(text, match.index + match[0].length);
|
|
93
|
+
|
|
94
|
+
return end === undefined ? [] : [{ at: match.index, end }];
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Reads the Ant Design theme objects of a file into `facts`. `scopeAt` says which mode the name of an object stands for. */
|
|
99
|
+
export function antdTheme(
|
|
100
|
+
facts: Facts,
|
|
101
|
+
file: string,
|
|
102
|
+
text: string,
|
|
103
|
+
env: ReadonlyMap<string, Literal>,
|
|
104
|
+
scopeAt: (index: number) => Scope,
|
|
105
|
+
): void {
|
|
106
|
+
if (!USES_ANTD.test(text)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let seen = 0;
|
|
111
|
+
|
|
112
|
+
for (const match of text.matchAll(/(?<![\w$.])["']?token["']?\s*:\s*(?=\{)/g)) {
|
|
113
|
+
if (++seen > 12) {
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const literal = readLiteral(text, match.index + match[0].length, env);
|
|
118
|
+
|
|
119
|
+
if (typeof literal !== "object" || literal === null || Array.isArray(literal)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The object around `token`: its algorithm decides the mode (`darkAlgorithm` is dark; a choice between the two is both).
|
|
124
|
+
const open = openAround(text, match.index);
|
|
125
|
+
const end = open === undefined ? undefined : closeOf(text, open);
|
|
126
|
+
const algorithm =
|
|
127
|
+
open === undefined || end === undefined
|
|
128
|
+
? undefined
|
|
129
|
+
: topLevelValue(text, open, end, "algorithm");
|
|
130
|
+
const dark =
|
|
131
|
+
algorithm !== undefined &&
|
|
132
|
+
/darkAlgorithm/.test(algorithm) &&
|
|
133
|
+
!/defaultAlgorithm/.test(algorithm);
|
|
134
|
+
const named = scopeAt(match.index);
|
|
135
|
+
// A file named for the dark mode (`dark.ts`, `theme/dark/index.ts`) holds it, whatever its objects are called.
|
|
136
|
+
const fileMode = file
|
|
137
|
+
.replace(/\.[^./]+$/, "")
|
|
138
|
+
.split("/")
|
|
139
|
+
.some((part) => modeOf(part) === "dark")
|
|
140
|
+
? "dark"
|
|
141
|
+
: undefined;
|
|
142
|
+
const scope: Scope = dark ? "dark" : named !== "default" ? named : (fileMode ?? named);
|
|
143
|
+
|
|
144
|
+
// The tokens of a component are inside `components`, not here; `token` is the seed and the map.
|
|
145
|
+
let unit: number | undefined;
|
|
146
|
+
|
|
147
|
+
for (const [key, value] of Object.entries(literal).slice(0, 220)) {
|
|
148
|
+
if (key === "sizeUnit" && typeof value === "number") {
|
|
149
|
+
unit = value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const found = seed(key, value);
|
|
153
|
+
|
|
154
|
+
if (found !== undefined) {
|
|
155
|
+
addVar(facts, { ...found, scope, kind: "js", file });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// `sizeUnit` is the base of the spacing: the unit and its steps.
|
|
160
|
+
if (unit !== undefined && unit > 0) {
|
|
161
|
+
addVar(facts, {
|
|
162
|
+
name: "spacing",
|
|
163
|
+
value: `${unit}px`,
|
|
164
|
+
scope,
|
|
165
|
+
kind: "js",
|
|
166
|
+
file,
|
|
167
|
+
hint: "space",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/** The files of a project, read into facts (stream T): style files, scripts, and markup, by their extension. */
|
|
2
|
+
import {
|
|
3
|
+
colorsIn,
|
|
4
|
+
indentedToBraces,
|
|
5
|
+
objectStyles,
|
|
6
|
+
pageStyles,
|
|
7
|
+
parseStyles,
|
|
8
|
+
stripComments,
|
|
9
|
+
templateStyles,
|
|
10
|
+
} from "./scan-parse.ts";
|
|
11
|
+
import { hasAntdTheme } from "./scan-facts-antd.ts";
|
|
12
|
+
import { importConfig, importMaterial, importTokens } from "./scan-import.ts";
|
|
13
|
+
import { bump, emptyKinds, type Facts } from "./scan-facts.ts";
|
|
14
|
+
import { colorFact, ingestStyles } from "./scan-facts-use.ts";
|
|
15
|
+
import { modeOf } from "./scan-facts-lists.ts";
|
|
16
|
+
import { themeVars } from "./scan-facts-theme.ts";
|
|
17
|
+
import { wordpressNames, wordpressTheme } from "./scan-facts-wordpress.ts";
|
|
18
|
+
|
|
19
|
+
const CLASS_USE =
|
|
20
|
+
/(?<![\w-])(?:bg|text|border|ring|fill|stroke|outline|from|to|via|divide|decoration|accent|caret|placeholder|shadow)-([a-z][a-z0-9]*(?:-[a-z0-9]+)*)(?:\/\d+)?(?![\w-])/g;
|
|
21
|
+
|
|
22
|
+
const DEFAULT_COLOR_CLASS =
|
|
23
|
+
/(?<![\w-])(bg|text|border|ring|outline|fill|stroke|divide|from|to|via|decoration|accent|caret|placeholder)-((?:slate|gray|zinc|neutral|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(?:50|[1-9]00|950)|white|black)(?:\/\d+)?(?![\w-])/g;
|
|
24
|
+
|
|
25
|
+
const CLASS_KIND: Record<string, "bg" | "text" | "border" | "fill"> = {
|
|
26
|
+
bg: "bg",
|
|
27
|
+
from: "bg",
|
|
28
|
+
to: "bg",
|
|
29
|
+
via: "bg",
|
|
30
|
+
text: "text",
|
|
31
|
+
placeholder: "text",
|
|
32
|
+
decoration: "text",
|
|
33
|
+
caret: "text",
|
|
34
|
+
accent: "text",
|
|
35
|
+
border: "border",
|
|
36
|
+
ring: "border",
|
|
37
|
+
outline: "border",
|
|
38
|
+
divide: "border",
|
|
39
|
+
fill: "fill",
|
|
40
|
+
stroke: "fill",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const TEXT_CLASS = /(?<![\w-])text-(xs|sm|base|lg|xl|[2-9]xl)(?![\w-])/g;
|
|
44
|
+
|
|
45
|
+
const ROUNDED_CLASS = /(?<![\w-])rounded(?:-(none|xs|sm|md|lg|xl|2xl|3xl|full))?(?![\w-])/g;
|
|
46
|
+
|
|
47
|
+
const WEIGHT_CLASS =
|
|
48
|
+
/(?<![\w-])font-(thin|extralight|light|normal|medium|semibold|bold|extrabold|black)(?![\w-])/g;
|
|
49
|
+
|
|
50
|
+
const SPACING_CLASS =
|
|
51
|
+
/(?<![\w-])(?:p|px|py|pt|pr|pb|pl|ps|pe|m|mx|my|mt|mr|mb|ml|ms|me|gap|gap-x|gap-y|space-x|space-y)-(\d+(?:\.\d+)?)(?![\w-])/g;
|
|
52
|
+
|
|
53
|
+
/** The theme-like file names: a JavaScript file with these may hold a design theme. */
|
|
54
|
+
// A script that writes styles: styled-components, Emotion, Stitches, makeStyles, a style prop or an `sx` prop.
|
|
55
|
+
const STYLED =
|
|
56
|
+
/\bstyled\s*[.(]|\bcss\s*`|createGlobalStyle|keyframes\s*`|makeStyles|createUseStyles|\bsx\s*=|style\s*=\s*\{|\bcva\s*\(|\bglobalStyle\b|StyleSheet\.create|\bstyle\s*:\s*\{|\bstyles\s*:\s*\[?\s*`/;
|
|
57
|
+
|
|
58
|
+
// A script that holds a theme whatever its name: `colors: {` / `palette: {` / `themes: {` with a few color values.
|
|
59
|
+
const THEME_CONTENT =
|
|
60
|
+
/\b(?:colors|colours|palette|themes?)\s*:\s*\{[^]{0,2500}?#[0-9a-fA-F]{3,8}\b[^]{0,1500}?#[0-9a-fA-F]{3,8}\b/;
|
|
61
|
+
|
|
62
|
+
const THEMEISH =
|
|
63
|
+
/(?:^|\/)(?:tailwind\.config|[^/]*(?:theme|tokens?|colors?|palette|design|variables|styles?|constants?|config)[^/]*)\.(?:[cm]?[jt]s|tsx?)$|(?:^|\/)(?:theme|themes|tokens|design-tokens|styles|design|constants)\/(?:[^/]+\/)*[^/]+\.[cm]?[jt]sx?$|(?:^|\/)[^/]*(?:tokens|theme|design)[^/]*\/(?:[^/]+\/)*[^/]+\.[cm]?[jt]sx?$/i;
|
|
64
|
+
|
|
65
|
+
/** The line of the file where a block of it starts, or undefined when the block is not a part of the file as it is. */
|
|
66
|
+
function lineOfBlock(raw: string, block: string): number | undefined {
|
|
67
|
+
const at = raw.indexOf(block);
|
|
68
|
+
|
|
69
|
+
return at === -1 ? undefined : 1 + (raw.slice(0, at).match(/\n/g)?.length ?? 0);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Reads one file's text into the facts. `extension` is the lower-case ending with its dot. */
|
|
73
|
+
export function ingestFile(facts: Facts, rel: string, extension: string, raw: string): void {
|
|
74
|
+
const text = stripComments(raw);
|
|
75
|
+
const parts = rel.split("/");
|
|
76
|
+
// `dark.less`, `theme.dark.css`, `variables-night.scss`, `themes/dark/colors.css`.
|
|
77
|
+
const named = modeOf(parts.at(-1)!.replace(/\.[^.]+$/, ""));
|
|
78
|
+
const mode = named ?? (parts.length > 1 ? modeOf(parts.at(-2)!) : undefined);
|
|
79
|
+
|
|
80
|
+
facts.fileMode = mode === "dark" || mode === "light" ? mode : undefined;
|
|
81
|
+
facts.file = rel;
|
|
82
|
+
|
|
83
|
+
// WordPress names its variables `--wp--preset--color--primary`; the scan reads them as `--color-primary`.
|
|
84
|
+
// A stylesheet that is read before theme.json is renamed too: the files come in the order of their rank.
|
|
85
|
+
if (facts.wordpress || /--wp--(?:preset|custom)--/.test(raw)) {
|
|
86
|
+
raw = wordpressNames(raw);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// `@function space($n) { @return $n * 4px; }`: a call `space(3)` is then 12px.
|
|
90
|
+
if (extension === ".scss" || extension === ".sass") {
|
|
91
|
+
for (const match of text.matchAll(
|
|
92
|
+
/@function\s+([\w-]+)\s*\(\s*\$([\w-]+)[^)]*\)\s*\{\s*@return\s+([^;]+);/g,
|
|
93
|
+
)) {
|
|
94
|
+
if (facts.sassFunctions.size < 40) {
|
|
95
|
+
facts.sassFunctions.set(match[1]!, {
|
|
96
|
+
param: match[2]!,
|
|
97
|
+
expression: match[3]!.trim(),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// `$x` and `@x` are uses of a stylesheet variable.
|
|
104
|
+
if (extension === ".scss" || extension === ".sass" || extension === ".less") {
|
|
105
|
+
for (const match of text.matchAll(/[$@]([A-Za-z_][\w-]*)(?!\s*:)/g)) {
|
|
106
|
+
bump(facts.varUse, match[1]!);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// `var(--x)` is a use of a variable, wherever it is written.
|
|
111
|
+
for (const match of text.matchAll(/var\(\s*--([\w-]+)/g)) {
|
|
112
|
+
bump(facts.varUse, match[1]!);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (
|
|
116
|
+
/@import\s+["']tailwindcss|@tailwind\s+base|@theme\b|tailwind\.config/.test(text) ||
|
|
117
|
+
/(?:^|\/)tailwind\.config\./.test(rel)
|
|
118
|
+
) {
|
|
119
|
+
facts.tailwind = true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (/@import\s+["']tailwindcss|@theme\b/.test(text)) {
|
|
123
|
+
facts.tailwindVersion = 4;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
importMaterial(facts.imported, rel, raw);
|
|
127
|
+
|
|
128
|
+
// A Style Dictionary config in JavaScript names the token files by globs.
|
|
129
|
+
if (
|
|
130
|
+
/^\.(?:[cm]?[jt]s)$/.test(extension) &&
|
|
131
|
+
/\bplatforms\b/.test(raw) &&
|
|
132
|
+
/\bsource\b/.test(raw)
|
|
133
|
+
) {
|
|
134
|
+
// The raw text: a glob (`tokens/**/*.json`) holds `/**/`, which the comment stripper takes for a comment.
|
|
135
|
+
importConfig(facts.imported, rel, raw);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (extension === ".json") {
|
|
139
|
+
if (!wordpressTheme(facts, rel, raw)) {
|
|
140
|
+
importTokens(facts.imported, rel, raw);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Ant Design's theme editor exports its `ThemeConfig` as JSON: `{ "token": { "colorPrimary": "#1677ff" } }`.
|
|
144
|
+
if (hasAntdTheme(raw)) {
|
|
145
|
+
themeVars(facts, rel, raw);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const isStyle = /^\.(?:css|scss|sass|less|styl|stylus|pcss|postcss)$/.test(extension);
|
|
152
|
+
const isPage = /^\.(?:html|htm|vue|svelte|astro|mdx|php|erb|hbs|njk)$/.test(extension);
|
|
153
|
+
const isScript = /^\.(?:[cm]?[jt]s|[jt]sx)$/.test(extension);
|
|
154
|
+
|
|
155
|
+
if (isStyle) {
|
|
156
|
+
// Indented Sass and Stylus have no braces: they are turned into some first.
|
|
157
|
+
const source = /^\.(?:sass|styl|stylus)$/.test(extension) ? indentedToBraces(raw) : raw;
|
|
158
|
+
|
|
159
|
+
ingestStyles(facts, rel, parseStyles(source, 1, facts.darkMixins));
|
|
160
|
+
} else if (isPage) {
|
|
161
|
+
const { blocks, inline } = pageStyles(raw);
|
|
162
|
+
|
|
163
|
+
for (const block of blocks) {
|
|
164
|
+
ingestStyles(facts, rel, parseStyles(block, lineOfBlock(raw, block), facts.darkMixins));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
ingestStyles(facts, rel, inline);
|
|
168
|
+
} else if (
|
|
169
|
+
isScript &&
|
|
170
|
+
(THEMEISH.test(rel) ||
|
|
171
|
+
THEME_CONTENT.test(raw) ||
|
|
172
|
+
hasAntdTheme(raw) ||
|
|
173
|
+
!/^\.(?:[cm]?[jt]s)$/.test(extension) ||
|
|
174
|
+
STYLED.test(raw))
|
|
175
|
+
) {
|
|
176
|
+
const blocks = templateStyles(raw);
|
|
177
|
+
|
|
178
|
+
for (const block of blocks) {
|
|
179
|
+
ingestStyles(facts, rel, parseStyles(block, lineOfBlock(raw, block), facts.darkMixins));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (THEMEISH.test(rel) || THEME_CONTENT.test(raw) || hasAntdTheme(raw)) {
|
|
183
|
+
// A theme object holds values, not styles in use: `border: "#2d3640"` there is a token, not a border.
|
|
184
|
+
themeVars(facts, rel, raw);
|
|
185
|
+
} else {
|
|
186
|
+
ingestStyles(facts, rel, objectStyles(raw));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (isPage || isScript) {
|
|
191
|
+
let seen = 0;
|
|
192
|
+
|
|
193
|
+
// Tailwind's default colors by class: `bg-slate-200`, `text-white`, `border-red-500/50`.
|
|
194
|
+
for (const match of text.matchAll(DEFAULT_COLOR_CLASS)) {
|
|
195
|
+
if (++seen > 8000) {
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const kinds = facts.classColors.get(match[2]!) ?? emptyKinds();
|
|
200
|
+
const kind = CLASS_KIND[match[1]!] ?? "other";
|
|
201
|
+
|
|
202
|
+
kinds[kind] += 1;
|
|
203
|
+
facts.classColors.set(match[2]!, kinds);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
seen = 0;
|
|
207
|
+
|
|
208
|
+
// Tailwind's arbitrary values: `bg-[#fff4d6]`, `text-[rgb(0,0,0)]`.
|
|
209
|
+
for (const match of text.matchAll(
|
|
210
|
+
/(?<![\w-])(bg|text|border|fill|stroke|ring|outline|from|to|via)-\[(#[0-9a-fA-F]{3,8}|(?:rgba?|hsla?|oklch|oklab)\([^\])]*\))\]/g,
|
|
211
|
+
)) {
|
|
212
|
+
if (++seen > 2000) {
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const kind =
|
|
217
|
+
(
|
|
218
|
+
{
|
|
219
|
+
bg: "bg",
|
|
220
|
+
text: "text",
|
|
221
|
+
fill: "fill",
|
|
222
|
+
stroke: "fill",
|
|
223
|
+
from: "bg",
|
|
224
|
+
to: "bg",
|
|
225
|
+
via: "bg",
|
|
226
|
+
} as Record<string, "bg" | "text" | "fill" | "border">
|
|
227
|
+
)[match[1]!] ?? "border";
|
|
228
|
+
|
|
229
|
+
for (const color of colorsIn(match[2]!.replace(/_/g, " "))) {
|
|
230
|
+
colorFact(facts.colors, color.hex, color.text)[kind] += 1;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
seen = 0;
|
|
235
|
+
|
|
236
|
+
for (const match of text.matchAll(
|
|
237
|
+
/<(?:body|html)\b[^>]*?\bclass(?:Name)?\s*=\s*["'`{]+([^"'`}]*)/gi,
|
|
238
|
+
)) {
|
|
239
|
+
for (const name of match[1]!.split(/\s+/).slice(0, 20)) {
|
|
240
|
+
if (/^[\w-]{1,40}$/.test(name)) {
|
|
241
|
+
facts.rootClasses.add(name);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
seen = 0;
|
|
247
|
+
|
|
248
|
+
// Every class name used in markup, to weigh the rules that set a color by how often their class is used.
|
|
249
|
+
for (const match of text.matchAll(
|
|
250
|
+
/\bclass(?:Name)?\s*=\s*(?:["'`]|\{\s*["'`])([^"'`}]*)/g,
|
|
251
|
+
)) {
|
|
252
|
+
if (++seen > 4000) {
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
for (const name of match[1]!.split(/\s+/).slice(0, 40)) {
|
|
257
|
+
if (/^[\w-]{1,40}$/.test(name)) {
|
|
258
|
+
bump(facts.classCount, name);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
seen = 0;
|
|
264
|
+
|
|
265
|
+
for (const match of text.matchAll(CLASS_USE)) {
|
|
266
|
+
if (++seen > 8000) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
bump(facts.classUse, match[1]!);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
seen = 0;
|
|
274
|
+
|
|
275
|
+
for (const match of text.matchAll(SPACING_CLASS)) {
|
|
276
|
+
if (++seen > 8000) {
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Tailwind's step is a multiple of 0.25rem (4px).
|
|
281
|
+
bump(facts.classSpacing, Number(match[1]) * 4);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const [pattern, map] of [
|
|
285
|
+
[TEXT_CLASS, facts.classText],
|
|
286
|
+
[ROUNDED_CLASS, facts.classRadius],
|
|
287
|
+
[WEIGHT_CLASS, facts.classWeight],
|
|
288
|
+
] as const) {
|
|
289
|
+
seen = 0;
|
|
290
|
+
|
|
291
|
+
for (const match of text.matchAll(pattern)) {
|
|
292
|
+
if (++seen > 8000) {
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
bump(map, match[1] ?? "default");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|