@astryxdesign/cli 0.7.9-canary.f384e62 → 0.7.9-canary.f85b201

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.
Files changed (31) hide show
  1. package/README.md +1 -1
  2. package/api/component/__fixtures__/resolutions/modules.txt +1 -1
  3. package/api/swizzle/copy/copy.d.mts +17 -1
  4. package/api/swizzle/copy/copy.mjs +240 -5
  5. package/api/swizzle/swizzle.doc.mjs +1 -1
  6. package/api/swizzle/swizzle.type.mjs +1 -1
  7. package/api/theme/build/build.mjs +340 -71
  8. package/api/theme/build/icon-imports.d.mts +35 -4
  9. package/api/theme/build/icon-imports.mjs +299 -38
  10. package/assets/docs/author-a-theme.doc.mjs +3 -1
  11. package/assets/docs/icons.doc.mjs +29 -0
  12. package/assets/docs/motion.doc.mjs +1 -1
  13. package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.doc.mjs +14 -0
  14. package/assets/templates/blocks/components/Hooks/useMergedRefsHookUsage.tsx +78 -0
  15. package/assets/templates/blocks/components/Hooks/useScrollOverflowHookUsage.doc.mjs +14 -0
  16. package/assets/templates/blocks/components/Hooks/useScrollOverflowHookUsage.tsx +94 -0
  17. package/assets/templates/blocks/components/Link/useLinkComponentHookUsage.doc.mjs +14 -0
  18. package/assets/templates/blocks/components/Link/useLinkComponentHookUsage.tsx +89 -0
  19. package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.doc.mjs +15 -0
  20. package/assets/templates/blocks/components/usePanGesture/PanDraggableBox.tsx +98 -0
  21. package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.doc.mjs +14 -0
  22. package/assets/templates/blocks/components/usePanGesture/PanPlayingCard.tsx +147 -0
  23. package/assets/theme.template.ts +14 -2
  24. package/authoring/doctypes/base/type.ts +41 -0
  25. package/authoring/doctypes/component/component.doc.mjs +7 -1
  26. package/authoring/doctypes/component/type.ts +5 -0
  27. package/authoring/index.d.ts +2 -0
  28. package/clients/cli/commands/swizzle.mjs +10 -0
  29. package/clients/cli/lib/component-format.mjs +49 -0
  30. package/foundation/response/response-types.doc.mjs +1 -1
  31. package/package.json +9 -9
