@pithyjs/vite-plugin 0.2.0-beta.0 → 0.2.0-beta.10

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 (116) hide show
  1. package/dist/aotCodegen/generateBindings.d.ts +1 -1
  2. package/dist/aotCodegen/generateBindings.d.ts.map +1 -1
  3. package/dist/aotCodegen/generateBindings.js +4 -2
  4. package/dist/aotCodegen/generateBindings.js.map +1 -1
  5. package/dist/aotCodegen/generateDomFactory.d.ts +2 -1
  6. package/dist/aotCodegen/generateDomFactory.d.ts.map +1 -1
  7. package/dist/aotCodegen/generateDomFactory.js +48 -18
  8. package/dist/aotCodegen/generateDomFactory.js.map +1 -1
  9. package/dist/aotCodegen/index.d.ts +2 -2
  10. package/dist/aotCodegen/index.js +1 -1
  11. package/dist/aotCodegen/index.js.map +1 -1
  12. package/dist/aotCodegen/serializeNode.d.ts +1 -1
  13. package/dist/aotCodegen/serializeNode.d.ts.map +1 -1
  14. package/dist/aotCodegen/serializeNode.js +2 -3
  15. package/dist/aotCodegen/serializeNode.js.map +1 -1
  16. package/dist/aotCodegen/structuralCodegen.d.ts +2 -2
  17. package/dist/aotCodegen/structuralCodegen.d.ts.map +1 -1
  18. package/dist/aotCodegen/structuralCodegen.js +2 -2
  19. package/dist/aotCodegen/structuralCodegen.js.map +1 -1
  20. package/dist/aotCodegen/utils.js.map +1 -1
  21. package/dist/babelPlugin/babelPlugin.d.ts +1 -1
  22. package/dist/babelPlugin/babelPlugin.d.ts.map +1 -1
  23. package/dist/babelPlugin/babelPlugin.js +140 -7
  24. package/dist/babelPlugin/babelPlugin.js.map +1 -1
  25. package/dist/babelPlugin/extractUsedIdentifiers.d.ts +13 -3
  26. package/dist/babelPlugin/extractUsedIdentifiers.d.ts.map +1 -1
  27. package/dist/babelPlugin/extractUsedIdentifiers.js +83 -48
  28. package/dist/babelPlugin/extractUsedIdentifiers.js.map +1 -1
  29. package/dist/buildPlugin/plugin.js.map +1 -1
  30. package/dist/designSystemPlugin/channelVars.js.map +1 -1
  31. package/dist/designSystemPlugin/collector.d.ts +1 -1
  32. package/dist/designSystemPlugin/collector.js +1 -1
  33. package/dist/designSystemPlugin/collector.js.map +1 -1
  34. package/dist/designSystemPlugin/cssTokens.d.ts +1 -1
  35. package/dist/designSystemPlugin/cssTokens.js +1 -1
  36. package/dist/designSystemPlugin/cssTokens.js.map +1 -1
  37. package/dist/designSystemPlugin/enumerate.d.ts.map +1 -1
  38. package/dist/designSystemPlugin/enumerate.js +13 -13
  39. package/dist/designSystemPlugin/enumerate.js.map +1 -1
  40. package/dist/designSystemPlugin/extractor.d.ts +1 -1
  41. package/dist/designSystemPlugin/extractor.js.map +1 -1
  42. package/dist/designSystemPlugin/generator.d.ts +1 -1
  43. package/dist/designSystemPlugin/generator.js +3 -3
  44. package/dist/designSystemPlugin/generator.js.map +1 -1
  45. package/dist/designSystemPlugin/index.d.ts +2 -2
  46. package/dist/designSystemPlugin/index.js +1 -1
  47. package/dist/designSystemPlugin/index.js.map +1 -1
  48. package/dist/designSystemPlugin/patterns/a11y.d.ts +1 -1
  49. package/dist/designSystemPlugin/patterns/a11y.js.map +1 -1
  50. package/dist/designSystemPlugin/patterns/borders.d.ts +1 -1
  51. package/dist/designSystemPlugin/patterns/borders.js +1 -1
  52. package/dist/designSystemPlugin/patterns/borders.js.map +1 -1
  53. package/dist/designSystemPlugin/patterns/colors.d.ts +1 -1
  54. package/dist/designSystemPlugin/patterns/colors.js +2 -2
  55. package/dist/designSystemPlugin/patterns/colors.js.map +1 -1
  56. package/dist/designSystemPlugin/patterns/iconography.d.ts +1 -1
  57. package/dist/designSystemPlugin/patterns/iconography.js +1 -1
  58. package/dist/designSystemPlugin/patterns/iconography.js.map +1 -1
  59. package/dist/designSystemPlugin/patterns/index.d.ts +1 -1
  60. package/dist/designSystemPlugin/patterns/index.js +11 -11
  61. package/dist/designSystemPlugin/patterns/index.js.map +1 -1
  62. package/dist/designSystemPlugin/patterns/layout.d.ts +1 -1
  63. package/dist/designSystemPlugin/patterns/layout.js +1 -1
  64. package/dist/designSystemPlugin/patterns/layout.js.map +1 -1
  65. package/dist/designSystemPlugin/patterns/motion.d.ts +1 -1
  66. package/dist/designSystemPlugin/patterns/motion.js +1 -1
  67. package/dist/designSystemPlugin/patterns/motion.js.map +1 -1
  68. package/dist/designSystemPlugin/patterns/radius.d.ts +1 -1
  69. package/dist/designSystemPlugin/patterns/radius.js +1 -1
  70. package/dist/designSystemPlugin/patterns/radius.js.map +1 -1
  71. package/dist/designSystemPlugin/patterns/shadows.d.ts +1 -1
  72. package/dist/designSystemPlugin/patterns/shadows.d.ts.map +1 -1
  73. package/dist/designSystemPlugin/patterns/shadows.js +1 -1
  74. package/dist/designSystemPlugin/patterns/shadows.js.map +1 -1
  75. package/dist/designSystemPlugin/patterns/smartContainer.d.ts +1 -1
  76. package/dist/designSystemPlugin/patterns/smartContainer.js +1 -1
  77. package/dist/designSystemPlugin/patterns/smartContainer.js.map +1 -1
  78. package/dist/designSystemPlugin/patterns/spacing.d.ts +1 -1
  79. package/dist/designSystemPlugin/patterns/spacing.js +1 -1
  80. package/dist/designSystemPlugin/patterns/spacing.js.map +1 -1
  81. package/dist/designSystemPlugin/patterns/typography.d.ts +1 -1
  82. package/dist/designSystemPlugin/patterns/typography.js +1 -1
  83. package/dist/designSystemPlugin/patterns/typography.js.map +1 -1
  84. package/dist/designSystemPlugin/plugin.d.ts +11 -1
  85. package/dist/designSystemPlugin/plugin.d.ts.map +1 -1
  86. package/dist/designSystemPlugin/plugin.js +421 -55
  87. package/dist/designSystemPlugin/plugin.js.map +1 -1
  88. package/dist/designSystemPlugin/types.d.ts +8 -2
  89. package/dist/designSystemPlugin/types.d.ts.map +1 -1
  90. package/dist/designSystemPlugin/types.js.map +1 -1
  91. package/dist/htmlTransform/htmlTransform.d.ts +1 -1
  92. package/dist/htmlTransform/htmlTransform.d.ts.map +1 -1
  93. package/dist/htmlTransform/htmlTransform.js +5 -2
  94. package/dist/htmlTransform/htmlTransform.js.map +1 -1
  95. package/dist/iconsPlugin/index.d.ts +2 -2
  96. package/dist/iconsPlugin/index.js +1 -1
  97. package/dist/iconsPlugin/index.js.map +1 -1
  98. package/dist/iconsPlugin/optimize.js.map +1 -1
  99. package/dist/iconsPlugin/plugin.d.ts +1 -1
  100. package/dist/iconsPlugin/plugin.js +2 -2
  101. package/dist/iconsPlugin/plugin.js.map +1 -1
  102. package/dist/iconsPlugin/types.js.map +1 -1
  103. package/dist/index.d.ts +8 -8
  104. package/dist/index.js +7 -7
  105. package/dist/index.js.map +1 -1
  106. package/dist/pithyPlugin/plugin.d.ts.map +1 -1
  107. package/dist/pithyPlugin/plugin.js +1 -8
  108. package/dist/pithyPlugin/plugin.js.map +1 -1
  109. package/dist/routerPlugin/manifest.js.map +1 -1
  110. package/dist/routerPlugin/plugin.js +2 -2
  111. package/dist/routerPlugin/plugin.js.map +1 -1
  112. package/dist/routerPlugin/scanner.js.map +1 -1
  113. package/dist/ssrPlugin/plugin.d.ts.map +1 -1
  114. package/dist/ssrPlugin/plugin.js +26 -21
  115. package/dist/ssrPlugin/plugin.js.map +1 -1
  116. package/package.json +15 -9
