@astryxdesign/cli 0.7.5-canary.33645a2 → 0.7.5-canary.34d2513
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/api/theme/build/build.icon-function-entries.test.mjs +499 -0
- package/api/theme/build/build.mjs +322 -106
- package/api/theme/build/build.project-core.test.mjs +63 -8
- package/api/theme/build/build.test.mjs +50 -0
- package/api/theme/build/icon-imports.d.mts +12 -2
- package/api/theme/build/icon-imports.mjs +36 -11
- package/assets/docs/author-a-theme.doc.mjs +2 -0
- package/assets/docs/icons.doc.mjs +1 -1
- package/clients/cli/commands/build-theme.outside-project.test.mjs +140 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +2 -6
- package/package.json +9 -9
|
@@ -12,8 +12,10 @@
|
|
|
12
12
|
* build emits the exact CSS the `<Theme>` runtime does), writes:
|
|
13
13
|
* - A CSS file with token overrides and component styles
|
|
14
14
|
* - A CSS declaration module for strict side-effect imports
|
|
15
|
-
* - A JS module that re-exports the built theme (+ icon registry)
|
|
16
|
-
*
|
|
15
|
+
* - A JS module that re-exports the built theme (+ icon registry); function
|
|
16
|
+
* icon entries stay functions for `Icon`, beside a node view of the registry
|
|
17
|
+
* - A .d.ts (plus an optional .variants.d.ts for custom prop values and the
|
|
18
|
+
* theme's icon variant names)
|
|
17
19
|
*
|
|
18
20
|
* It performs the writes and returns a `theme.build` receipt — its `warnings`
|
|
19
21
|
* carry override problems and every declaration core's generator dropped
|
|
@@ -58,7 +60,7 @@
|
|
|
58
60
|
|
|
59
61
|
import * as fs from 'node:fs';
|
|
60
62
|
import * as path from 'node:path';
|
|
61
|
-
import {isBuiltin} from 'node:module';
|
|
63
|
+
import {createRequire, isBuiltin} from 'node:module';
|
|
62
64
|
import {fileURLToPath, pathToFileURL} from 'node:url';
|
|
63
65
|
import {parse} from '@babel/parser';
|
|
64
66
|
import {createJiti} from 'jiti';
|
|
@@ -306,7 +308,9 @@ function realLocation(target) {
|
|
|
306
308
|
* Core, so building with it keeps the CSS identical; and a CLI run one-off
|
|
307
309
|
* (`npx @astryxdesign/cli`) has no Core beside it at all. Null when there is
|
|
308
310
|
* nothing better than the CLI's own import: no installed Core (the Astryx
|
|
309
|
-
* repository, a bare directory)
|
|
311
|
+
* repository, a bare directory) or the same Core. A Core that is there but
|
|
312
|
+
* does not load, or lacks the functions the build calls, throws, naming it:
|
|
313
|
+
* building with another Core would not match the app's `<Theme>`.
|
|
310
314
|
*
|
|
311
315
|
* @param {string} cwd
|
|
312
316
|
* @param {CoreBindings} cliCore - The CLI's own Core, from {@link loadCliCore}.
|
|
@@ -316,29 +320,36 @@ function realLocation(target) {
|
|
|
316
320
|
*/
|
|
317
321
|
async function loadProjectCore(cwd, cliCore, {projectOnly = false} = {}) {
|
|
318
322
|
// Outside a project, the Core the answering copy carries (spec AST-081 FR3).
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
(projectOnly ? null : fetchCarriedThemeCoreDir(cwd));
|
|
323
|
+
const installed = findInstalledPackage(cwd, '@astryxdesign/core');
|
|
324
|
+
const dir = installed ?? (projectOnly ? null : fetchCarriedThemeCoreDir(cwd));
|
|
322
325
|
const themeFile = dir ? importTarget(dir, './theme') : null;
|
|
323
326
|
if (!dir || !themeFile) return null;
|
|
324
327
|
if (cliCore.themeModule) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
import.meta.resolve('@astryxdesign/core/theme'),
|
|
328
|
-
);
|
|
329
|
-
if (realOrSelf(own) === realOrSelf(themeFile)) return null;
|
|
330
|
-
} catch {
|
|
331
|
-
// The CLI's own Core cannot be located; load the project's.
|
|
332
|
-
}
|
|
328
|
+
const own = cliCoreThemeFile();
|
|
329
|
+
if (own != null && realOrSelf(own) === realOrSelf(themeFile)) return null;
|
|
333
330
|
}
|
|
334
331
|
let themeModule;
|
|
335
332
|
try {
|
|
336
333
|
themeModule = await import(pathToFileURL(themeFile).href);
|
|
337
|
-
} catch {
|
|
338
|
-
|
|
334
|
+
} catch (error) {
|
|
335
|
+
throw new AstryxError(
|
|
336
|
+
installed == null
|
|
337
|
+
? carriedCoreMessage(dir, /** @type {Error} */ (error))
|
|
338
|
+
: coreUnavailableMessage(cwd, /** @type {Error} */ (error)),
|
|
339
|
+
undefined,
|
|
340
|
+
ERROR_CODES.ERR_CORE_NOT_FOUND,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const missing = ['defineTheme', 'generateThemeRulesSplit'].filter(
|
|
344
|
+
name => typeof themeModule[name] !== 'function',
|
|
345
|
+
);
|
|
346
|
+
if (missing.length > 0) {
|
|
347
|
+
throw new AstryxError(
|
|
348
|
+
`The @astryxdesign/core at ${path.relative(cwd, dir) || '.'} does not export ${missing.join(' or ')} from its \`./theme\` entry, which \`${formatCliCommand('theme build')}\` calls. Update it.`,
|
|
349
|
+
undefined,
|
|
350
|
+
ERROR_CODES.ERR_CORE_INCOMPATIBLE,
|
|
351
|
+
);
|
|
339
352
|
}
|
|
340
|
-
if (!themeModule.defineTheme || !themeModule.generateThemeRulesSplit)
|
|
341
|
-
return null;
|
|
342
353
|
let rootModule = null;
|
|
343
354
|
const rootFile = importTarget(dir, '.');
|
|
344
355
|
if (rootFile) {
|
|
@@ -359,6 +370,40 @@ async function loadProjectCore(cwd, cliCore, {projectOnly = false} = {}) {
|
|
|
359
370
|
};
|
|
360
371
|
}
|
|
361
372
|
|
|
373
|
+
/**
|
|
374
|
+
* The file the CLI's own `@astryxdesign/core/theme` resolves to, or null when
|
|
375
|
+
* the CLI has no Core of its own (a one-off `npx` run).
|
|
376
|
+
* @returns {string | null}
|
|
377
|
+
*/
|
|
378
|
+
function cliCoreThemeFile() {
|
|
379
|
+
try {
|
|
380
|
+
return fileURLToPath(import.meta.resolve('@astryxdesign/core/theme'));
|
|
381
|
+
} catch (error) {
|
|
382
|
+
const {code} = /** @type {NodeJS.ErrnoException} */ (error);
|
|
383
|
+
if (
|
|
384
|
+
code === 'ERR_MODULE_NOT_FOUND' ||
|
|
385
|
+
code === 'ERR_PACKAGE_PATH_NOT_EXPORTED'
|
|
386
|
+
) {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* The error for a Core the answering copy carries (outside a project) that
|
|
395
|
+
* does not load.
|
|
396
|
+
* @param {string} dir
|
|
397
|
+
* @param {Error} importError
|
|
398
|
+
* @returns {string}
|
|
399
|
+
*/
|
|
400
|
+
function carriedCoreMessage(dir, importError) {
|
|
401
|
+
return (
|
|
402
|
+
`Could not load the @astryxdesign/core this CLI release carries, at ${dir}.` +
|
|
403
|
+
`\n Import error: ${importError.message}`
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
362
407
|
/** @type {Map<string, Awaited<ReturnType<typeof loadProjectCore>>>} */
|
|
363
408
|
const _projectCores = new Map();
|
|
364
409
|
|
|
@@ -379,7 +424,7 @@ async function selectCore(cwd, {projectOnly = false} = {}) {
|
|
|
379
424
|
// A global or cached copy's own Core is the carried one.
|
|
380
425
|
if (projectOnly && !projectCore && carriesCore() && !cliCoreIsProjects(cwd)) {
|
|
381
426
|
throw new AstryxError(
|
|
382
|
-
"The project's Core isn't installed
|
|
427
|
+
"The project's Core isn't installed.",
|
|
383
428
|
undefined,
|
|
384
429
|
ERROR_CODES.ERR_CORE_NOT_FOUND,
|
|
385
430
|
);
|
|
@@ -405,13 +450,11 @@ function carriesCore() {
|
|
|
405
450
|
function cliCoreIsProjects(cwd) {
|
|
406
451
|
const dir = findProjectCoreDir(cwd);
|
|
407
452
|
if (!dir) return false;
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
453
|
+
const own = cliCoreThemeFile();
|
|
454
|
+
const theirs = importTarget(dir, './theme');
|
|
455
|
+
return (
|
|
456
|
+
own != null && theirs != null && realOrSelf(own) === realOrSelf(theirs)
|
|
457
|
+
);
|
|
415
458
|
}
|
|
416
459
|
|
|
417
460
|
/**
|
|
@@ -1536,12 +1579,21 @@ async function validateAdaptationEnrollment(themeDef) {
|
|
|
1536
1579
|
* generating a `declare module` block for them would be dead code — those are
|
|
1537
1580
|
* skipped.
|
|
1538
1581
|
*
|
|
1539
|
-
*
|
|
1582
|
+
* A theme that lists `iconVariants` also augments `IconVariantMap` in
|
|
1583
|
+
* `@astryxdesign/core/Icon` with those names, when the installed core declares
|
|
1584
|
+
* it, so `<Icon variant>` accepts them (spec:AST-087 FR3).
|
|
1585
|
+
*
|
|
1586
|
+
* @param {{components?: Record<string, Record<string, Record<string, unknown>>>, iconVariants?: readonly string[]}} themeDef - Theme definition (resolved by defineTheme)
|
|
1540
1587
|
* @returns {Promise<string|null>} TypeScript declaration content, or null if no augmentations needed
|
|
1541
1588
|
*/
|
|
1542
1589
|
async function generateVariantDeclarationsAsync(themeDef) {
|
|
1543
1590
|
const componentLayers = getThemeComponentLayers(themeDef);
|
|
1544
|
-
|
|
1591
|
+
const iconVariantNames =
|
|
1592
|
+
Array.isArray(themeDef.iconVariants) &&
|
|
1593
|
+
componentHasAugmentableInterface('Icon', 'IconVariantMap')
|
|
1594
|
+
? [...new Set(themeDef.iconVariants)]
|
|
1595
|
+
: [];
|
|
1596
|
+
if (componentLayers.length === 0 && iconVariantNames.length === 0) {
|
|
1545
1597
|
return null;
|
|
1546
1598
|
}
|
|
1547
1599
|
|
|
@@ -1577,7 +1629,7 @@ async function generateVariantDeclarationsAsync(themeDef) {
|
|
|
1577
1629
|
const hasCustom = Object.values(customValues).some(props =>
|
|
1578
1630
|
Object.values(props).some(values => values.size > 0),
|
|
1579
1631
|
);
|
|
1580
|
-
if (!hasCustom) return null;
|
|
1632
|
+
if (!hasCustom && iconVariantNames.length === 0) return null;
|
|
1581
1633
|
|
|
1582
1634
|
const sections = ['// Generated by astryx theme build', 'export {};', ''];
|
|
1583
1635
|
|
|
@@ -1617,6 +1669,17 @@ async function generateVariantDeclarationsAsync(themeDef) {
|
|
|
1617
1669
|
}
|
|
1618
1670
|
}
|
|
1619
1671
|
|
|
1672
|
+
if (iconVariantNames.length > 0) {
|
|
1673
|
+
sections.push(`declare module '@astryxdesign/core/Icon' {`);
|
|
1674
|
+
sections.push(' interface IconVariantMap {');
|
|
1675
|
+
for (const name of iconVariantNames) {
|
|
1676
|
+
sections.push(` ${tsStringLiteral(name)}: true;`);
|
|
1677
|
+
}
|
|
1678
|
+
sections.push(' }');
|
|
1679
|
+
sections.push('}');
|
|
1680
|
+
sections.push('');
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1620
1683
|
// If every custom value targeted a non-augmentable prop, there's nothing to
|
|
1621
1684
|
// emit beyond the header — return null so no `.variants.d.ts` is written.
|
|
1622
1685
|
const hasEmittedAugmentation = sections.some(line =>
|
|
@@ -1855,19 +1918,43 @@ async function importThemeModule(filePath, interception, /** @type {any} */ load
|
|
|
1855
1918
|
commonJsPatch.undo();
|
|
1856
1919
|
}
|
|
1857
1920
|
} else {
|
|
1858
|
-
mod = await jiti.import(filePath
|
|
1921
|
+
mod = await jiti.import(filePath);
|
|
1859
1922
|
}
|
|
1860
1923
|
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1924
|
+
// Both loads hand back the module. The theme is read from the module's own
|
|
1925
|
+
// exports, never from the module object itself: jiti's interop answers a
|
|
1926
|
+
// property the module lacks from its default export (and a missing
|
|
1927
|
+
// `default` with the module), so the module can pass for the theme while
|
|
1928
|
+
// its own keys hide the theme's fields.
|
|
1929
|
+
const isModule = mod != null && typeof mod === 'object';
|
|
1930
|
+
const hasDefault = isModule && Object.hasOwn(mod, 'default');
|
|
1931
|
+
const defaultExport = hasDefault ? mod.default : undefined;
|
|
1932
|
+
if (isThemeObject(defaultExport)) {
|
|
1933
|
+
return {theme: defaultExport, exportName: 'default', degraded};
|
|
1934
|
+
}
|
|
1935
|
+
if (isModule) {
|
|
1867
1936
|
for (const [exportName, value] of Object.entries(mod)) {
|
|
1937
|
+
if (exportName === 'default' || exportName === 'module.exports') continue;
|
|
1868
1938
|
if (isThemeObject(value)) return {theme: value, exportName, degraded};
|
|
1869
1939
|
}
|
|
1870
1940
|
}
|
|
1941
|
+
// A plain-object default export is the module's theme even when it has
|
|
1942
|
+
// no tokens of its own (local tokens, typography or component styles may
|
|
1943
|
+
// be all it defines), or no name: the build's validation says what it
|
|
1944
|
+
// lacks.
|
|
1945
|
+
if (isPlainObject(defaultExport)) {
|
|
1946
|
+
return {theme: defaultExport, exportName: 'default', degraded};
|
|
1947
|
+
}
|
|
1948
|
+
// A CommonJS module whose exports are the theme (`module.exports =
|
|
1949
|
+
// theme`) has no default of its own: interop answers `default` with
|
|
1950
|
+
// those exports, and without interop (a family build's loader) the module
|
|
1951
|
+
// is them.
|
|
1952
|
+
if (isModule && !hasDefault) {
|
|
1953
|
+
const exportsObject = mod.default ?? mod;
|
|
1954
|
+
if (isThemeObject(exportsObject)) {
|
|
1955
|
+
return {theme: exportsObject, exportName: 'default', degraded};
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1871
1958
|
|
|
1872
1959
|
throw new Error(
|
|
1873
1960
|
`Could not find a defineTheme() result in ${filePath}.\n` +
|
|
@@ -1875,6 +1962,45 @@ async function importThemeModule(filePath, interception, /** @type {any} */ load
|
|
|
1875
1962
|
);
|
|
1876
1963
|
}
|
|
1877
1964
|
|
|
1965
|
+
/**
|
|
1966
|
+
* Whether the theme file's own folder resolves Core's theme entry, as the
|
|
1967
|
+
* theme's own `import ... from '@astryxdesign/core/theme'` would. A theme
|
|
1968
|
+
* outside any project (an empty folder, a fresh clone) does not, and is
|
|
1969
|
+
* handed the Core the CLI answers with instead, as a new project with that
|
|
1970
|
+
* Core would build it (`theme build` runs the same outside a project, spec
|
|
1971
|
+
* AST-081 FR3). Only a Core not installed there (MODULE_NOT_FOUND) reads as
|
|
1972
|
+
* no: an installed Core whose entry does not resolve is the theme's own, and
|
|
1973
|
+
* its load reports that.
|
|
1974
|
+
* @param {string} filePath
|
|
1975
|
+
* @returns {boolean}
|
|
1976
|
+
*/
|
|
1977
|
+
function themeFolderResolvesCore(filePath) {
|
|
1978
|
+
try {
|
|
1979
|
+
createRequire(filePath).resolve('@astryxdesign/core/theme');
|
|
1980
|
+
return true;
|
|
1981
|
+
} catch (error) {
|
|
1982
|
+
const {code} = /** @type {NodeJS.ErrnoException} */ (error);
|
|
1983
|
+
if (code === 'MODULE_NOT_FOUND') return false;
|
|
1984
|
+
if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') return true;
|
|
1985
|
+
throw error;
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
/**
|
|
1990
|
+
* A plain object, not an array or a module namespace: a theme module's
|
|
1991
|
+
* default export, read as its theme.
|
|
1992
|
+
* @param {any} value
|
|
1993
|
+
* @returns {boolean}
|
|
1994
|
+
*/
|
|
1995
|
+
function isPlainObject(value) {
|
|
1996
|
+
return (
|
|
1997
|
+
value != null &&
|
|
1998
|
+
typeof value === 'object' &&
|
|
1999
|
+
!Array.isArray(value) &&
|
|
2000
|
+
value[Symbol.toStringTag] !== 'Module'
|
|
2001
|
+
);
|
|
2002
|
+
}
|
|
2003
|
+
|
|
1878
2004
|
/**
|
|
1879
2005
|
* @param {any} value
|
|
1880
2006
|
* @returns {boolean}
|
|
@@ -1890,12 +2016,9 @@ function isThemeObject(value) {
|
|
|
1890
2016
|
}
|
|
1891
2017
|
|
|
1892
2018
|
/**
|
|
1893
|
-
* Extract the theme definition from a JS/TS file
|
|
1894
|
-
*
|
|
1895
|
-
*
|
|
1896
|
-
* The legacy fallback needs no interception: it evaluates the `defineTheme()`
|
|
1897
|
-
* ARGUMENT, so what it returns is raw authored input with `adaptations` still
|
|
1898
|
-
* on it — nothing has erased anything yet. It is therefore never degraded.
|
|
2019
|
+
* Extract the theme definition from a JS/TS file, through jiti (full TS
|
|
2020
|
+
* support). A module that does not load fails with its own error: the theme
|
|
2021
|
+
* is never read from the file's text instead.
|
|
1899
2022
|
*
|
|
1900
2023
|
* @param {string} filePath
|
|
1901
2024
|
* @param {import('./core-interception.mjs').CoreInterception} [interception]
|
|
@@ -1906,18 +2029,12 @@ async function extractThemeDefinition(filePath, interception, /** @type {any} */
|
|
|
1906
2029
|
try {
|
|
1907
2030
|
return await importThemeModule(filePath, interception, loader);
|
|
1908
2031
|
} catch (jitiError) {
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
}
|
|
1914
|
-
|
|
1915
|
-
const je = /** @type {Error} */ (jitiError);
|
|
1916
|
-
throw new Error(
|
|
1917
|
-
`Failed to load theme from ${filePath}: ${je.message}\n` +
|
|
1918
|
-
`Make sure all imports in the theme file are resolvable.`,
|
|
1919
|
-
);
|
|
1920
|
-
}
|
|
2032
|
+
const je = /** @type {Error} */ (jitiError);
|
|
2033
|
+
throw new Error(
|
|
2034
|
+
`Failed to load theme from ${filePath}: ${je.message}\n` +
|
|
2035
|
+
`Make sure all imports in the theme file are resolvable.`,
|
|
2036
|
+
{cause: jitiError},
|
|
2037
|
+
);
|
|
1921
2038
|
}
|
|
1922
2039
|
}
|
|
1923
2040
|
|
|
@@ -1963,45 +2080,6 @@ export async function validateThemePrivateInputs(
|
|
|
1963
2080
|
}
|
|
1964
2081
|
}
|
|
1965
2082
|
|
|
1966
|
-
/**
|
|
1967
|
-
* Fallback extraction via regex + eval.
|
|
1968
|
-
* Only works for plain object literals — can't follow imports or variables.
|
|
1969
|
-
* Never erase icon references: an unresolved registry must preserve the loader
|
|
1970
|
-
* error instead of turning an incomplete theme into a successful build.
|
|
1971
|
-
* @param {string} filePath
|
|
1972
|
-
* @returns {any}
|
|
1973
|
-
*/
|
|
1974
|
-
function extractThemeDefinitionLegacy(filePath) {
|
|
1975
|
-
const content = fs.readFileSync(filePath, 'utf8');
|
|
1976
|
-
|
|
1977
|
-
const defineMatch = content.match(/defineTheme\s*\(\s*({[\s\S]*?})\s*\)/);
|
|
1978
|
-
if (!defineMatch) {
|
|
1979
|
-
const defaultMatch = content.match(/export\s+default\s+({[\s\S]*?});/);
|
|
1980
|
-
if (!defaultMatch) {
|
|
1981
|
-
throw new Error(
|
|
1982
|
-
`Could not find defineTheme() call or default export in ${filePath}.\n` +
|
|
1983
|
-
`Expected: defineTheme({ name: '...', tokens: {...} })`,
|
|
1984
|
-
);
|
|
1985
|
-
}
|
|
1986
|
-
|
|
1987
|
-
return eval(`(${defaultMatch[1]})`);
|
|
1988
|
-
}
|
|
1989
|
-
|
|
1990
|
-
let objStr = defineMatch[1];
|
|
1991
|
-
objStr = objStr.replace(/\s+as\s+const/g, '');
|
|
1992
|
-
|
|
1993
|
-
try {
|
|
1994
|
-
return eval(`(${objStr})`);
|
|
1995
|
-
} catch (e) {
|
|
1996
|
-
const err = /** @type {Error} */ (e);
|
|
1997
|
-
throw new Error(
|
|
1998
|
-
`Failed to parse theme definition in ${filePath}: ${err.message}\n` +
|
|
1999
|
-
`Make sure the defineTheme() argument is a plain object literal.`,
|
|
2000
|
-
{cause: e},
|
|
2001
|
-
);
|
|
2002
|
-
}
|
|
2003
|
-
}
|
|
2004
|
-
|
|
2005
2083
|
/**
|
|
2006
2084
|
* Extract a family member's named registry import from its source file.
|
|
2007
2085
|
* Returns { importPath, exportName } or null when the theme names none.
|
|
@@ -2042,6 +2120,53 @@ function extractRegistryInfo(filePath, field) {
|
|
|
2042
2120
|
return null;
|
|
2043
2121
|
}
|
|
2044
2122
|
|
|
2123
|
+
/**
|
|
2124
|
+
* Whether a theme's icon entries include a function of `{size, variant}`.
|
|
2125
|
+
* @param {Record<string, unknown> | null | undefined} entries
|
|
2126
|
+
* @returns {boolean}
|
|
2127
|
+
*/
|
|
2128
|
+
function hasFunctionIconEntry(entries) {
|
|
2129
|
+
return Object.values(entries ?? {}).some(
|
|
2130
|
+
entry => typeof entry === 'function',
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
/**
|
|
2135
|
+
* A built theme's icon entries: a theme from defineTheme keeps them in
|
|
2136
|
+
* `__icons` when any is a function (its `icons` is the node view); otherwise
|
|
2137
|
+
* `icons` holds them.
|
|
2138
|
+
* @param {any} theme
|
|
2139
|
+
* @returns {Record<string, unknown> | undefined}
|
|
2140
|
+
*/
|
|
2141
|
+
function themeIconEntries(theme) {
|
|
2142
|
+
return theme?.__icons ?? theme?.icons;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
/**
|
|
2146
|
+
* The generated code that reads a registry's entries as React nodes, each
|
|
2147
|
+
* function entry read at `{size: 'md'}` like core's public reads. It never
|
|
2148
|
+
* passes the registry to a call, so a theme extending the built module can
|
|
2149
|
+
* still trace the registry through its `__icons` field, and it reads no
|
|
2150
|
+
* global, which a registry import could shadow. The registry expression runs
|
|
2151
|
+
* where only `entriesName` and `nodesName` are declared, both names the
|
|
2152
|
+
* registry imports cannot use.
|
|
2153
|
+
* @param {string} entries - Expression for the registry's entries.
|
|
2154
|
+
* @param {string} entriesName - A reserved local name.
|
|
2155
|
+
* @param {string} nodesName - A reserved local name.
|
|
2156
|
+
* @returns {string}
|
|
2157
|
+
*/
|
|
2158
|
+
function iconNodesExpression(entries, entriesName, nodesName) {
|
|
2159
|
+
return `(() => {
|
|
2160
|
+
const ${entriesName} = ${entries};
|
|
2161
|
+
const ${nodesName} = {};
|
|
2162
|
+
for (const name in ${entriesName}) {
|
|
2163
|
+
const entry = ${entriesName}[name];
|
|
2164
|
+
${nodesName}[name] = typeof entry === 'function' ? entry({size: 'md'}) : entry;
|
|
2165
|
+
}
|
|
2166
|
+
return ${nodesName};
|
|
2167
|
+
})()`;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2045
2170
|
/**
|
|
2046
2171
|
* Generate a minimal JS module for a built theme.
|
|
2047
2172
|
* Includes the theme name, marker, and re-exports the icon registry.
|
|
@@ -2063,6 +2188,11 @@ function extractRegistryInfo(filePath, field) {
|
|
|
2063
2188
|
* not given, direct registry specifiers are preserved, and inherited relative
|
|
2064
2189
|
* imports are rebased from the base module to the theme entry's directory.
|
|
2065
2190
|
*
|
|
2191
|
+
* When an icon entry is a function of `{size, variant}`, the module keeps the
|
|
2192
|
+
* registry as `__icons`, which `Icon` resolves at the size and variant it
|
|
2193
|
+
* renders, and the theme's `icons` and the re-exported registry are the node
|
|
2194
|
+
* view, each function read at `{size: 'md'}` as `DefinedTheme['icons']` is.
|
|
2195
|
+
*
|
|
2066
2196
|
* @param {any} themeDef
|
|
2067
2197
|
* @param {import('./icon-imports.mjs').IconImports | null} iconInfo
|
|
2068
2198
|
* @param {string} [iconsSpecifier] - Overrides the selected registry specifier.
|
|
@@ -2111,12 +2241,28 @@ function generateBuiltModule(
|
|
|
2111
2241
|
? `const ${iconInfo.exportName} = ${iconInfo.expression};\n`
|
|
2112
2242
|
: '';
|
|
2113
2243
|
const iconsExpression = moduleOptions.iconsExpression ?? iconInfo?.exportName;
|
|
2114
|
-
const
|
|
2244
|
+
const iconNodes =
|
|
2245
|
+
iconsExpression && hasFunctionIconEntry(themeIconEntries(themeDef))
|
|
2246
|
+
? `${themeBinding}IconNodes`
|
|
2247
|
+
: null;
|
|
2248
|
+
const iconNodesDeclaration =
|
|
2249
|
+
iconNodes && iconsExpression
|
|
2250
|
+
? `const ${iconNodes} = ${iconNodesExpression(iconsExpression, `${themeBinding}IconEntries`, iconNodes)};\n`
|
|
2251
|
+
: '';
|
|
2252
|
+
const iconsField = iconNodes
|
|
2253
|
+
? ` icons: ${iconNodes},\n __icons: ${iconsExpression},`
|
|
2254
|
+
: iconsExpression
|
|
2255
|
+
? ` icons: ${iconsExpression},`
|
|
2256
|
+
: '';
|
|
2115
2257
|
const indicatorsField = moduleOptions.indicatorsExpression
|
|
2116
2258
|
? `\n indicators: ${moduleOptions.indicatorsExpression},`
|
|
2117
2259
|
: '';
|
|
2118
2260
|
const iconReExport =
|
|
2119
|
-
iconInfo && exportIcons
|
|
2261
|
+
iconInfo && exportIcons
|
|
2262
|
+
? iconNodes
|
|
2263
|
+
? `\nexport { ${iconNodes} as ${iconInfo.exportName} };\n`
|
|
2264
|
+
: `\nexport { ${iconInfo.exportName} };\n`
|
|
2265
|
+
: '';
|
|
2120
2266
|
|
|
2121
2267
|
// Resolve token values — tuples become light-dark() strings
|
|
2122
2268
|
/** @type {Record<string, unknown>} */
|
|
@@ -2190,7 +2336,7 @@ function generateBuiltModule(
|
|
|
2190
2336
|
),
|
|
2191
2337
|
);
|
|
2192
2338
|
|
|
2193
|
-
return `${iconImport}${iconDeclaration}/**
|
|
2339
|
+
return `${iconImport}${iconDeclaration}${iconNodesDeclaration}/**
|
|
2194
2340
|
* ${themeDef.name} theme — built by \`${getCliInvocation()} theme build\`
|
|
2195
2341
|
* Import the CSS file alongside this module:
|
|
2196
2342
|
*
|
|
@@ -2240,7 +2386,39 @@ export declare const ${iconInfo.exportName}: IconRegistry;
|
|
|
2240
2386
|
(typeOptions.includeThemeImport ?? true)
|
|
2241
2387
|
? `import type { DefinedTheme } from '@astryxdesign/core/theme';\n`
|
|
2242
2388
|
: '';
|
|
2243
|
-
return `${variantsRef}${themeImport}${iconType}export declare const ${themeBinding}:
|
|
2389
|
+
return `${variantsRef}${themeImport}${iconType}export declare const ${themeBinding}: ${builtThemeType(themeDef)};\n`;
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
/**
|
|
2393
|
+
* A TypeScript string literal type for `value`.
|
|
2394
|
+
* @param {string} value
|
|
2395
|
+
* @returns {string}
|
|
2396
|
+
*/
|
|
2397
|
+
function tsStringLiteral(value) {
|
|
2398
|
+
const inner = JSON.stringify(value)
|
|
2399
|
+
.slice(1, -1)
|
|
2400
|
+
.replace(/\\"/g, '"')
|
|
2401
|
+
.replace(/'/g, "\\'");
|
|
2402
|
+
return `'${inner}'`;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
/**
|
|
2406
|
+
* The declared type of a built theme. A theme that lists icon variants
|
|
2407
|
+
* carries core's type-only `__iconVariant` marker with those names, so a theme
|
|
2408
|
+
* that extends the built one types its icon entries' `variant` by the list,
|
|
2409
|
+
* exactly as it would extending the source theme.
|
|
2410
|
+
* SYNC: packages/core/src/theme/defineTheme.ts (ResolvedDefinedTheme)
|
|
2411
|
+
* @param {any} themeDef
|
|
2412
|
+
* @returns {string}
|
|
2413
|
+
*/
|
|
2414
|
+
function builtThemeType(themeDef) {
|
|
2415
|
+
const names = themeDef.iconVariants;
|
|
2416
|
+
if (!Array.isArray(names)) return 'DefinedTheme';
|
|
2417
|
+
const variant =
|
|
2418
|
+
names.length > 0
|
|
2419
|
+
? [...new Set(names)].map(tsStringLiteral).join(' | ')
|
|
2420
|
+
: 'never';
|
|
2421
|
+
return `DefinedTheme & { __iconVariant?(variant: ${variant}): void }`;
|
|
2244
2422
|
}
|
|
2245
2423
|
|
|
2246
2424
|
// =============================================================================
|
|
@@ -2617,13 +2795,15 @@ async function themeBuildInternal(
|
|
|
2617
2795
|
}
|
|
2618
2796
|
await selectCore(cwd, {projectOnly: projectCore});
|
|
2619
2797
|
|
|
2620
|
-
// Standalone builds
|
|
2621
|
-
// adaptations
|
|
2798
|
+
// Standalone builds need interception when an older Core could erase
|
|
2799
|
+
// adaptations, and when the theme's folder has no Core of its own: its
|
|
2800
|
+
// imports of Core are then handed the Core this build selected, as family
|
|
2801
|
+
// builds' are. Family preparation always supplies the same recorder as its
|
|
2622
2802
|
// shared loader so exact authored parent identity stays CLI-private instead
|
|
2623
2803
|
// of becoming a public DefinedTheme field.
|
|
2624
2804
|
const interception =
|
|
2625
2805
|
options.__familyInterception ??
|
|
2626
|
-
(_generateAdaptationCSS
|
|
2806
|
+
(_generateAdaptationCSS && themeFolderResolvesCore(filePath)
|
|
2627
2807
|
? undefined
|
|
2628
2808
|
: interceptCore(_coreThemeModule, _coreRootModule));
|
|
2629
2809
|
|
|
@@ -2712,10 +2892,26 @@ async function themeBuildInternal(
|
|
|
2712
2892
|
// Family preparation keeps its own named-import registry contract, which
|
|
2713
2893
|
// themeBuildFamily checks against the selected members' graph.
|
|
2714
2894
|
const standaloneIcons = !options.__prepareFamily;
|
|
2895
|
+
// Function icon entries stay functions in the built module, beside a node
|
|
2896
|
+
// view bound to `<theme>IconNodes` and computed from `<theme>IconEntries`.
|
|
2897
|
+
const functionEntries =
|
|
2898
|
+
hasFunctionIconEntry(themeIconEntries(themeDef)) ||
|
|
2899
|
+
hasFunctionIconEntry(themeIconEntries(themeDef.extends));
|
|
2900
|
+
/** @param {boolean} reserveIconNodes */
|
|
2901
|
+
const reservedNames = reserveIconNodes => [
|
|
2902
|
+
`${toIdentifier(themeDef.name)}Theme`,
|
|
2903
|
+
...(reserveIconNodes
|
|
2904
|
+
? [
|
|
2905
|
+
`${toIdentifier(themeDef.name)}ThemeIconNodes`,
|
|
2906
|
+
`${toIdentifier(themeDef.name)}ThemeIconEntries`,
|
|
2907
|
+
]
|
|
2908
|
+
: []),
|
|
2909
|
+
];
|
|
2715
2910
|
const iconResolution = {
|
|
2716
2911
|
readSource: (/** @type {string} */ file) => fs.readFileSync(file, 'utf8'),
|
|
2717
2912
|
resolveModule: resolveThemeModule,
|
|
2718
|
-
reservedNames:
|
|
2913
|
+
reservedNames: reservedNames(functionEntries),
|
|
2914
|
+
functionEntries,
|
|
2719
2915
|
rawInput: 'extends' in themeDef,
|
|
2720
2916
|
hasIcons:
|
|
2721
2917
|
Object.keys(themeDef.icons ?? {}).length > 0 ||
|
|
@@ -2839,9 +3035,14 @@ async function themeBuildInternal(
|
|
|
2839
3035
|
!iconInfo &&
|
|
2840
3036
|
Object.keys(resolvedTheme.icons ?? {}).length > 0
|
|
2841
3037
|
) {
|
|
3038
|
+
const resolvedFunctionEntries = hasFunctionIconEntry(
|
|
3039
|
+
themeIconEntries(resolvedTheme),
|
|
3040
|
+
);
|
|
2842
3041
|
iconInfo = await resolveIconImports(filePath, themeExportName, {
|
|
2843
3042
|
...iconResolution,
|
|
2844
3043
|
hasIcons: true,
|
|
3044
|
+
functionEntries: resolvedFunctionEntries,
|
|
3045
|
+
reservedNames: reservedNames(resolvedFunctionEntries),
|
|
2845
3046
|
});
|
|
2846
3047
|
}
|
|
2847
3048
|
// An opaque package base can legitimately have no icons. Its .icons
|
|
@@ -3104,7 +3305,7 @@ async function themeBuildInternal(
|
|
|
3104
3305
|
);
|
|
3105
3306
|
const dtsContent =
|
|
3106
3307
|
generatedHeader(sourceRelative, 'ts', buildCommand, versions) +
|
|
3107
|
-
generateBuiltTypes(themeDef, iconInfo, variantsFileName);
|
|
3308
|
+
generateBuiltTypes(resolvedTheme || themeDef, iconInfo, variantsFileName);
|
|
3108
3309
|
|
|
3109
3310
|
const unloadedFonts = [
|
|
3110
3311
|
...new Set([
|
|
@@ -3551,8 +3752,16 @@ export async function themeBuildFamily(
|
|
|
3551
3752
|
['icons', member.iconInfo],
|
|
3552
3753
|
['indicators', member.indicatorInfo],
|
|
3553
3754
|
]) {
|
|
3554
|
-
|
|
3555
|
-
|
|
3755
|
+
// A theme with a function entry gets its own node view of `icons`, so
|
|
3756
|
+
// an inherit-only member is recognized by its entries.
|
|
3757
|
+
const value =
|
|
3758
|
+
field === 'icons'
|
|
3759
|
+
? themeIconEntries(member.theme)
|
|
3760
|
+
: member.theme[field];
|
|
3761
|
+
const inherited =
|
|
3762
|
+
field === 'icons'
|
|
3763
|
+
? themeIconEntries(member.sourceParent)
|
|
3764
|
+
: member.sourceParent?.[field];
|
|
3556
3765
|
if (
|
|
3557
3766
|
value &&
|
|
3558
3767
|
Object.keys(value).length > 0 &&
|
|
@@ -3623,7 +3832,14 @@ export async function themeBuildFamily(
|
|
|
3623
3832
|
// prettier-ignore
|
|
3624
3833
|
for (const registry of [icon, indicator]) if (registry.info) registryImports.push({specifier: registry.specifier ?? registry.info.importPath, exportName: registry.info.exportName});
|
|
3625
3834
|
const ownIcons = icon.info ? `${themeBinding}Icons` : null;
|
|
3626
|
-
|
|
3835
|
+
// A parent with function entries keeps them in `__icons`; its `icons`
|
|
3836
|
+
// is the node view.
|
|
3837
|
+
const parent = members.find(
|
|
3838
|
+
other => other.theme.name === member.parentName,
|
|
3839
|
+
);
|
|
3840
|
+
const inheritedIcons = parentBinding
|
|
3841
|
+
? `${parentBinding}.${hasFunctionIconEntry(themeIconEntries(parent?.theme)) ? '__icons' : 'icons'}`
|
|
3842
|
+
: null;
|
|
3627
3843
|
const ownIndicators = indicator.info ? `${themeBinding}Indicators` : null;
|
|
3628
3844
|
const inheritedIndicators = parentBinding
|
|
3629
3845
|
? `${parentBinding}.indicators`
|