@@ -1013,31 +1013,44 @@ function resolveCoreRoot() {
1013
1013
  const _componentDeclCache = new Map();
1014
1014
  /**
1015
1015
  * @param {string} pascalName
1016
+ * @param {{includeTypes?: boolean}} [options]
1016
1017
  * @returns {string}
1017
1018
  */
1018
- function readComponentDeclarations(pascalName) {
1019
- if (_componentDeclCache.has(pascalName)) {
1020
- return _componentDeclCache.get(pascalName) ?? '';
1019
+ function readComponentDeclarations(pascalName, options = {}) {
1020
+ const includeTypes = options.includeTypes === true;
1021
+ const cacheKey = `${pascalName}:${includeTypes ? 'with-types' : 'index-only'}`;
1022
+ if (_componentDeclCache.has(cacheKey)) {
1023
+ return _componentDeclCache.get(cacheKey) ?? '';
1021
1024
  }
1022
1025
  let contents = '';
1023
1026
  const coreRoot = resolveCoreRoot();
1024
1027
  if (coreRoot) {
1025
- const candidates = [
1026
- path.join(coreRoot, 'dist', pascalName, 'index.d.ts'),
1027
- path.join(coreRoot, 'src', pascalName, 'index.ts'),
1028
+ const candidateSets = [
1029
+ [path.join(coreRoot, 'dist', pascalName, 'index.d.ts')],
1030
+ [path.join(coreRoot, 'src', pascalName, 'index.ts')],
1028
1031
  ];
1029
- for (const file of candidates) {
1030
- try {
1031
- if (fs.existsSync(file)) {
1032
- contents = fs.readFileSync(file, 'utf-8');
1033
- break;
1032
+ if (includeTypes) {
1033
+ candidateSets[0].push(
1034
+ path.join(coreRoot, 'dist', pascalName, 'types.d.ts'),
1035
+ );
1036
+ candidateSets[1].push(path.join(coreRoot, 'src', pascalName, 'types.ts'));
1037
+ }
1038
+ for (const files of candidateSets) {
1039
+ if (fs.existsSync(files[0])) {
1040
+ for (const file of files) {
1041
+ try {
1042
+ if (fs.existsSync(file)) {
1043
+ contents += fs.readFileSync(file, 'utf-8') + '\n';
1044
+ }
1045
+ } catch {
1046
+ // ignore
1047
+ }
1034
1048
  }
1035
- } catch {
1036
- // ignore and try the next candidate
1049
+ if (contents) break;
1037
1050
  }
1038
1051
  }
1039
1052
  }
1040
- _componentDeclCache.set(pascalName, contents);
1053
+ _componentDeclCache.set(cacheKey, contents);
1041
1054
  return contents;
1042
1055
  }
1043
1056
 
@@ -1163,10 +1176,11 @@ async function resolveAugmentationTargetCandidates(componentName) {
1163
1176
  * re-exported) as a type/interface.
1164
1177
  * @param {string} pascalName
1165
1178
  * @param {string} interfaceName
1179
+ * @param {{includeTypes?: boolean}} [options]
1166
1180
  * @returns {boolean}
1167
1181
  */
1168
- function componentHasAugmentableInterface(pascalName, interfaceName) {
1169
- const decl = readComponentDeclarations(pascalName);
1182
+ function componentHasAugmentableInterface(pascalName, interfaceName, options) {
1183
+ const decl = readComponentDeclarations(pascalName, options);
1170
1184
  if (!decl) return false;
1171
1185
  // Require an actual interface declaration in this public subpath, not just a
1172
1186
  // type re-export. Module augmentation only widens consumers that import the
@@ -1199,6 +1213,56 @@ function isComponentValueAugmentationPoint(pascalName, interfaceName) {
1199
1213
  );
1200
1214
  }
1201
1215
 
1216
+ /**
1217
+ * Extension points whose public module and interface do not follow the usual
1218
+ * `<Component><Prop>Map` convention. TextType reads CustomTextTypes from the
1219
+ * theme module, so both classification and declaration emission must resolve
1220
+ * this same public augmentation point.
1221
+ *
1222
+ * @type {Record<string, {moduleName: string, interfaceName: string, includeTypes?: boolean}>}
1223
+ */
1224
+ const AUGMENTATION_OVERRIDES = {
1225
+ 'text.type': {
1226
+ moduleName: 'theme',
1227
+ interfaceName: 'CustomTextTypes',
1228
+ includeTypes: true,
1229
+ },
1230
+ };
1231
+
1232
+ /**
1233
+ * Resolve the public module/interface that can widen a component prop value.
1234
+ * @param {string} component
1235
+ * @param {string} prop
1236
+ * @returns {Promise<{moduleName: string, interfaceName: string}|null>}
1237
+ */
1238
+ async function resolveAugmentationTarget(component, prop) {
1239
+ const override = AUGMENTATION_OVERRIDES[`${component}.${prop}`];
1240
+ if (override) {
1241
+ return componentHasAugmentableInterface(
1242
+ override.moduleName,
1243
+ override.interfaceName,
1244
+ {includeTypes: override.includeTypes},
1245
+ )
1246
+ ? {moduleName: override.moduleName, interfaceName: override.interfaceName}
1247
+ : null;
1248
+ }
1249
+
1250
+ const propPascal = prop.charAt(0).toUpperCase() + prop.slice(1);
1251
+ const target = (await resolveAugmentationTargetCandidates(component)).find(
1252
+ candidate =>
1253
+ isComponentValueAugmentationPoint(
1254
+ candidate.moduleName,
1255
+ `${candidate.interfacePrefix}${propPascal}Map`,
1256
+ ),
1257
+ );
1258
+ return target
1259
+ ? {
1260
+ moduleName: target.moduleName,
1261
+ interfaceName: `${target.interfacePrefix}${propPascal}Map`,
1262
+ }
1263
+ : null;
1264
+ }
1265
+
1202
1266
  /**
1203
1267
  * Adaptation values from resolved runtime rules, raw input, or normalized built
1204
1268
  * metadata. Built modules intentionally omit `__adaptationRules`, so every CLI
@@ -1503,14 +1567,7 @@ async function classifyComponentValue(component, prop, value) {
1503
1567
  return 'builtin';
1504
1568
  }
1505
1569
 
1506
- const propPascal = prop.charAt(0).toUpperCase() + prop.slice(1);
1507
- const target = (await resolveAugmentationTargetCandidates(component)).find(
1508
- candidate =>
1509
- isComponentValueAugmentationPoint(
1510
- candidate.moduleName,
1511
- `${candidate.interfacePrefix}${propPascal}Map`,
1512
- ),
1513
- );
1570
+ const target = await resolveAugmentationTarget(component, prop);
1514
1571
  return target ? 'enrollment' : 'unresolved';
1515
1572
  }
1516
1573
 
@@ -1668,15 +1725,7 @@ async function generateVariantDeclarationsAsync(themeDef) {
1668
1725
  for (const [prop, values] of Object.entries(props)) {
1669
1726
  if (values.size === 0) continue;
1670
1727
 
1671
- const propPascal = prop.charAt(0).toUpperCase() + prop.slice(1);
1672
- const target = (
1673
- await resolveAugmentationTargetCandidates(component)
1674
- ).find(candidate =>
1675
- isComponentValueAugmentationPoint(
1676
- candidate.moduleName,
1677
- `${candidate.interfacePrefix}${propPascal}Map`,
1678
- ),
1679
- );
1728
+ const target = await resolveAugmentationTarget(component, prop);
1680
1729
 
1681
1730
  // Resolve the augmentation point again to name it. Classification has
1682
1731
  // already established that one exists (that is what `enrollment` means),
@@ -1687,7 +1736,7 @@ async function generateVariantDeclarationsAsync(themeDef) {
1687
1736
  if (!target) continue;
1688
1737
 
1689
1738
  const modulePath = `@astryxdesign/core/${target.moduleName}`;
1690
- const interfaceName = `${target.interfacePrefix}${propPascal}Map`;
1739
+ const {interfaceName} = target;
1691
1740
 
1692
1741
  sections.push(`declare module '${modulePath}' {`);
1693
1742
  sections.push(` interface ${interfaceName} {`);
@@ -2120,7 +2169,7 @@ export async function validateThemePrivateInputs(
2120
2169
  * import { defaultIconRegistry } from './icons';
2121
2170
  * icons: defaultIconRegistry,
2122
2171
  * @param {string} filePath
2123
- * @param {'icons' | 'indicators'} field
2172
+ * @param {'icons' | 'indicators' | 'componentIcons'} field
2124
2173
  * @returns {{exportName: string, importPath: string} | null}
2125
2174
  */
2126
2175
  function extractRegistryInfo(filePath, field) {
@@ -2198,6 +2247,83 @@ function iconNodesExpression(entries, entriesName, nodesName) {
2198
2247
  })()`;
2199
2248
  }
2200
2249
 
2250
+ /**
2251
+ * A theme's component icon slot values: a theme from defineTheme keeps them
2252
+ * in `__componentIcons` when any is a function (its `componentIcons` is the
2253
+ * static view); otherwise `componentIcons` holds them.
2254
+ * @param {any} theme
2255
+ * @returns {Record<string, unknown> | undefined}
2256
+ */
2257
+ function themeComponentIcons(theme) {
2258
+ return theme?.__componentIcons ?? theme?.componentIcons;
2259
+ }
2260
+
2261
+ /**
2262
+ * The generated function that merges a theme's own slot values over its
2263
+ * base's, as `defineTheme` does: an own slot replaces the inherited one whole,
2264
+ * and an `undefined` own slot leaves it in place. It reads no global.
2265
+ * @param {string} name - A reserved function name.
2266
+ * @returns {string}
2267
+ */
2268
+ function componentIconMergeFunction(name) {
2269
+ return `function ${name}(base, own) {
2270
+ const merged = {...base};
2271
+ for (const slot in own) {
2272
+ if (own[slot] !== undefined) merged[slot] = own[slot];
2273
+ }
2274
+ return merged;
2275
+ }
2276
+ `;
2277
+ }
2278
+
2279
+ /**
2280
+ * The generated code that reads the static view of a theme's slot values:
2281
+ * every slot without a function. Like the icon node view, it reads no global
2282
+ * and declares only reserved local names.
2283
+ * @param {string} valuesName - A reserved local name bound to the values.
2284
+ * @param {string} viewName - A reserved local name.
2285
+ * @returns {string}
2286
+ */
2287
+ function componentIconViewExpression(valuesName, viewName) {
2288
+ return `(() => {
2289
+ const ${viewName} = {};
2290
+ for (const slot in ${valuesName}) {
2291
+ const value = ${valuesName}[slot];
2292
+ if (value !== undefined && typeof value !== 'function') ${viewName}[slot] = value;
2293
+ }
2294
+ return ${viewName};
2295
+ })()`;
2296
+ }
2297
+
2298
+ /**
2299
+ * One import statement for a carried registry or slot binding. Ordinary named
2300
+ * imports stay byte-compatible; paths with quotes or escapes are encoded, and
2301
+ * default, namespace, and aliased bindings are kept.
2302
+ * @param {{importPath: string, importedName: string, localName: string}} binding
2303
+ * @param {string} [specifier] - Replaces the import path when given.
2304
+ * @returns {string}
2305
+ */
2306
+ function importStatement({importPath, importedName, localName}, specifier) {
2307
+ const source =
2308
+ specifier !== undefined
2309
+ ? JSON.stringify(specifier)
2310
+ : /['\\\r\n]/u.test(importPath)
2311
+ ? JSON.stringify(importPath)
2312
+ : `'${importPath}'`;
2313
+ const imported = /^[$\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(
2314
+ importedName,
2315
+ )
2316
+ ? importedName
2317
+ : JSON.stringify(importedName);
2318
+ const binding =
2319
+ importedName === 'default'
2320
+ ? localName
2321
+ : importedName === '*'
2322
+ ? `* as ${localName}`
2323
+ : `{ ${imported}${importedName === localName ? '' : ` as ${localName}`} }`;
2324
+ return `import ${binding} from ${source};\n`;
2325
+ }
2326
+
2201
2327
  /**
2202
2328
  * Generate a minimal JS module for a built theme.
2203
2329
  * Includes the theme name, marker, and re-exports the icon registry.
@@ -2224,10 +2350,15 @@ function iconNodesExpression(entries, entriesName, nodesName) {
2224
2350
  * renders, and the theme's `icons` and the re-exported registry are the node
2225
2351
  * view, each function read at `{size: 'md'}` as `DefinedTheme['icons']` is.
2226
2352
  *
2353
+ * Component icon slots without a function are written as JSON. When a slot
2354
+ * value is a function, the module keeps the values, carried through
2355
+ * `componentIconInfo`'s imports, as `__componentIcons`, and the theme's
2356
+ * `componentIcons` is the static view, as `DefinedTheme['componentIcons']` is.
2357
+ *
2227
2358
  * @param {any} themeDef
2228
2359
  * @param {import('./icon-imports.mjs').IconImports | null} iconInfo
2229
2360
  * @param {string} [iconsSpecifier] - Overrides the selected registry specifier.
2230
- * @param {{themeBinding?: string, iconsExpression?: string, indicatorsExpression?: string, artifactBaseName?: string, exportIcons?: boolean}} [moduleOptions]
2361
+ * @param {{themeBinding?: string, iconsExpression?: string, indicatorsExpression?: string, artifactBaseName?: string, exportIcons?: boolean, componentIconInfo?: {imports: import('./icon-imports.mjs').IconImports['imports'], expression: string, mergeName?: string} | null}} [moduleOptions]
2231
2362
  * @returns {string}
2232
2363
  */
2233
2364
  function generateBuiltModule(
@@ -2240,32 +2371,17 @@ function generateBuiltModule(
2240
2371
  moduleOptions.themeBinding ?? `${toIdentifier(themeDef.name)}Theme`;
2241
2372
  const artifactBaseName = moduleOptions.artifactBaseName ?? themeDef.name;
2242
2373
  const exportIcons = moduleOptions.exportIcons ?? true;
2243
- // Keep ordinary direct named imports byte-compatible, while encoding paths
2244
- // with quotes/escapes and retaining default, namespace, and aliased bindings.
2245
2374
  const iconImport = (iconInfo?.imports ?? [])
2246
- .map(({importPath, importedName, localName}) => {
2247
- const overridden =
2375
+ .map(binding =>
2376
+ importStatement(
2377
+ binding,
2248
2378
  iconsSpecifier !== undefined &&
2249
- importPath === iconInfo?.iconsSpecifierImportPath &&
2250
- localName === iconInfo?.iconsSpecifierLocalName;
2251
- const specifier = overridden
2252
- ? JSON.stringify(iconsSpecifier)
2253
- : /['\\\r\n]/u.test(importPath)
2254
- ? JSON.stringify(importPath)
2255
- : `'${importPath}'`;
2256
- const imported = /^[$\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(
2257
- importedName,
2258
- )
2259
- ? importedName
2260
- : JSON.stringify(importedName);
2261
- const binding =
2262
- importedName === 'default'
2263
- ? localName
2264
- : importedName === '*'
2265
- ? `* as ${localName}`
2266
- : `{ ${imported}${importedName === localName ? '' : ` as ${localName}`} }`;
2267
- return `import ${binding} from ${specifier};\n`;
2268
- })
2379
+ binding.importPath === iconInfo?.iconsSpecifierImportPath &&
2380
+ binding.localName === iconInfo?.iconsSpecifierLocalName
2381
+ ? iconsSpecifier
2382
+ : undefined,
2383
+ ),
2384
+ )
2269
2385
  .join('');
2270
2386
  const iconDeclaration =
2271
2387
  iconInfo && iconInfo.expression !== iconInfo.exportName
@@ -2295,6 +2411,29 @@ function generateBuiltModule(
2295
2411
  : `\nexport { ${iconInfo.exportName} };\n`
2296
2412
  : '';
2297
2413
 
2414
+ const componentIconValues = themeComponentIcons(themeDef);
2415
+ const componentIconInfo = moduleOptions.componentIconInfo ?? null;
2416
+ const carriesSlotFunctions = hasFunctionIconEntry(componentIconValues);
2417
+ if (carriesSlotFunctions && !componentIconInfo) {
2418
+ throw new AstryxError(
2419
+ `Theme "${themeDef.name}" has componentIcons slot functions that the build cannot carry. ` +
2420
+ 'Import the slot functions from their own module into the theme file.',
2421
+ undefined,
2422
+ ERROR_CODES.ERR_THEME_INVALID,
2423
+ );
2424
+ }
2425
+ const componentIconValuesName = `${themeBinding}ComponentIconValues`;
2426
+ const componentIconPrelude =
2427
+ carriesSlotFunctions && componentIconInfo
2428
+ ? componentIconInfo.imports
2429
+ .map(binding => importStatement(binding))
2430
+ .join('') +
2431
+ (componentIconInfo.mergeName
2432
+ ? componentIconMergeFunction(componentIconInfo.mergeName)
2433
+ : '') +
2434
+ `const ${componentIconValuesName} = ${componentIconInfo.expression};\n`
2435
+ : '';
2436
+
2298
2437
  // Resolve token values — tuples become light-dark() strings
2299
2438
  /** @type {Record<string, unknown>} */
2300
2439
  const resolvedTokens = {};
@@ -2365,9 +2504,18 @@ function generateBuiltModule(
2365
2504
  list ?? [],
2366
2505
  ]),
2367
2506
  ),
2368
- );
2507
+ ) +
2508
+ (carriesSlotFunctions
2509
+ ? ` componentIcons: ${componentIconViewExpression(
2510
+ componentIconValuesName,
2511
+ `${themeBinding}ComponentIconView`,
2512
+ )
2513
+ .split('\n')
2514
+ .map((line, i) => (i === 0 ? line : ' ' + line))
2515
+ .join('\n')},\n __componentIcons: ${componentIconValuesName},\n`
2516
+ : serializeField('componentIcons', componentIconValues, true));
2369
2517
 
2370
- return `${iconImport}${iconDeclaration}${iconNodesDeclaration}/**
2518
+ return `${iconImport}${componentIconPrelude}${iconDeclaration}${iconNodesDeclaration}/**
2371
2519
  * ${themeDef.name} theme — built by \`${getCliInvocation()} theme build\`
2372
2520
  * Import the CSS file alongside this module:
2373
2521
  *
@@ -2955,13 +3103,20 @@ async function themeBuildInternal(
2955
3103
  ]
2956
3104
  : []),
2957
3105
  ];
3106
+ // Names the slot values, their static view, and their merge function take
3107
+ // in the built module.
3108
+ const componentIconNames = [
3109
+ `${toIdentifier(themeDef.name)}ThemeComponentIconValues`,
3110
+ `${toIdentifier(themeDef.name)}ThemeComponentIconView`,
3111
+ `${toIdentifier(themeDef.name)}ThemeMergeComponentIcons`,
3112
+ ];
2958
3113
  const iconResolution = {
2959
3114
  readSource: (/** @type {string} */ file) =>
2960
3115
  path.resolve(file) === path.resolve(filePath)
2961
3116
  ? themeSource
2962
3117
  : fs.readFileSync(file, 'utf8'),
2963
3118
  resolveModule: resolveThemeModule,
2964
- reservedNames: reservedNames(functionEntries),
3119
+ reservedNames: [...reservedNames(functionEntries), ...componentIconNames],
2965
3120
  functionEntries,
2966
3121
  rawInput: 'extends' in themeDef,
2967
3122
  hasIcons:
@@ -2971,6 +3126,8 @@ async function themeBuildInternal(
2971
3126
  let iconInfo = standaloneIcons
2972
3127
  ? await resolveIconImports(filePath, themeExportName, iconResolution)
2973
3128
  : null;
3129
+ /** @type {import('./icon-imports.mjs').IconImports | null} */
3130
+ let componentIconInfo = null;
2974
3131
 
2975
3132
  // Validate component overrides
2976
3133
  const warnings = await validateComponentOverrides(themeDef);
@@ -3093,7 +3250,10 @@ async function themeBuildInternal(
3093
3250
  ...iconResolution,
3094
3251
  hasIcons: true,
3095
3252
  functionEntries: resolvedFunctionEntries,
3096
- reservedNames: reservedNames(resolvedFunctionEntries),
3253
+ reservedNames: [
3254
+ ...reservedNames(resolvedFunctionEntries),
3255
+ ...componentIconNames,
3256
+ ],
3097
3257
  });
3098
3258
  }
3099
3259
  // An opaque package base can legitimately have no icons. Its .icons
@@ -3118,6 +3278,27 @@ async function themeBuildInternal(
3118
3278
  );
3119
3279
  }
3120
3280
 
3281
+ // A slot function cannot be serialized, so the module imports it from
3282
+ // where the theme source does. Static slot values are written as JSON.
3283
+ if (
3284
+ standaloneIcons &&
3285
+ hasFunctionIconEntry(themeComponentIcons(resolvedTheme))
3286
+ ) {
3287
+ componentIconInfo = await resolveIconImports(filePath, themeExportName, {
3288
+ ...iconResolution,
3289
+ field: 'componentIcons',
3290
+ mergeName: `${toIdentifier(themeDef.name)}ThemeMergeComponentIcons`,
3291
+ hasIcons: true,
3292
+ functionEntries: true,
3293
+ reservedNames: [
3294
+ ...reservedNames(true),
3295
+ ...componentIconNames,
3296
+ ...(iconInfo?.imports.map(binding => binding.localName) ?? []),
3297
+ ...(iconInfo ? [iconInfo.exportName] : []),
3298
+ ],
3299
+ });
3300
+ }
3301
+
3121
3302
  // Cores that predate adaptations can still resolve typography, color,
3122
3303
  // radius, and motion into root tokens, but they do not retain the raw axis
3123
3304
  // inputs. Keep the effective metadata captured from the same defineTheme
@@ -3347,13 +3528,17 @@ async function themeBuildInternal(
3347
3528
  const cssDtsContent =
3348
3529
  generatedHeader(sourceRelative, 'ts', buildCommand, versions) +
3349
3530
  'export {};\n';
3350
- const jsContent =
3351
- generatedHeader(sourceRelative, 'js', buildCommand, versions) +
3352
- generateBuiltModule(
3353
- resolvedTheme || themeDef,
3354
- iconInfo,
3355
- options.iconsSpecifier,
3356
- );
3531
+ // A family member's module is generated by themeBuildFamily, which carries
3532
+ // its registries and slot functions through the family's own imports.
3533
+ const jsContent = options.__prepareFamily
3534
+ ? ''
3535
+ : generatedHeader(sourceRelative, 'js', buildCommand, versions) +
3536
+ generateBuiltModule(
3537
+ resolvedTheme || themeDef,
3538
+ iconInfo,
3539
+ options.iconsSpecifier,
3540
+ {componentIconInfo},
3541
+ );
3357
3542
  const dtsContent =
3358
3543
  generatedHeader(sourceRelative, 'ts', buildCommand, versions) +
3359
3544
  generateBuiltTypes(resolvedTheme || themeDef, iconInfo, variantsFileName);
@@ -3376,6 +3561,7 @@ async function themeBuildInternal(
3376
3561
  sourceParent,
3377
3562
  iconInfo: extractRegistryInfo(filePath, 'icons'),
3378
3563
  indicatorInfo: extractRegistryInfo(filePath, 'indicators'),
3564
+ componentIconInfo: extractRegistryInfo(filePath, 'componentIcons'),
3379
3565
  variantDecl,
3380
3566
  css: cssPlan,
3381
3567
  versions,
@@ -3695,6 +3881,81 @@ function allocateFamilyBindings(members) {
3695
3881
  const used = new Map();
3696
3882
  return new Map(members.map(member => { const base = familyThemeBinding(member.theme.name), count = used.get(base) ?? 0; used.set(base, count + 1); return [member.theme.name, count === 0 ? base : `${base}${count + 1}`]; }));
3697
3883
  }
3884
+ /**
3885
+ * How a family member's module carries slot functions: its parent's values,
3886
+ * then its own. A member supplies its own slot functions through a named
3887
+ * import of its whole `componentIcons` map, merged as `defineTheme` merges;
3888
+ * own static values are written as JSON. Null when the member has no slot
3889
+ * function, so its static values are written as JSON alone.
3890
+ * @param {any} member
3891
+ * @param {any} parent - The member's parent in the family, if any.
3892
+ * @param {{themeBinding: string | undefined, parentBinding: string | null | undefined, outDir: string, registryImports: Array<{specifier: string, exportName: string}>, expression: (own: string | null, inherited: string | null) => string | undefined}} context
3893
+ * @returns {{imports: Array<{importPath: string, importedName: string, localName: string}>, expression: string, mergeName?: string} | null}
3894
+ */
3895
+ function familyComponentIcons(member, parent, context) {
3896
+ const values = themeComponentIcons(member.theme);
3897
+ if (!hasFunctionIconEntry(values)) return null;
3898
+ const inheritedValues = themeComponentIcons(parent?.theme);
3899
+ const inherited =
3900
+ context.parentBinding && inheritedValues
3901
+ ? `${context.parentBinding}.${hasFunctionIconEntry(inheritedValues) ? '__componentIcons' : 'componentIcons'}`
3902
+ : null;
3903
+ /** @type {Array<{importPath: string, importedName: string, localName: string}>} */
3904
+ const imports = [];
3905
+ let own = null;
3906
+ const binding =
3907
+ context.themeBinding ?? `${toIdentifier(member.theme.name)}Theme`;
3908
+ const info = member.componentIconInfo;
3909
+ if (info) {
3910
+ const input = familyRegistryInput(
3911
+ info,
3912
+ member.filePath,
3913
+ context.outDir,
3914
+ undefined,
3915
+ );
3916
+ const importPath = input.specifier ?? info.importPath;
3917
+ own = `${binding}ComponentIconSource`;
3918
+ imports.push({importPath, importedName: info.exportName, localName: own});
3919
+ context.registryImports.push({
3920
+ specifier: importPath,
3921
+ exportName: info.exportName,
3922
+ });
3923
+ } else {
3924
+ // Without an import, a member's own slots are the ones that differ from
3925
+ // its parent's; they must all be static.
3926
+ /** @type {Record<string, unknown>} */
3927
+ const ownValues = {};
3928
+ for (const [slot, value] of Object.entries(values ?? {})) {
3929
+ if (
3930
+ inheritedValues &&
3931
+ Object.hasOwn(inheritedValues, slot) &&
3932
+ inheritedValues[slot] === value
3933
+ )
3934
+ continue;
3935
+ if (typeof value === 'function') {
3936
+ throw new AstryxError(
3937
+ `Theme "${member.theme.name}" must supply its componentIcons slot functions through a named import of its whole componentIcons map for family output.`,
3938
+ undefined,
3939
+ ERROR_CODES.ERR_THEME_INVALID,
3940
+ );
3941
+ }
3942
+ ownValues[slot] = value;
3943
+ }
3944
+ if (Object.keys(ownValues).length > 0) own = JSON.stringify(ownValues);
3945
+ }
3946
+ if (info) {
3947
+ // An imported map can hold an `undefined` slot: it keeps the parent's,
3948
+ // and a root drops it, as `defineTheme` does.
3949
+ const mergeName = `${binding}MergeComponentIcons`;
3950
+ return {
3951
+ imports,
3952
+ expression: `${mergeName}(${inherited ?? '{}'}, ${own})`,
3953
+ mergeName,
3954
+ };
3955
+ }
3956
+ const expression = context.expression(own, inherited);
3957
+ return expression ? {imports, expression} : null;
3958
+ }
3698
3959
  /** Rebase a scraped relative registry import to the aggregate module. @param {any} info @param {string} filePath @param {string} outDir @param {string | undefined} override */
3699
3960
  // prettier-ignore
3700
3961
  function familyRegistryInput(info, filePath, outDir, override) {
@@ -3907,6 +4168,13 @@ export async function themeBuildFamily(
3907
4168
  // rebased or overridden specifier replaces the scraped one.
3908
4169
  // prettier-ignore
3909
4170
  const iconImports = icon.info && ownIcons ? {imports: [{importPath: icon.info.importPath, importedName: icon.info.exportName, localName: ownIcons}], expression: ownIcons, exportName: ownIcons, iconsSpecifierImportPath: icon.info.importPath, iconsSpecifierLocalName: ownIcons} : null;
4171
+ const componentIconInfo = familyComponentIcons(member, parent, {
4172
+ themeBinding,
4173
+ parentBinding,
4174
+ outDir,
4175
+ registryImports,
4176
+ expression,
4177
+ });
3910
4178
  return (
3911
4179
  indicatorImport +
3912
4180
  generateBuiltModule(member.theme, iconImports, icon.specifier, {
@@ -3915,6 +4183,7 @@ export async function themeBuildFamily(
3915
4183
  indicatorsExpression: expression(ownIndicators, inheritedIndicators),
3916
4184
  artifactBaseName: baseName,
3917
4185
  exportIcons: false,
4186
+ componentIconInfo,
3918
4187
  })
3919
4188
  );
3920
4189
  })
@@ -10,14 +10,23 @@
10
10
  * base is read for its entries (`__icons`) as well as its node-valued `icons`,
11
11
  * and a built base module's `__icons` field is followed instead of `icons`.
12
12
  *
13
+ * With `field: 'componentIcons'`, the same tracing carries a theme's slot
14
+ * values: a slot function must come from an import, and an inline object may
15
+ * also hold static values (names, `null`, requests), written as JSON. A built
16
+ * base module is read through its export, like a package theme. A theme's own
17
+ * slots merge over its base's through `mergeName`, which skips an
18
+ * `undefined` slot as `defineTheme` does.
19
+ *
13
20
  * @param {string} filePath
14
21
  * @param {string | undefined} exportName
15
- * @param {{hasIcons: boolean, functionEntries?: boolean, readSource: (filename: string) => string, resolveModule: (specifier: string, fromFile: string) => string | Promise<string>, reservedNames?: string[], rawInput?: boolean}} options
22
+ * @param {{hasIcons: boolean, functionEntries?: boolean, field?: 'icons' | 'componentIcons', mergeName?: string, readSource: (filename: string) => string, resolveModule: (specifier: string, fromFile: string) => string | Promise<string>, reservedNames?: string[], rawInput?: boolean}} options
16
23
  * @returns {Promise<IconImports | null>}
17
24
  */
18
- export function resolveIconImports(filePath: string, exportName: string | undefined, { hasIcons, functionEntries, readSource, resolveModule, reservedNames, rawInput, }: {
25
+ export function resolveIconImports(filePath: string, exportName: string | undefined, { hasIcons, functionEntries, field, mergeName, readSource, resolveModule, reservedNames, rawInput, }: {
19
26
  hasIcons: boolean;
20
27
  functionEntries?: boolean;
28
+ field?: "icons" | "componentIcons";
29
+ mergeName?: string;
21
30
  readSource: (filename: string) => string;
22
31
  resolveModule: (specifier: string, fromFile: string) => string | Promise<string>;
23
32
  reservedNames?: string[];
@@ -33,6 +42,11 @@ export type IconImports = {
33
42
  exportName: string;
34
43
  iconsSpecifierImportPath?: string | undefined;
35
44
  iconsSpecifierLocalName?: string | undefined;
45
+ /**
46
+ * - Set when `expression` calls the slot merge
47
+ * function, which the module must declare under this name.
48
+ */
49
+ mergeName?: string | undefined;
36
50
  };
37
51
  export type RegistryReference = {
38
52
  kind: "reference";
@@ -41,8 +55,21 @@ export type RegistryReference = {
41
55
  members: string[];
42
56
  };
43
57
  /**
44
- * `entries` marks a package theme read for its icon entries: its node-valued
45
- * `icons` merged with the `__icons` that hold its function entries, if any.
58
+ * A `merge` with `theme` set merges a theme's own values over its base's;
59
+ * any other merge is a spread inside one object.
60
+ */
61
+ export type SlotData = {
62
+ json: unknown;
63
+ } | {
64
+ ref: RegistryReference;
65
+ } | {
66
+ object: Array<[string, SlotData]>;
67
+ };
68
+ /**
69
+ * `entries` marks a package theme read for its entries: its public field
70
+ * (node-valued `icons`, or the static `componentIcons` view) merged with the
71
+ * `__` field that holds its functions, if any. `object` is an inline
72
+ * `componentIcons` object: each slot is a static value or an imported one.
46
73
  */
47
74
  export type RegistryPlan = {
48
75
  kind: "registry";
@@ -54,4 +81,8 @@ export type RegistryPlan = {
54
81
  kind: "merge";
55
82
  base: RegistryPlan;
56
83
  own: RegistryPlan;
84
+ theme?: boolean;
85
+ } | {
86
+ kind: "object";
87
+ entries: Array<[string, SlotData]>;
57
88
  };