@@ -14,7 +14,7 @@
14
14
  }
15
15
  */
16
16
  import type { Plugin } from 'vite';
17
- import { type DesignSystemPluginOptions } from './types';
17
+ import { type DesignSystemPluginOptions } from './types.js';
18
18
  /**
19
19
  * Vite plugin for Pithy Design System CSS tree-shaking
20
20
  *
@@ -69,4 +69,14 @@ import { type DesignSystemPluginOptions } from './types';
69
69
  */
70
70
  /** @codexApi {"parent":"pithy.vite-plugin-design-system","name":"designSystemPlugin","stability":"experimental","signature":"(options?: DesignSystemPluginOptions) => Plugin"} */
71
71
  export declare function designSystemPlugin(options?: DesignSystemPluginOptions): Plugin;
72
+ /**
73
+ * Strip a subpath suffix off an alias replacement, yielding the package root.
74
+ * Returns undefined when the replacement does not end with the subpath, or
75
+ * when the peel is not absolute.
76
+ *
77
+ * Separator-insensitive: `sub` comes from an alias `find` and always uses `/`,
78
+ * while `replacement` comes from `path.resolve`. Comparing them raw made the
79
+ * peel never fire on Windows, and CI has no Windows job.
80
+ */
81
+ export declare function peelSubpath(replacement: string, sub: string): string | undefined;
72
82
  //# sourceMappingURL=plugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/designSystemPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF,OAAO,KAAK,EAAE,MAAM,EAAiC,MAAM,MAAM,CAAC;AAmBlE,OAAO,EAKL,KAAK,yBAAyB,EAC/B,MAAM,SAAS,CAAC;AAyEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,kLAAkL;AAClL,wBAAgB,kBAAkB,CAChC,OAAO,GAAE,yBAA8B,GACtC,MAAM,CAyjBR"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/designSystemPlugin/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AAEF,OAAO,KAAK,EAAE,MAAM,EAAiC,MAAM,MAAM,CAAC;AA2BlE,OAAO,EAKL,KAAK,yBAAyB,EAC/B,MAAM,SAAS,CAAC;AAgTjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,kLAAkL;AAClL,wBAAgB,kBAAkB,CAChC,OAAO,GAAE,yBAA8B,GACtC,MAAM,CAguBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,MAAM,GACV,MAAM,GAAG,SAAS,CAWpB"}
