@astryxdesign/cli 0.6.1-canary.f48d468 → 0.6.2-canary.a4bcbeb
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/api/init/init.doc.mjs +2 -2
- package/api/init/init.type.d.mts +1 -1
- package/api/init/init.type.mjs +1 -1
- package/api/init/run/run.mjs +1 -1
- package/api/theme/build/build.d.mts +45 -17
- package/api/theme/build/build.family.test.mjs +435 -0
- package/api/theme/build/build.mjs +655 -116
- package/api/theme/build/build.packed-old-core.test.mjs +19 -8
- package/api/theme/build/core-interception.d.mts +14 -2
- package/api/theme/build/core-interception.mjs +24 -14
- package/api/theme/build/family.d.mts +15 -0
- package/api/theme/build/family.mjs +442 -0
- package/assets/docs/theme.doc.dense.mjs +12 -0
- package/assets/docs/theme.doc.mjs +37 -0
- package/assets/docs/working-with-ai.doc.mjs +2 -1
- package/clients/cli/commands/build-theme.mjs +63 -5
- package/clients/cli/commands/init.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +28 -10
- package/foundation/agent-docs/agent-doc-state.mjs +1 -1
- package/foundation/agent-docs/agent-docs.d.mts +2 -2
- package/foundation/agent-docs/agent-docs.mjs +8 -6
- package/foundation/agent-docs/agent-docs.test.mjs +14 -0
- package/foundation/discovery/theming-targets.test.mjs +29 -0
- package/package.json +9 -9
|
@@ -47,7 +47,13 @@ const CORE_THEME_ENTRY = path.join(CORE_DIST, 'theme/index.js');
|
|
|
47
47
|
/** The CLI's published file set (packages/cli package.json `files`). */
|
|
48
48
|
const PACKED_DIRS = ['clients', 'api', 'assets', 'authoring', 'foundation'];
|
|
49
49
|
/** Runtime dependencies the packed CLI resolves from its install root. */
|
|
50
|
-
const RUNTIME_DEPS = [
|
|
50
|
+
const RUNTIME_DEPS = [
|
|
51
|
+
'@babel/parser',
|
|
52
|
+
'commander',
|
|
53
|
+
'jiti',
|
|
54
|
+
'jscodeshift',
|
|
55
|
+
'zod',
|
|
56
|
+
];
|
|
51
57
|
|
|
52
58
|
const OLD_CORE_VERSION = '0.5.4';
|
|
53
59
|
const CURRENT_CORE_VERSION = '0.6.0';
|
|
@@ -262,7 +268,7 @@ ${options.tla ? `const surface = await Promise.resolve('#eee');\n` : ''}export c
|
|
|
262
268
|
}
|
|
263
269
|
|
|
264
270
|
/**
|
|
265
|
-
* @param {string} themeFile
|
|
271
|
+
* @param {string | string[]} themeFile
|
|
266
272
|
* @param {string[]} [extraArgs]
|
|
267
273
|
* @returns {{status: number, envelope: any}}
|
|
268
274
|
*/
|
|
@@ -272,11 +278,8 @@ function buildTheme(themeFile, extraArgs = []) {
|
|
|
272
278
|
'node_modules/@astryxdesign/cli/clients/cli/bin/astryx.mjs',
|
|
273
279
|
);
|
|
274
280
|
try {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
[bin, 'theme', 'build', themeFile, ...extraArgs, '--json'],
|
|
278
|
-
{cwd: consumer, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']},
|
|
279
|
-
);
|
|
281
|
+
// prettier-ignore
|
|
282
|
+
const stdout = execFileSync(process.execPath, [bin, 'theme', 'build', ...(Array.isArray(themeFile) ? themeFile : [themeFile]), ...extraArgs, '--json'], {cwd: consumer, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']});
|
|
280
283
|
return {status: 0, envelope: JSON.parse(stdout)};
|
|
281
284
|
} catch (error) {
|
|
282
285
|
const e = /** @type {any} */ (error);
|
|
@@ -432,7 +435,9 @@ beforeAll(() => {
|
|
|
432
435
|
for (const dep of RUNTIME_DEPS) {
|
|
433
436
|
const installed = findInstalledPackage(CLI_PKG, dep);
|
|
434
437
|
if (!installed) throw new Error(`${dep} is not installed under ${CLI_PKG}`);
|
|
435
|
-
|
|
438
|
+
const target = path.join(modules, dep);
|
|
439
|
+
fs.mkdirSync(path.dirname(target), {recursive: true});
|
|
440
|
+
fs.symlinkSync(fs.realpathSync(installed), target);
|
|
436
441
|
}
|
|
437
442
|
|
|
438
443
|
// The installed core. Real built implementations behind a swappable
|
|
@@ -1088,6 +1093,12 @@ afterAll(() => {
|
|
|
1088
1093
|
describe('packed CLI against a core that predates adaptations', () => {
|
|
1089
1094
|
beforeAll(() => useCore('old'));
|
|
1090
1095
|
|
|
1096
|
+
it('rejects a family before an old core can erase adaptations', () => {
|
|
1097
|
+
// prettier-ignore
|
|
1098
|
+
const result = buildTheme(['adaptive-base.ts', 'extending.ts'], ['--family', '--family-key', 'adaptive-family']);
|
|
1099
|
+
expect(result.envelope.code).toBe('ERR_CORE_INCOMPATIBLE');
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1091
1102
|
it('builds a non-adaptation theme', () => {
|
|
1092
1103
|
const {status, envelope} = buildTheme('plain.ts');
|
|
1093
1104
|
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* silently delete every one of those exports from a theme file that imports a
|
|
13
13
|
* component. Every real export is preserved on each; one function is wrapped.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* Current-Core family builds also use this private recorder for exact parents;
|
|
16
|
+
* standalone builds with current Core do not.
|
|
17
17
|
*
|
|
18
18
|
* @param {any} coreThemeModule - The installed `@astryxdesign/core/theme` namespace.
|
|
19
19
|
* @param {any} [coreRootModule] - The installed `@astryxdesign/core` namespace.
|
|
@@ -44,6 +44,18 @@ export type CoreInterception = {
|
|
|
44
44
|
* - `virtualModules` for jiti.
|
|
45
45
|
*/
|
|
46
46
|
modules: Record<string, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* - Whether authored input was captured.
|
|
49
|
+
*/
|
|
50
|
+
observed: (theme: any) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* - Retain capture before stripping.
|
|
53
|
+
*/
|
|
54
|
+
retain: (theme: any) => void;
|
|
55
|
+
/**
|
|
56
|
+
* - Exact authored `extends` value.
|
|
57
|
+
*/
|
|
58
|
+
parentOf: (theme: any) => any;
|
|
47
59
|
/**
|
|
48
60
|
* - Every raw input that fed a
|
|
49
61
|
* theme, following its own `extends` chain and spread provenance.
|
|
@@ -79,6 +79,8 @@ import {findInstalledPackage} from '../../../foundation/fs/paths.mjs';
|
|
|
79
79
|
*/
|
|
80
80
|
export const THEME_LINEAGE = Symbol.for('astryx.theme.lineage');
|
|
81
81
|
|
|
82
|
+
/** Complete private capture retained for cached exports and repeated checks. */
|
|
83
|
+
const retainedLineageByReference = new WeakMap();
|
|
82
84
|
/**
|
|
83
85
|
* @typedef {object} ThemeLineage
|
|
84
86
|
* @property {any} input - The raw `defineTheme()` argument.
|
|
@@ -90,6 +92,9 @@ export const THEME_LINEAGE = Symbol.for('astryx.theme.lineage');
|
|
|
90
92
|
*
|
|
91
93
|
* @typedef {object} CoreInterception
|
|
92
94
|
* @property {Record<string, unknown>} modules - `virtualModules` for jiti.
|
|
95
|
+
* @property {(theme: any) => boolean} observed - Whether authored input was captured.
|
|
96
|
+
* @property {(theme: any) => void} retain - Retain capture before stripping.
|
|
97
|
+
* @property {(theme: any) => any} parentOf - Exact authored `extends` value.
|
|
93
98
|
* @property {(theme: any) => any[]} lineageOf - Every raw input that fed a
|
|
94
99
|
* theme, following its own `extends` chain and spread provenance.
|
|
95
100
|
* @property {(theme: any) => any[]} unobservedIn - Lineage members this
|
|
@@ -105,17 +110,16 @@ export const THEME_LINEAGE = Symbol.for('astryx.theme.lineage');
|
|
|
105
110
|
*/
|
|
106
111
|
|
|
107
112
|
/**
|
|
108
|
-
* Was this theme produced by a `defineTheme()` call the recorder saw?
|
|
109
|
-
*
|
|
113
|
+
* Was this theme produced by a `defineTheme()` call the recorder saw? The
|
|
114
|
+
* temporary symbol must be read before `strip`; retained WeakMap capture remains
|
|
115
|
+
* available for cached retries.
|
|
110
116
|
*
|
|
111
117
|
* @param {any} value
|
|
112
118
|
* @param {WeakMap<object, ThemeLineage>} byReference
|
|
113
119
|
* @returns {boolean}
|
|
114
120
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return byReference.has(value) || value[THEME_LINEAGE] !== undefined;
|
|
118
|
-
}
|
|
121
|
+
// prettier-ignore
|
|
122
|
+
function wasObserved(value, byReference) { return Boolean(value && typeof value === 'object' && (byReference.has(value) || retainedLineageByReference.has(value) || value[THEME_LINEAGE] !== undefined)); }
|
|
119
123
|
|
|
120
124
|
const GENERATIVE_AXIS_KEYS = ['typography', 'color', 'radius', 'motion'];
|
|
121
125
|
|
|
@@ -228,8 +232,8 @@ function mergeGenerativeAxes(inherited, own) {
|
|
|
228
232
|
* silently delete every one of those exports from a theme file that imports a
|
|
229
233
|
* component. Every real export is preserved on each; one function is wrapped.
|
|
230
234
|
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
235
|
+
* Current-Core family builds also use this private recorder for exact parents;
|
|
236
|
+
* standalone builds with current Core do not.
|
|
233
237
|
*
|
|
234
238
|
* @param {any} coreThemeModule - The installed `@astryxdesign/core/theme` namespace.
|
|
235
239
|
* @param {any} [coreRootModule] - The installed `@astryxdesign/core` namespace.
|
|
@@ -243,6 +247,7 @@ export function interceptCore(coreThemeModule, coreRootModule) {
|
|
|
243
247
|
const mark = (value, lineage) => {
|
|
244
248
|
if (!value || typeof value !== 'object') return;
|
|
245
249
|
byReference.set(value, lineage);
|
|
250
|
+
retainedLineageByReference.set(value, lineage);
|
|
246
251
|
try {
|
|
247
252
|
// Enumerable so a spread of this theme carries it; configurable so it
|
|
248
253
|
// can be removed again before the theme is used for output.
|
|
@@ -258,10 +263,8 @@ export function interceptCore(coreThemeModule, coreRootModule) {
|
|
|
258
263
|
};
|
|
259
264
|
|
|
260
265
|
/** @param {any} value @returns {ThemeLineage | undefined} */
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return byReference.get(value) ?? value[THEME_LINEAGE];
|
|
264
|
-
};
|
|
266
|
+
// prettier-ignore
|
|
267
|
+
const lineageFor = value => value && typeof value === 'object' ? byReference.get(value) ?? retainedLineageByReference.get(value) ?? value[THEME_LINEAGE] : undefined;
|
|
265
268
|
|
|
266
269
|
/**
|
|
267
270
|
* One namespace, spread so exports this file does not know about still
|
|
@@ -293,6 +296,12 @@ export function interceptCore(coreThemeModule, coreRootModule) {
|
|
|
293
296
|
'@astryxdesign/core': wrapNamespace(coreRootModule ?? coreThemeModule),
|
|
294
297
|
},
|
|
295
298
|
|
|
299
|
+
// prettier-ignore
|
|
300
|
+
observed(theme) { return wasObserved(theme, byReference); },
|
|
301
|
+
// prettier-ignore
|
|
302
|
+
retain(theme) { const lineage = lineageFor(theme); if (lineage && theme && typeof theme === 'object') retainedLineageByReference.set(theme, lineage); },
|
|
303
|
+
// prettier-ignore
|
|
304
|
+
parentOf(theme) { const input = lineageFor(theme)?.input; return input && typeof input === 'object' && 'extends' in input ? input.extends : theme && typeof theme === 'object' ? theme.extends : undefined; },
|
|
296
305
|
lineageOf(theme) {
|
|
297
306
|
/** @type {any[]} */
|
|
298
307
|
const inputs = [];
|
|
@@ -327,8 +336,8 @@ export function interceptCore(coreThemeModule, coreRootModule) {
|
|
|
327
336
|
unobservedIn(theme) {
|
|
328
337
|
// Lineage members this recorder never saw AND that carry no adaptation
|
|
329
338
|
// metadata of their own. On a degraded load these are exactly the
|
|
330
|
-
// themes whose adaptations, if any, were erased unobserved.
|
|
331
|
-
//
|
|
339
|
+
// themes whose adaptations, if any, were erased unobserved. Retained
|
|
340
|
+
// WeakMap capture keeps this answer stable after a cached retry.
|
|
332
341
|
//
|
|
333
342
|
// A RAW INPUT reached through an observed result is itself evidence —
|
|
334
343
|
// it is the very object the recorder captured, and only the result
|
|
@@ -404,6 +413,7 @@ export function interceptCore(coreThemeModule, coreRootModule) {
|
|
|
404
413
|
if (!value || typeof value !== 'object' || seen.has(value)) continue;
|
|
405
414
|
seen.add(value);
|
|
406
415
|
const lineage = lineageFor(value);
|
|
416
|
+
if (lineage) retainedLineageByReference.set(value, lineage);
|
|
407
417
|
if (Object.hasOwn(value, THEME_LINEAGE)) {
|
|
408
418
|
try {
|
|
409
419
|
delete value[THEME_LINEAGE];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {Array<{theme: any, sourceTheme: any, sourceParent: any, filePath: string}>} members
|
|
6
|
+
* @returns {any[]}
|
|
7
|
+
*/
|
|
8
|
+
export function resolveThemeFamily(members: Array<{
|
|
9
|
+
theme: any;
|
|
10
|
+
sourceTheme: any;
|
|
11
|
+
sourceParent: any;
|
|
12
|
+
filePath: string;
|
|
13
|
+
}>): any[];
|
|
14
|
+
/** @param {any[]} members */
|
|
15
|
+
export function generateFamilyCSS(members: any[]): string;
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
/**
|
|
3
|
+
* @file Thin family packaging over existing compiler output.
|
|
4
|
+
*/
|
|
5
|
+
const THEME_SCOPE_TO = '[data-astryx-theme]';
|
|
6
|
+
/** @param {string} a @param {string} b */
|
|
7
|
+
const compareText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
8
|
+
/**
|
|
9
|
+
* @param {Array<{theme: any, sourceTheme: any, sourceParent: any, filePath: string}>} members
|
|
10
|
+
* @returns {any[]}
|
|
11
|
+
*/
|
|
12
|
+
export function resolveThemeFamily(members) {
|
|
13
|
+
if (members.length < 2)
|
|
14
|
+
throw new Error(
|
|
15
|
+
'A theme family requires at least two selected theme files.',
|
|
16
|
+
);
|
|
17
|
+
const byName = new Map();
|
|
18
|
+
const sources = new Set();
|
|
19
|
+
for (const member of members) {
|
|
20
|
+
if (sources.has(member.filePath)) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Duplicate family source: ${member.filePath}. Select each theme file once.`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (byName.has(member.theme.name)) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Duplicate theme identity "${member.theme.name}" in the selected family.`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
sources.add(member.filePath);
|
|
31
|
+
byName.set(member.theme.name, member);
|
|
32
|
+
}
|
|
33
|
+
const withParents = members.map(member => {
|
|
34
|
+
const parent = member.sourceParent;
|
|
35
|
+
if (
|
|
36
|
+
parent !== undefined &&
|
|
37
|
+
(!parent ||
|
|
38
|
+
typeof parent !== 'object' ||
|
|
39
|
+
typeof parent.name !== 'string' ||
|
|
40
|
+
parent.name.length === 0)
|
|
41
|
+
) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Theme "${member.theme.name}" has an invalid normalized parent.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const parentName = parent?.name ?? null;
|
|
47
|
+
if (parentName && !byName.has(parentName)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Theme "${member.theme.name}" is missing selected ancestor "${parentName}".`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (parentName && parent !== byName.get(parentName).sourceTheme) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Theme "${member.theme.name}" does not extend the selected "${parentName}" source.`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return {...member, parentName};
|
|
58
|
+
});
|
|
59
|
+
const resolved = new Map(
|
|
60
|
+
withParents.map(member => [member.theme.name, member]),
|
|
61
|
+
);
|
|
62
|
+
const marks = new Map();
|
|
63
|
+
/** @param {any} member */
|
|
64
|
+
const visit = member => {
|
|
65
|
+
const mark = marks.get(member.theme.name) ?? 0;
|
|
66
|
+
if (mark === 1)
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Cycle detected in selected theme family at "${member.theme.name}".`,
|
|
69
|
+
);
|
|
70
|
+
if (mark === 2) return;
|
|
71
|
+
marks.set(member.theme.name, 1);
|
|
72
|
+
if (member.parentName) visit(resolved.get(member.parentName));
|
|
73
|
+
marks.set(member.theme.name, 2);
|
|
74
|
+
};
|
|
75
|
+
for (const member of withParents) visit(member);
|
|
76
|
+
const roots = withParents.filter(member => member.parentName === null);
|
|
77
|
+
if (roots.length !== 1) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`A theme family must have exactly one selected root; found ${roots.length}.`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const emitted = new Set();
|
|
83
|
+
/** @type {any[]} */
|
|
84
|
+
const ordered = [];
|
|
85
|
+
while (ordered.length < withParents.length) {
|
|
86
|
+
const eligible = withParents
|
|
87
|
+
.filter(
|
|
88
|
+
member =>
|
|
89
|
+
!emitted.has(member.theme.name) &&
|
|
90
|
+
(!member.parentName || emitted.has(member.parentName)),
|
|
91
|
+
)
|
|
92
|
+
.sort((a, b) =>
|
|
93
|
+
compareText(
|
|
94
|
+
`${a.theme.name}\0${a.filePath}`,
|
|
95
|
+
`${b.theme.name}\0${b.filePath}`,
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
if (eligible.length === 0)
|
|
99
|
+
throw new Error('Cycle detected in selected theme family.');
|
|
100
|
+
emitted.add(eligible[0].theme.name);
|
|
101
|
+
ordered.push(eligible[0]);
|
|
102
|
+
}
|
|
103
|
+
return ordered;
|
|
104
|
+
}
|
|
105
|
+
/** @param {string} name */
|
|
106
|
+
const themeSelector = name => `[data-astryx-theme=${JSON.stringify(name)}]`;
|
|
107
|
+
/** @param {string[]} names */
|
|
108
|
+
const familyScopeSelector = names =>
|
|
109
|
+
`:where(${names.map(themeSelector).join(', ')})`;
|
|
110
|
+
/** @param {string} rule @returns {{open: number, close: number}} */
|
|
111
|
+
function ruleBounds(rule) {
|
|
112
|
+
let open = -1;
|
|
113
|
+
let depth = 0;
|
|
114
|
+
let paren = 0;
|
|
115
|
+
let urlDepth = 0;
|
|
116
|
+
let quote = '';
|
|
117
|
+
let escaped = false;
|
|
118
|
+
let comment = false;
|
|
119
|
+
for (let index = 0; index < rule.length; index++) {
|
|
120
|
+
const char = rule[index];
|
|
121
|
+
if (comment) {
|
|
122
|
+
if (char === '*' && rule[index + 1] === '/') {
|
|
123
|
+
comment = false;
|
|
124
|
+
index++;
|
|
125
|
+
}
|
|
126
|
+
} else if (escaped) {
|
|
127
|
+
escaped = false;
|
|
128
|
+
} else if (char === '\\') {
|
|
129
|
+
escaped = true;
|
|
130
|
+
} else if (quote) {
|
|
131
|
+
if (char === quote) quote = '';
|
|
132
|
+
} else if (char === '"' || char === "'") {
|
|
133
|
+
quote = char;
|
|
134
|
+
} else if (urlDepth === 0 && char === '/' && rule[index + 1] === '*') {
|
|
135
|
+
comment = true;
|
|
136
|
+
index++;
|
|
137
|
+
} else if (char === '(') {
|
|
138
|
+
paren++;
|
|
139
|
+
if (rule.slice(Math.max(0, index - 3), index).toLowerCase() === 'url') {
|
|
140
|
+
urlDepth = paren;
|
|
141
|
+
}
|
|
142
|
+
} else if (char === ')' && paren > 0) {
|
|
143
|
+
if (urlDepth === paren) urlDepth = 0;
|
|
144
|
+
paren--;
|
|
145
|
+
} else if (char === '{' && paren === 0) {
|
|
146
|
+
if (open === -1) open = index;
|
|
147
|
+
depth++;
|
|
148
|
+
} else if (char === '}' && paren === 0 && open !== -1 && --depth === 0) {
|
|
149
|
+
return {open, close: index};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
throw new Error('Theme compiler emitted a malformed rule.');
|
|
153
|
+
}
|
|
154
|
+
/** @param {string} rule */
|
|
155
|
+
function declarationEntries(rule) {
|
|
156
|
+
const {open, close} = ruleBounds(rule);
|
|
157
|
+
const selector = rule.slice(0, open).trim();
|
|
158
|
+
const body = rule.slice(open + 1, close);
|
|
159
|
+
/** @type {string[]} */
|
|
160
|
+
const declarations = [];
|
|
161
|
+
let start = 0;
|
|
162
|
+
let quote = '';
|
|
163
|
+
let escaped = false;
|
|
164
|
+
let comment = false;
|
|
165
|
+
let paren = 0;
|
|
166
|
+
let urlDepth = 0;
|
|
167
|
+
let braces = 0;
|
|
168
|
+
let brackets = 0;
|
|
169
|
+
for (let index = 0; index < body.length; index++) {
|
|
170
|
+
const char = body[index];
|
|
171
|
+
if (comment) {
|
|
172
|
+
if (char === '*' && body[index + 1] === '/') {
|
|
173
|
+
comment = false;
|
|
174
|
+
index++;
|
|
175
|
+
}
|
|
176
|
+
} else if (escaped) {
|
|
177
|
+
escaped = false;
|
|
178
|
+
} else if (char === '\\') {
|
|
179
|
+
escaped = true;
|
|
180
|
+
} else if (quote) {
|
|
181
|
+
if (char === quote) quote = '';
|
|
182
|
+
} else if (char === '"' || char === "'") {
|
|
183
|
+
quote = char;
|
|
184
|
+
} else if (urlDepth === 0 && char === '/' && body[index + 1] === '*') {
|
|
185
|
+
comment = true;
|
|
186
|
+
index++;
|
|
187
|
+
} else if (char === '(') {
|
|
188
|
+
paren++;
|
|
189
|
+
if (body.slice(Math.max(0, index - 3), index).toLowerCase() === 'url') {
|
|
190
|
+
urlDepth = paren;
|
|
191
|
+
}
|
|
192
|
+
} else if (char === ')' && paren > 0) {
|
|
193
|
+
if (urlDepth === paren) urlDepth = 0;
|
|
194
|
+
paren--;
|
|
195
|
+
} else if (char === '[' && urlDepth === 0) {
|
|
196
|
+
brackets++;
|
|
197
|
+
} else if (char === ']' && urlDepth === 0 && brackets > 0) {
|
|
198
|
+
brackets--;
|
|
199
|
+
} else if (char === '{' && paren === 0) {
|
|
200
|
+
braces++;
|
|
201
|
+
} else if (char === '}' && paren === 0 && braces > 0) {
|
|
202
|
+
braces--;
|
|
203
|
+
} else if (char === ';' && paren === 0 && braces === 0 && brackets === 0) {
|
|
204
|
+
const declaration = body.slice(start, index + 1).trim();
|
|
205
|
+
if (declaration) declarations.push(declaration);
|
|
206
|
+
start = index + 1;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (body.slice(start).trim())
|
|
210
|
+
throw new Error('Theme compiler emitted an unterminated declaration.');
|
|
211
|
+
const counts = new Map();
|
|
212
|
+
return declarations
|
|
213
|
+
.map(declaration => {
|
|
214
|
+
const property = declaration.slice(0, declaration.indexOf(':')).trim();
|
|
215
|
+
const occurrence = counts.get(property) ?? 0;
|
|
216
|
+
counts.set(property, occurrence + 1);
|
|
217
|
+
return {key: `${property}\0${occurrence}`, declaration};
|
|
218
|
+
})
|
|
219
|
+
.map(entry => ({...entry, selector}));
|
|
220
|
+
}
|
|
221
|
+
/** @param {string[]} rules */
|
|
222
|
+
function ruleEntries(rules) {
|
|
223
|
+
const counts = new Map();
|
|
224
|
+
return rules.map(rule => {
|
|
225
|
+
const {open} = ruleBounds(rule);
|
|
226
|
+
const selector = rule.slice(0, open).trim();
|
|
227
|
+
const occurrence = counts.get(selector) ?? 0;
|
|
228
|
+
counts.set(selector, occurrence + 1);
|
|
229
|
+
return {key: `${selector}\0${occurrence}`, rule, selector};
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/** @param {Array<Array<{key: string}>>} sequences @param {string} _description @returns {string[] | null} */
|
|
233
|
+
function orderedKeys(sequences, _description) {
|
|
234
|
+
const rank = new Map();
|
|
235
|
+
const edges = new Map();
|
|
236
|
+
const indegree = new Map();
|
|
237
|
+
let nextRank = 0;
|
|
238
|
+
for (const sequence of sequences) {
|
|
239
|
+
for (const entry of sequence) {
|
|
240
|
+
if (!rank.has(entry.key)) rank.set(entry.key, nextRank++);
|
|
241
|
+
edges.set(entry.key, edges.get(entry.key) ?? new Set());
|
|
242
|
+
indegree.set(entry.key, indegree.get(entry.key) ?? 0);
|
|
243
|
+
}
|
|
244
|
+
for (let index = 1; index < sequence.length; index++) {
|
|
245
|
+
const before = sequence[index - 1].key;
|
|
246
|
+
const after = sequence[index].key;
|
|
247
|
+
if (!edges.get(before).has(after)) {
|
|
248
|
+
edges.get(before).add(after);
|
|
249
|
+
indegree.set(after, indegree.get(after) + 1);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const ready = [...rank.keys()].filter(key => indegree.get(key) === 0);
|
|
254
|
+
/** @type {string[]} */
|
|
255
|
+
const ordered = [];
|
|
256
|
+
while (ready.length > 0) {
|
|
257
|
+
ready.sort((a, b) => rank.get(a) - rank.get(b) || compareText(a, b));
|
|
258
|
+
const key = ready.shift();
|
|
259
|
+
ordered.push(key);
|
|
260
|
+
for (const after of edges.get(key)) {
|
|
261
|
+
indegree.set(after, indegree.get(after) - 1);
|
|
262
|
+
if (indegree.get(after) === 0) ready.push(after);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (ordered.length !== rank.size) return null;
|
|
266
|
+
return ordered;
|
|
267
|
+
}
|
|
268
|
+
/** @param {Array<{names: string[], selector: string, declaration: string}>} items */
|
|
269
|
+
function renderDeclarations(items) {
|
|
270
|
+
/** @type {Array<{scope: string, names: string[], selector: string, declarations: string[]}>} */
|
|
271
|
+
const blocks = [];
|
|
272
|
+
/** @type {{scope: string, names: string[], selector: string, declarations: string[]} | undefined} */
|
|
273
|
+
let current;
|
|
274
|
+
for (const item of items) {
|
|
275
|
+
const scope = `${item.names.join('\0')}\0${item.selector}`;
|
|
276
|
+
if (!current || current.scope !== scope) {
|
|
277
|
+
current = {
|
|
278
|
+
scope,
|
|
279
|
+
names: item.names,
|
|
280
|
+
selector: item.selector,
|
|
281
|
+
declarations: [],
|
|
282
|
+
};
|
|
283
|
+
blocks.push(current);
|
|
284
|
+
}
|
|
285
|
+
current.declarations.push(item.declaration);
|
|
286
|
+
}
|
|
287
|
+
return blocks.map(block => {
|
|
288
|
+
const declarations = block.declarations
|
|
289
|
+
.map(value => ` ${value}`)
|
|
290
|
+
.join('\n');
|
|
291
|
+
return `@scope (${familyScopeSelector(block.names)}) to (${THEME_SCOPE_TO}) {\n ${block.selector} {\n${declarations}\n }\n}`;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/** @param {any[]} members @param {string} field */
|
|
295
|
+
function completeMemberRules(members, field) {
|
|
296
|
+
return members
|
|
297
|
+
.filter(member => member.css[field].length > 0)
|
|
298
|
+
.map(
|
|
299
|
+
member =>
|
|
300
|
+
`@scope (${familyScopeSelector([member.theme.name])}) to (${THEME_SCOPE_TO}) {\n${member.css[field].join('\n\n')}\n}`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
/** @param {any[]} members @param {string} field */
|
|
304
|
+
function factorRules(members, field) {
|
|
305
|
+
const ruleSequences = members.map(member => ruleEntries(member.css[field]));
|
|
306
|
+
const ruleMaps = ruleSequences.map(
|
|
307
|
+
sequence => new Map(sequence.map(entry => [entry.key, entry])),
|
|
308
|
+
);
|
|
309
|
+
/** @type {Array<{names: string[], selector: string, declaration: string}>} */
|
|
310
|
+
const items = [];
|
|
311
|
+
const ruleOrder = orderedKeys(ruleSequences, 'rule');
|
|
312
|
+
if (!ruleOrder) return completeMemberRules(members, field);
|
|
313
|
+
for (const ruleKey of ruleOrder) {
|
|
314
|
+
const declarations = ruleMaps.map(map => {
|
|
315
|
+
const rule = map.get(ruleKey);
|
|
316
|
+
return rule ? declarationEntries(rule.rule) : [];
|
|
317
|
+
});
|
|
318
|
+
const declarationMaps = declarations.map(
|
|
319
|
+
sequence => new Map(sequence.map(entry => [entry.key, entry])),
|
|
320
|
+
);
|
|
321
|
+
const declarationOrder = orderedKeys(declarations, 'declaration');
|
|
322
|
+
if (!declarationOrder) return completeMemberRules(members, field);
|
|
323
|
+
for (const declarationKey of declarationOrder) {
|
|
324
|
+
/** @type {Map<string, {selector: string, declaration: string, names: string[]}>} */
|
|
325
|
+
const groups = new Map();
|
|
326
|
+
for (let index = 0; index < members.length; index++) {
|
|
327
|
+
const entry = declarationMaps[index].get(declarationKey);
|
|
328
|
+
if (!entry) continue;
|
|
329
|
+
const group = groups.get(entry.declaration) ?? {
|
|
330
|
+
selector: entry.selector,
|
|
331
|
+
declaration: entry.declaration,
|
|
332
|
+
names: [],
|
|
333
|
+
};
|
|
334
|
+
group.names.push(members[index].theme.name);
|
|
335
|
+
groups.set(entry.declaration, group);
|
|
336
|
+
}
|
|
337
|
+
items.push(...groups.values());
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return renderDeclarations(items);
|
|
341
|
+
}
|
|
342
|
+
/** Replace only a generated @scope prelude, never authored CSS payloads. @param {string} css @param {string} search @param {string} replacement */
|
|
343
|
+
// prettier-ignore
|
|
344
|
+
function replaceScopeSyntax(css, search, replacement) {
|
|
345
|
+
let output = '', quote = '', escaped = false, comment = false, lastCode = '';
|
|
346
|
+
for (let index = 0; index < css.length; index++) {
|
|
347
|
+
const char = css[index];
|
|
348
|
+
if (comment) {
|
|
349
|
+
output += char;
|
|
350
|
+
if (char === '*' && css[index + 1] === '/') { output += '/'; comment = false; index++; }
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
if (quote) {
|
|
354
|
+
output += char;
|
|
355
|
+
if (escaped) escaped = false;
|
|
356
|
+
else if (char === '\\') escaped = true;
|
|
357
|
+
else if (char === quote) quote = '';
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (char === '/' && css[index + 1] === '*') { output += '/*'; comment = true; index++; continue; }
|
|
361
|
+
if (char === '"' || char === "'") { output += char; quote = char; continue; }
|
|
362
|
+
if ((lastCode === '' || lastCode === '{' || lastCode === '}') && css.startsWith(search, index)) {
|
|
363
|
+
output += replacement; lastCode = ')'; index += search.length - 1; continue;
|
|
364
|
+
}
|
|
365
|
+
output += char;
|
|
366
|
+
if (!/\s/.test(char)) lastCode = char;
|
|
367
|
+
}
|
|
368
|
+
return output;
|
|
369
|
+
}
|
|
370
|
+
/** @param {string} css @param {string} name */
|
|
371
|
+
const normalizeScope = (css, name) =>
|
|
372
|
+
replaceScopeSyntax(
|
|
373
|
+
css,
|
|
374
|
+
`@scope (${themeSelector(name)})`,
|
|
375
|
+
'@scope (__ASTRYX_FAMILY_SCOPE__)',
|
|
376
|
+
);
|
|
377
|
+
/** @param {string} css @param {string[]} names */
|
|
378
|
+
const applyScope = (css, names) =>
|
|
379
|
+
replaceScopeSyntax(
|
|
380
|
+
css,
|
|
381
|
+
'@scope (__ASTRYX_FAMILY_SCOPE__)',
|
|
382
|
+
`@scope (${familyScopeSelector(names)})`,
|
|
383
|
+
);
|
|
384
|
+
/** @param {any[]} members @param {string} field */
|
|
385
|
+
function factorSections(members, field) {
|
|
386
|
+
/** @type {Map<string, {css: string, names: string[]}>} */
|
|
387
|
+
const groups = new Map();
|
|
388
|
+
for (const member of members) {
|
|
389
|
+
const css = normalizeScope(member.css[field] ?? '', member.theme.name);
|
|
390
|
+
if (!css) continue;
|
|
391
|
+
const group = groups.get(css) ?? {css, names: []};
|
|
392
|
+
group.names.push(member.theme.name);
|
|
393
|
+
groups.set(css, group);
|
|
394
|
+
}
|
|
395
|
+
return [...groups.values()].map(group => applyScope(group.css, group.names));
|
|
396
|
+
}
|
|
397
|
+
/** Keep document rules top-level-active; initialize nested tuple members locally. @param {any[]} members */
|
|
398
|
+
// prettier-ignore
|
|
399
|
+
function colorSchemeSections(members) {
|
|
400
|
+
const groups = new Map();
|
|
401
|
+
for (const member of members) {
|
|
402
|
+
const css = member.css.colorScheme;
|
|
403
|
+
if (!css) continue;
|
|
404
|
+
const names = groups.get(css) ?? []; names.push(member.theme.name); groups.set(css, names);
|
|
405
|
+
}
|
|
406
|
+
return [...groups].reduce((result, [css, names]) => {
|
|
407
|
+
const identities = familyScopeSelector(names), top = `${identities}:not(:where([data-astryx-theme] [data-astryx-theme]))`;
|
|
408
|
+
const active = (/** @type {string} */ selector) => `${selector}${identities},\n${selector}:where(:not([data-astryx-theme])):has(${top})`;
|
|
409
|
+
result.base.push(`@scope (${identities}) to (${THEME_SCOPE_TO}) {\n :where(:scope:not([data-theme]):not(html[data-theme] *)) { color-scheme: light dark; }\n :where(:scope:not([data-theme]):is(html[data-theme="light"] *)) { color-scheme: light; }\n :where(:scope:not([data-theme]):is(html[data-theme="dark"] *)) { color-scheme: dark; }\n}`);
|
|
410
|
+
result.themed.push(css.replace(':root', active(':root')).replace('html[data-theme="light"]', active('html[data-theme="light"]')).replace('html[data-theme="dark"]', active('html[data-theme="dark"]')));
|
|
411
|
+
return result;
|
|
412
|
+
}, /** @type {{base: string[], themed: string[]}} */ ({base: [], themed: []}));
|
|
413
|
+
}
|
|
414
|
+
/** @param {any[]} members */
|
|
415
|
+
export function generateFamilyCSS(members) {
|
|
416
|
+
const root = members[0];
|
|
417
|
+
for (const member of members) {
|
|
418
|
+
if (member.css.base !== root.css.base)
|
|
419
|
+
throw new Error(
|
|
420
|
+
'Theme-independent base CSS changed between family members.',
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
const prose = [
|
|
424
|
+
...factorRules(members, 'prose'),
|
|
425
|
+
...factorSections(members, 'adaptationProse'),
|
|
426
|
+
];
|
|
427
|
+
const schemes = colorSchemeSections(members);
|
|
428
|
+
const themed = [
|
|
429
|
+
...schemes.themed,
|
|
430
|
+
...factorRules(members, 'component'),
|
|
431
|
+
...factorSections(members, 'adaptation'),
|
|
432
|
+
...factorSections(members, 'onMedia'),
|
|
433
|
+
].filter(Boolean);
|
|
434
|
+
const parts = [];
|
|
435
|
+
if (prose.length > 0) parts.push(`@layer reset {\n${prose.join('\n\n')}\n}`);
|
|
436
|
+
const base = [root.css.base, ...schemes.base].filter(Boolean);
|
|
437
|
+
if (base.length > 0)
|
|
438
|
+
parts.push(`@layer astryx-base {\n${base.join('\n\n')}\n}`);
|
|
439
|
+
if (themed.length > 0)
|
|
440
|
+
parts.push(`@layer astryx-theme {\n${themed.join('\n\n')}\n}`);
|
|
441
|
+
return `${parts.join('\n\n')}\n`;
|
|
442
|
+
}
|
|
@@ -128,6 +128,18 @@ export const docsDense = {
|
|
|
128
128
|
null,
|
|
129
129
|
],
|
|
130
130
|
},
|
|
131
|
+
{
|
|
132
|
+
section: 'Building a Theme Family',
|
|
133
|
+
title: 'Family Build',
|
|
134
|
+
content: [
|
|
135
|
+
{
|
|
136
|
+
type: 'prose',
|
|
137
|
+
text: 'theme build --family <base> <descendants...> --family-key <key> emits one keyed .css + .js + .d.ts beside the root; key must differ from every member name. load CSS once; import ESM separately; switch by theme identity only. all members download eagerly. --check compares the trio. use standalone build for one theme.',
|
|
138
|
+
},
|
|
139
|
+
null,
|
|
140
|
+
null,
|
|
141
|
+
],
|
|
142
|
+
},
|
|
131
143
|
{
|
|
132
144
|
section: 'Runtime vs Built Themes',
|
|
133
145
|
title: 'Runtime vs Built',
|