@@ -13,14 +13,14 @@
13
13
  }
14
14
  }
15
15
  */
16
- import { readFileSync, readdirSync, statSync } from 'fs';
17
- import { join, dirname } from 'path';
16
+ import { readFileSync, readdirSync, realpathSync, statSync } from 'fs';
17
+ import { join, dirname, extname, relative, isAbsolute, posix, win32, } from 'path';
18
18
  import { themeToCss, resolveTheme, designSystemToCss, } from '@pithyjs/design-system/palette';
19
- import { createCollector } from './collector';
20
- import { collectChannelVarRefs, createChannelVarGate, isChannelScanCandidate, resolveChannelScanRoots, scanChannelVarRefs, } from './channelVars';
21
- import { generateCSS } from './generator';
22
- import { enumerateAllUtilities } from './enumerate';
23
- import { VIRTUAL_MODULE_ID, RESOLVED_VIRTUAL_MODULE_ID, resolveOptions, defaultModeOf, } from './types';
19
+ import { createCollector } from './collector.js';
20
+ import { collectChannelVarRefs, createChannelVarGate, isChannelScanCandidate, resolveChannelScanRoots, scanChannelVarRefs, } from './channelVars.js';
21
+ import { generateCSS } from './generator.js';
22
+ import { enumerateAllUtilities } from './enumerate.js';
23
+ import { VIRTUAL_MODULE_ID, RESOLVED_VIRTUAL_MODULE_ID, resolveOptions, defaultModeOf, } from './types.js';
24
24
  /**
25
25
  * Format byte count to human-readable string
26
26
  */
@@ -30,12 +30,202 @@ function formatBytes(bytes) {
30
30
  const kb = bytes / 1024;
31
31
  return `${kb.toFixed(1)} KB`;
32
32
  }
33
+ /**
34
+ * Last-resort spelling of the same thing {@link registryExclusions} names exactly:
35
+ * for a path the resolver never produced, such as a `transform` id reaching
36
+ * in through a published install.
37
+ */
38
+ const UI_KIT_REGISTRY_PATH = /\/ui-kit\/(?:src|dist)\/registry\//;
39
+ /** The one directory inside ui-kit that holds generated modules. */
40
+ const REGISTRY_DIR_NAME = 'registry';
41
+ /** The package whose registry this plugin must not read. */
42
+ const UI_KIT_NAME = '@pithyjs/ui-kit';
43
+ /** Vite's default `resolve.extensions`, for a config that sets none. */
44
+ const RESOLVE_EXTENSIONS = [
45
+ '.mjs',
46
+ '.js',
47
+ '.mts',
48
+ '.ts',
49
+ '.jsx',
50
+ '.tsx',
51
+ '.json',
52
+ ];
53
+ /** A JS extension may be the OUTPUT name of a TypeScript source. */
54
+ const TS_SOURCE_EXTENSIONS = {
55
+ '.js': ['.ts', '.tsx'],
56
+ '.jsx': ['.tsx'],
57
+ '.mjs': ['.mts', '.mtsx'],
58
+ '.cjs': ['.cts', '.ctsx'],
59
+ };
60
+ /** `statSync`, or undefined for a path that does not exist. */
61
+ function statOf(p) {
62
+ try {
63
+ return statSync(p);
64
+ }
65
+ catch {
66
+ return undefined;
67
+ }
68
+ }
69
+ /** Vite's default `resolve.mainFields`, for a config that sets none. */
70
+ const MAIN_FIELDS = ['browser', 'module', 'jsnext:main', 'jsnext'];
71
+ /**
72
+ * A directory's package entry, in Vite's order: `exports`, each main field,
73
+ * then `main`. `undefined` means no manifest or no entry, so `index` is next;
74
+ * `null` means an entry this does not evaluate (a conditional `exports` map,
75
+ * a `browser` entry Vite would choose by sniffing), and the caller must
76
+ * exclude nothing rather than guess.
77
+ */
78
+ function packageEntry(dir, mainFields) {
79
+ let raw;
80
+ try {
81
+ raw = readFileSync(join(dir, 'package.json'), 'utf8');
82
+ }
83
+ catch {
84
+ return undefined;
85
+ }
86
+ let pkg;
87
+ try {
88
+ pkg = JSON.parse(raw);
89
+ }
90
+ catch {
91
+ return null;
92
+ }
93
+ if (pkg.exports !== undefined) {
94
+ const exp = pkg.exports;
95
+ const root = exp && typeof exp === 'object' && '.' in exp ? exp['.'] : exp;
96
+ return typeof root === 'string' ? root : null;
97
+ }
98
+ for (const field of [...mainFields, 'main']) {
99
+ const value = pkg[field];
100
+ if (field === 'browser') {
101
+ const entry = value && typeof value === 'object'
102
+ ? value['.']
103
+ : value;
104
+ if (typeof entry !== 'string')
105
+ continue;
106
+ // Vite picks between a browser entry and a different `module` entry
107
+ // by sniffing the file for ESM syntax; that is not mirrored here.
108
+ const sniffed = mainFields.includes('module') &&
109
+ typeof pkg.module === 'string' &&
110
+ pkg.module !== entry;
111
+ return sniffed ? null : entry;
112
+ }
113
+ // Vite's `entryPoint ||= data.main`: an empty entry is no entry.
114
+ if (typeof value === 'string' && value)
115
+ return value;
116
+ }
117
+ return undefined;
118
+ }
119
+ /**
120
+ * The module file an alias replacement actually resolves to.
121
+ *
122
+ * A replacement is a string substitution, not a module id. Vite's order: the
123
+ * literal file, then the TS source of a JS spelling, then each of the
124
+ * configured `extensions` in order; only then a directory, through its
125
+ * package entry and else its `index`. A directory is never the answer: it
126
+ * names one entry module, not every file in it.
127
+ */
128
+ function resolveReplacement(replacement, extensions, mainFields, readManifest = true) {
129
+ const direct = statOf(replacement);
130
+ if (direct?.isFile())
131
+ return replacement;
132
+ const ext = extname(replacement);
133
+ const stem = ext ? replacement.slice(0, -ext.length) : replacement;
134
+ const candidates = ext
135
+ ? (TS_SOURCE_EXTENSIONS[ext] ?? []).map(e => stem + e)
136
+ : [];
137
+ for (const e of extensions)
138
+ candidates.push(replacement + e);
139
+ const file = candidates.find(candidate => statOf(candidate)?.isFile());
140
+ if (file || !direct?.isDirectory())
141
+ return file;
142
+ const entry = readManifest
143
+ ? packageEntry(replacement, mainFields)
144
+ : undefined;
145
+ if (entry === null)
146
+ return undefined;
147
+ if (entry !== undefined) {
148
+ // With the manifest skipped, as Vite resolves an entry: `main: "."`
149
+ // would otherwise resolve this same directory forever.
150
+ return resolveReplacement(join(replacement, entry), extensions, mainFields, false);
151
+ }
152
+ return extensions
153
+ .map(e => join(replacement, `index${e}`))
154
+ .find(candidate => statOf(candidate)?.isFile());
155
+ }
156
+ const NO_EXCLUSIONS = { dirs: [], files: [] };
157
+ /**
158
+ * ui-kit's generated entry points, as its package.json `exports` lists them.
159
+ * An alias may redirect any one of them on its own, so each is resolved
160
+ * separately rather than inferred from whichever alias named the package.
161
+ */
162
+ const REGISTRY_SUBPATHS = [
163
+ `/${REGISTRY_DIR_NAME}`,
164
+ `/${REGISTRY_DIR_NAME}/components`,
165
+ `/${REGISTRY_DIR_NAME}/docs`,
166
+ `/${REGISTRY_DIR_NAME}/renderer`,
167
+ ];
168
+ /**
169
+ * One alias applied to one importee, by Vite's own rules: exact, or a prefix
170
+ * ending at a `/` segment boundary, or a pattern.
171
+ */
172
+ function aliasTargetFor(alias, importee) {
173
+ if (!alias.replacement)
174
+ return undefined;
175
+ if (typeof alias.find === 'string') {
176
+ if (alias.find === importee)
177
+ return alias.replacement;
178
+ if (importee.startsWith(`${alias.find}/`)) {
179
+ return alias.replacement + importee.slice(alias.find.length);
180
+ }
181
+ return undefined;
182
+ }
183
+ // A LOCAL CLONE. `test` and `[Symbol.replace]` both advance `lastIndex` on
184
+ // a /g or /y expression, and the object belongs to Vite — leaving it
185
+ // advanced made the user's alias stop resolving for the rest of the build.
186
+ const find = new RegExp(alias.find.source, alias.find.flags);
187
+ if (!find.test(importee))
188
+ return undefined;
189
+ find.lastIndex = 0;
190
+ return importee.replace(find, alias.replacement);
191
+ }
192
+ /** `realpathSync`, or undefined for a path that cannot be resolved. */
193
+ function safeRealpath(p) {
194
+ try {
195
+ return realpathSync(p);
196
+ }
197
+ catch {
198
+ return undefined;
199
+ }
200
+ }
201
+ /**
202
+ * ui-kit's generated registry projection — metadata and documentation prose,
203
+ * never component code, so it holds no utility map to find.
204
+ *
205
+ * It is skipped because it NAMES utility classes in example assertions ("has
206
+ * u-icon-sm class"). The scan cannot tell a quoted name in prose from one in a
207
+ * real class map, so those became `!important` rules in every app's entry CSS,
208
+ * where `.u-icon-sm` outweighed Toast's own icon sizing.
209
+ *
210
+ * The exclusions are derived from the RESOLVED aliases, not from the string
211
+ * "ui-kit" in the path: the alias may point anywhere, and an alias onto
212
+ * `/vendor/pithy-ui/src` leaked `u-line-clamp-3` back out when this was a
213
+ * path pattern alone. Every form is checked at this one choke point, so the
214
+ * directory walk and the `transform` hook cannot disagree.
215
+ */
216
+ function isGeneratedRegistry(normalizedPath, exclusions) {
217
+ return (exclusions.files.includes(normalizedPath) ||
218
+ exclusions.dirs.some(dir => normalizedPath.startsWith(dir)) ||
219
+ UI_KIT_REGISTRY_PATH.test(normalizedPath));
220
+ }
33
221
  /**
34
222
  * Check if a file path matches the include/exclude patterns
35
223
  */
36
- function shouldProcessFile(filePath, include, exclude) {
224
+ function shouldProcessFile(filePath, include, exclude, exclusions = NO_EXCLUSIONS) {
37
225
  // Simple pattern matching - could use micromatch for more robust matching
38
226
  const normalizedPath = filePath.replace(/\\/g, '/');
227
+ if (isGeneratedRegistry(normalizedPath, exclusions))
228
+ return false;
39
229
  // Check excludes first
40
230
  for (const pattern of exclude) {
41
231
  if (matchPattern(normalizedPath, pattern)) {
@@ -240,6 +430,12 @@ export function designSystemPlugin(options = {}) {
240
430
  let cachedCSS = null;
241
431
  let lastHash = '';
242
432
  let fullCSSBytes = null;
433
+ /**
434
+ * `<resolved ui-kit dir>/registry/`, with the realpath spelling too so an
435
+ * alias onto a symlink and a `transform` id that is the real path both
436
+ * match. `undefined` until first asked; reset in `configResolved`.
437
+ */
438
+ let registryExclusionsCache;
243
439
  /** Latch so the degraded-scan warning prints once per build, not per load. */
244
440
  let warnedInactive = false;
245
441
  /** Same, for the active-path summary. */
@@ -259,63 +455,201 @@ export function designSystemPlugin(options = {}) {
259
455
  * The alias replacement may point to a file (e.g. `.../src/index.ts`)
260
456
  * rather than a directory. In that case we normalize to the containing
261
457
  * directory so that `walk()` can enumerate the source tree.
458
+ *
459
+ * Matches exact, prefix, subpath and RegExp aliases, in three passes ordered
460
+ * by how strongly each form identifies the package (see the body). A subpath
461
+ * (`@pithyjs/ui-kit/styles`) is peeled back to the package root; one that
462
+ * cannot be peeled is used as-is, last.
463
+ *
464
+ * Hand-rolls Vite's alias semantics, as `aliasReplacement` in
465
+ * channelVars.ts does. #366 proposes replacing both with
466
+ * `config.createResolver()`.
262
467
  */
468
+ function resolveUiKitTarget() {
469
+ const aliases = aliasList();
470
+ if (aliases.length === 0)
471
+ return undefined;
472
+ // The loop takes the first USABLE answer, so a weaker form must not run
473
+ // before a stronger one — order of keys in the alias object would decide
474
+ // the result. 0 NAMES the package, 1 DERIVES a root by stripping the
475
+ // subpath, 2 falls back to the aliased directory.
476
+ for (const pass of [0, 1, 2]) {
477
+ for (const alias of aliases) {
478
+ const dir = resolveFromAlias(alias, pass);
479
+ // Pass 2 is a module's PARENT, which is a directory to scan and
480
+ // nothing more. Only passes 0 and 1 identify the package itself, so
481
+ // only they may imply where its registry sits.
482
+ if (dir)
483
+ return { dir, isPackageRoot: pass !== 2 };
484
+ }
485
+ }
486
+ return undefined;
487
+ }
263
488
  function resolveUiKitDir() {
489
+ return resolveUiKitTarget()?.dir;
490
+ }
491
+ /** `config.resolve.alias` as a list; Vite's object form is not supported. */
492
+ function aliasList() {
264
493
  const aliases = config.resolve?.alias;
265
- if (!aliases)
494
+ return Array.isArray(aliases) ? aliases : [];
495
+ }
496
+ /**
497
+ * One alias entry -> a usable ui-kit directory, or undefined.
498
+ * `pass` selects which alias forms are eligible; see resolveUiKitDir.
499
+ */
500
+ function resolveFromAlias(alias, pass) {
501
+ if (!alias.replacement)
266
502
  return undefined;
267
- // Vite normalizes aliases to an array of { find, replacement }
268
- if (Array.isArray(aliases)) {
269
- for (const alias of aliases) {
270
- if (!alias.replacement)
271
- continue;
272
- const NAME = '@pithyjs/ui-kit';
273
- if (typeof alias.find === 'string') {
274
- // Vite's rule, not exact-only: `=== name`, or a prefix ending at a
275
- // `/` SEGMENT boundary. Exact-only here while `aliasReplacement`
276
- // implemented Vite's rule meant `{find:'@pithyjs'}` resolved the scan
277
- // root but not this one — so ui-kit's utility classes never reached
278
- // the collector and its components rendered UNSTYLED in JIT mode.
279
- // Same failure as the RegExp branch had, left in the string branch.
280
- if (alias.find === NAME) {
281
- const dir = normalizeToDir(alias.replacement);
282
- if (dir)
283
- return dir;
284
- }
285
- else if (NAME.startsWith(`${alias.find}/`)) {
286
- const dir = normalizeToDir(alias.replacement + NAME.slice(alias.find.length));
287
- if (dir)
288
- return dir;
289
- }
290
- }
291
- else if (alias.find instanceof RegExp) {
292
- // A LOCAL CLONE. The alias object is Vite's, and it calls
293
- // `pattern.test(importee)` on it for every later import. `test` and
294
- // `[Symbol.replace]` both advance `lastIndex` on /y or /g, so
295
- // leaving it advanced made the user's alias stop resolving for the
296
- // rest of the build — a module-resolution failure caused by a CSS
297
- // plugin. Resetting before the call did not cover that; not touching
298
- // the object does.
299
- const find = new RegExp(alias.find.source, alias.find.flags);
300
- if (find.test(NAME)) {
301
- find.lastIndex = 0;
302
- // STRING replacement so `$1` works — the canonical monorepo alias
303
- // is `/^@pithyjs\/(.*)$/` with `packages/$1/src`.
304
- const dir = normalizeToDir(NAME.replace(find, alias.replacement));
305
- // Only accept a usable answer: a broad regex matching first must
306
- // not shadow a later exact alias that would have worked.
307
- if (dir)
308
- return dir;
309
- }
503
+ const NAME = UI_KIT_NAME;
504
+ if (typeof alias.find === 'string') {
505
+ // Vite's rule, not exact-only: `=== name`, or a prefix ending at a
506
+ // `/` SEGMENT boundary. Exact-only here meant `{find:'@pithyjs'}`
507
+ // resolved nowhere, so ui-kit's utility classes never reached the
508
+ // collector and its components rendered UNSTYLED in JIT mode.
509
+ if (alias.find === NAME) {
510
+ return pass === 0 ? normalizeToDir(alias.replacement) : undefined;
511
+ }
512
+ if (NAME.startsWith(`${alias.find}/`)) {
513
+ return pass === 0
514
+ ? normalizeToDir(alias.replacement + NAME.slice(alias.find.length))
515
+ : undefined;
516
+ }
517
+ if (alias.find.startsWith(`${NAME}/`)) {
518
+ // A SUBPATH alias (`@pithyjs/ui-kit/styles`) names a directory inside
519
+ // the package, so peel it off to get the root.
520
+ // Neither subpath form may return a path inside the Vite root:
521
+ // `walk(config.root)` covers that tree, and `log()` would report an
522
+ // APP directory as "ui-kit source".
523
+ const sub = alias.find.slice(NAME.length);
524
+ const peeled = peelSubpath(alias.replacement, sub);
525
+ if (peeled !== undefined) {
526
+ if (pass !== 1)
527
+ return undefined;
528
+ const dir = normalizeToDir(peeled);
529
+ return dir && !isInsideRoot(dir) ? dir : undefined;
310
530
  }
531
+ // Unpeelable: the replacement points somewhere else entirely, which is
532
+ // what every app here writes (`.../styles/index.scss`).
533
+ //
534
+ // This is only the directory to SCAN. What to exclude is collected
535
+ // separately, over every alias — see `registryExclusions`.
536
+ if (pass !== 2)
537
+ return undefined;
538
+ const dir = normalizeToDir(alias.replacement);
539
+ return dir && !isInsideRoot(dir) ? dir : undefined;
311
540
  }
541
+ return undefined;
542
+ }
543
+ if (alias.find instanceof RegExp) {
544
+ // A LOCAL CLONE. The alias object is Vite's, and it calls
545
+ // `pattern.test(importee)` on it for every later import. `test` and
546
+ // `[Symbol.replace]` both advance `lastIndex` on /y or /g, so
547
+ // leaving it advanced made the user's alias stop resolving for the
548
+ // rest of the build — a module-resolution failure caused by a CSS
549
+ // plugin. Resetting before the call did not cover that; not touching
550
+ // the object does.
551
+ if (pass !== 0)
552
+ return undefined;
553
+ const find = new RegExp(alias.find.source, alias.find.flags);
554
+ if (!find.test(NAME))
555
+ return undefined;
556
+ find.lastIndex = 0;
557
+ // STRING replacement so `$1` works — the canonical monorepo alias
558
+ // is `/^@pithyjs\/(.*)$/` with `packages/$1/src`.
559
+ // Only accept a usable answer: a broad regex matching first must
560
+ // not shadow a later exact alias that would have worked.
561
+ return normalizeToDir(NAME.replace(find, alias.replacement));
312
562
  }
313
563
  return undefined;
314
564
  }
565
+ /** True when `p` is at or below the Vite root, which `walk()` already covers. */
566
+ function isInsideRoot(p) {
567
+ if (!config.root)
568
+ return false;
569
+ const rel = relative(config.root, p);
570
+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
571
+ }
315
572
  /**
316
573
  * Ensure a path points to a directory. If the path is a file, return
317
574
  * its parent directory. Returns undefined if the path doesn't exist.
318
575
  */
576
+ /**
577
+ * Every generated-registry path this config can reach.
578
+ *
579
+ * Two independent sources, because one alias cannot answer both questions.
580
+ * The preferred scan root contributes its own `registry/` subtree. Then
581
+ * EVERY explicit `@pithyjs/ui-kit/registry*` alias contributes its own
582
+ * target: an app may point the docs module at one tree and the package at
583
+ * another, and Vite honours both, so deriving the exclusions from the
584
+ * single preferred root let the override's docs through `transform`.
585
+ */
586
+ function registryExclusions() {
587
+ if (registryExclusionsCache)
588
+ return registryExclusionsCache;
589
+ const dirs = new Set();
590
+ const files = new Set();
591
+ const addDir = (p) => {
592
+ for (const q of [p, safeRealpath(p)]) {
593
+ if (q)
594
+ dirs.add(`${q.replace(/\\/g, '/').replace(/\/+$/, '')}/`);
595
+ }
596
+ };
597
+ const addFile = (p) => {
598
+ for (const q of [p, safeRealpath(p)]) {
599
+ if (q)
600
+ files.add(q.replace(/\\/g, '/'));
601
+ }
602
+ };
603
+ // Where the package's own registry sits — but only from an alias that
604
+ // identified the package. A module's parent directory is not a root.
605
+ const target = resolveUiKitTarget();
606
+ if (target?.isPackageRoot)
607
+ addDir(join(target.dir, REGISTRY_DIR_NAME));
608
+ // Then every alias that redirects a registry entry point, by the
609
+ // specifier rather than by the alias name, so a pattern alias counts too.
610
+ for (const sub of REGISTRY_SUBPATHS) {
611
+ for (const alias of aliasList()) {
612
+ const replacement = aliasTargetFor(alias, `${UI_KIT_NAME}${sub}`);
613
+ if (!replacement)
614
+ continue;
615
+ const resolved = resolveReplacement(replacement, config.resolve?.extensions ?? RESOLVE_EXTENSIONS, config.resolve?.mainFields ?? MAIN_FIELDS);
616
+ if (!resolved)
617
+ continue;
618
+ // ALWAYS the file itself, both spellings: a link on the file is not
619
+ // resolved by taking the realpath of its directory, and Vite hands
620
+ // `transform` the real path.
621
+ addFile(resolved);
622
+ const dir = registryDirOf(resolved, sub);
623
+ if (dir)
624
+ addDir(dir);
625
+ }
626
+ }
627
+ registryExclusionsCache = { dirs: [...dirs], files: [...files] };
628
+ return registryExclusionsCache;
629
+ }
630
+ /**
631
+ * The registry DIRECTORY a file replacement implies, or undefined.
632
+ *
633
+ * Only when the path mirrors the alias name: `<x>/registry/docs.ts` for
634
+ * `/registry/docs` puts the module IN the directory the subpath names, so
635
+ * its generated siblings are covered too.
636
+ *
637
+ * A flat `<x>/registry.ts` ends in `/registry` as well, but that is the
638
+ * file's basename — there the registry is a file, and claiming `<x>` or
639
+ * `<x>/registry/` from it took out a real component. A one-segment subpath
640
+ * claims a directory only through that directory's `index` entry.
641
+ */
642
+ function registryDirOf(replacement, sub) {
643
+ const noExt = replacement.replace(/\\/g, '/').replace(/\.[^./]+$/, '');
644
+ const isIndex = noExt.endsWith('/index');
645
+ const logical = isIndex ? noExt.slice(0, -'/index'.length) : noExt;
646
+ if (!logical.endsWith(sub))
647
+ return undefined;
648
+ const drop = sub.split('/').length - 2;
649
+ if (drop === 0)
650
+ return isIndex ? logical : undefined;
651
+ return logical.split('/').slice(0, -drop).join('/');
652
+ }
319
653
  function normalizeToDir(p) {
320
654
  try {
321
655
  const stat = statSync(p);
@@ -339,6 +673,7 @@ export function designSystemPlugin(options = {}) {
339
673
  */
340
674
  function scanSourceFiles() {
341
675
  const files = [];
676
+ const exclusions = registryExclusions();
342
677
  function walk(dir) {
343
678
  let entries;
344
679
  try {
@@ -357,7 +692,7 @@ export function designSystemPlugin(options = {}) {
357
692
  walk(fullPath);
358
693
  }
359
694
  else if (entry.isFile() &&
360
- shouldProcessFile(fullPath, include, exclude)) {
695
+ shouldProcessFile(fullPath, include, exclude, exclusions)) {
361
696
  files.push(fullPath);
362
697
  }
363
698
  }
@@ -536,8 +871,16 @@ export function designSystemPlugin(options = {}) {
536
871
  }
537
872
  return {
538
873
  name: 'pithy-design-system',
874
+ // Read by `@pithyjs/ssr`'s static export, which runs a second,
875
+ // declaration-level purge over the CSS this plugin emits and would
876
+ // otherwise drop the runtime-composed names this safelist protects. There
877
+ // is no import either way between the two packages.
878
+ api: {
879
+ channelVarSafelist: resolvedOptions.channelVarSafelist,
880
+ },
539
881
  configResolved(resolvedConfig) {
540
882
  config = resolvedConfig;
883
+ registryExclusionsCache = undefined;
541
884
  // Initialize collector — pass all enumerated utilities so regex
542
885
  // safelist entries (e.g. /^u-text-/) can be expanded correctly.
543
886
  collector = createCollector({
@@ -564,7 +907,8 @@ export function designSystemPlugin(options = {}) {
564
907
  },
565
908
  transform(code, id) {
566
909
  // Skip if collector not yet initialized or file doesn't match
567
- if (!collector || !shouldProcessFile(id, include, exclude)) {
910
+ if (!collector ||
911
+ !shouldProcessFile(id, include, exclude, registryExclusions())) {
568
912
  return null;
569
913
  }
570
914
  // Extract utilities from the file
@@ -595,7 +939,7 @@ export function designSystemPlugin(options = {}) {
595
939
  });
596
940
  }
597
941
  srv.watcher.on('unlink', filePath => {
598
- if (!shouldProcessFile(filePath, include, exclude)) {
942
+ if (!shouldProcessFile(filePath, include, exclude, registryExclusions())) {
599
943
  return;
600
944
  }
601
945
  log(`File removed: ${filePath}`);
@@ -628,4 +972,26 @@ export function designSystemPlugin(options = {}) {
628
972
  },
629
973
  };
630
974
  }
975
+ /**
976
+ * Strip a subpath suffix off an alias replacement, yielding the package root.
977
+ * Returns undefined when the replacement does not end with the subpath, or
978
+ * when the peel is not absolute.
979
+ *
980
+ * Separator-insensitive: `sub` comes from an alias `find` and always uses `/`,
981
+ * while `replacement` comes from `path.resolve`. Comparing them raw made the
982
+ * peel never fire on Windows, and CI has no Windows job.
983
+ */
984
+ export function peelSubpath(replacement, sub) {
985
+ const norm = (x) => x.replace(/\\/g, '/');
986
+ if (!norm(replacement).endsWith(norm(sub)))
987
+ return undefined;
988
+ const peeled = replacement.slice(0, -sub.length);
989
+ // `'./styles'` peels to '.', which made the plugin walk the process CWD.
990
+ // Both platforms, not `isAbsolute`: that follows the host and would reject
991
+ // `C:\...` on a POSIX runner. Also catches '' from a bare `/styles`.
992
+ if (!posix.isAbsolute(norm(peeled)) && !win32.isAbsolute(peeled)) {
993
+ return undefined;
994
+ }
995
+ return peeled;
996
+ }
631
997
  //# sourceMappingURL=plugin.js.map