@adea-ai/ui 0.147.13 → 0.149.0
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/dist/vite/index.d.ts +2 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/source-parser.d.ts +57 -0
- package/dist/vite/source-parser.d.ts.map +1 -0
- package/dist/vite/source-parser.js +248 -0
- package/dist/vite/source-parser.js.map +1 -0
- package/dist/vite/tailwind-sources.d.ts +111 -0
- package/dist/vite/tailwind-sources.d.ts.map +1 -0
- package/dist/vite/tailwind-sources.js +501 -0
- package/dist/vite/tailwind-sources.js.map +1 -0
- package/dist/vite.js +2 -0
- package/package.json +29 -3
- package/src/testing/solid-transform.ts +48 -0
- package/src/vite/index.ts +17 -0
- package/src/vite/source-parser.ts +358 -0
- package/src/vite/tailwind-sources.ts +754 -0
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
realpathSync,
|
|
7
|
+
symlinkSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from 'node:fs'
|
|
10
|
+
import { basename, dirname, extname, isAbsolute, relative, resolve, sep } from 'node:path'
|
|
11
|
+
import {
|
|
12
|
+
defaultExport,
|
|
13
|
+
directRuntimeExports,
|
|
14
|
+
runtimeReexports,
|
|
15
|
+
dynamicRuntimeImports,
|
|
16
|
+
hasRuntimeDeclaration,
|
|
17
|
+
parseSource,
|
|
18
|
+
type SyntaxNode,
|
|
19
|
+
selectNamespace,
|
|
20
|
+
staticRuntimeImports,
|
|
21
|
+
} from './source-parser'
|
|
22
|
+
|
|
23
|
+
import { normalizePath, type Plugin, type ResolvedConfig } from 'vite'
|
|
24
|
+
|
|
25
|
+
export type Selection =
|
|
26
|
+
| { kind: 'execute' | 'side-effect' | 'all' }
|
|
27
|
+
| { kind: 'named'; names: string[] }
|
|
28
|
+
|
|
29
|
+
interface RuntimeImport {
|
|
30
|
+
specifier: string
|
|
31
|
+
selection: Selection
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ExportTarget {
|
|
35
|
+
file: string
|
|
36
|
+
selection: Selection
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ParsedModule {
|
|
40
|
+
file: string
|
|
41
|
+
syntax: SyntaxNode
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface SourceDiscoveryOptions {
|
|
45
|
+
entrypoint: string | string[]
|
|
46
|
+
repositoryRoot: string
|
|
47
|
+
uiPackageRoot: string
|
|
48
|
+
uiSourceRoot: string
|
|
49
|
+
/** Source roots of installed packages (such as the app framework) whose shared views render UI modules. */
|
|
50
|
+
sourcePackageRoots?: string[]
|
|
51
|
+
resolveId(specifier: string, importer: string): Promise<string | undefined>
|
|
52
|
+
/** Error-message prefix naming the consumer. */
|
|
53
|
+
label?: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SourceDirectiveOptions {
|
|
57
|
+
files: string[]
|
|
58
|
+
uiPackageRoot: string
|
|
59
|
+
stablePackagePath: string
|
|
60
|
+
stylesheetPath: string
|
|
61
|
+
label?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const defaultLabel = 'ui-tailwind-sources'
|
|
65
|
+
|
|
66
|
+
function modulePath(id: string): string {
|
|
67
|
+
const withoutQuery = id.split('?')[0] ?? id
|
|
68
|
+
const absolute = resolve(withoutQuery)
|
|
69
|
+
try {
|
|
70
|
+
return realpathSync(absolute)
|
|
71
|
+
} catch {
|
|
72
|
+
return absolute
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function inside(path: string, root: string): boolean {
|
|
77
|
+
const pathFromRoot = relative(root, path)
|
|
78
|
+
return (
|
|
79
|
+
pathFromRoot === '' ||
|
|
80
|
+
(pathFromRoot !== '..' && !pathFromRoot.startsWith(`..${sep}`) && !isAbsolute(pathFromRoot))
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isWorkspaceSourceFile(
|
|
85
|
+
path: string,
|
|
86
|
+
repositoryRoot: string,
|
|
87
|
+
sourcePackageRoots: readonly string[] = []
|
|
88
|
+
): boolean {
|
|
89
|
+
if (sourcePackageRoots.some((root) => inside(path, root))) return true
|
|
90
|
+
if (!inside(path, repositoryRoot)) return false
|
|
91
|
+
const segments = relative(repositoryRoot, path).split(sep)
|
|
92
|
+
return (
|
|
93
|
+
(segments[0] === 'apps' || segments[0] === 'packages') &&
|
|
94
|
+
segments.includes('src') &&
|
|
95
|
+
!segments.includes('node_modules')
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isCodeFile(path: string): boolean {
|
|
100
|
+
return ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'].includes(extname(path))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readModule(path: string, cache: Map<string, ParsedModule>): ParsedModule {
|
|
104
|
+
const file = modulePath(path)
|
|
105
|
+
const cached = cache.get(file)
|
|
106
|
+
if (cached) return cached
|
|
107
|
+
const source = readFileSync(file, 'utf8')
|
|
108
|
+
const parsed: ParsedModule = { file, syntax: parseSource(file, source) }
|
|
109
|
+
cache.set(file, parsed)
|
|
110
|
+
return parsed
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function runtimeImports(parsed: ParsedModule, label: string): RuntimeImport[] {
|
|
114
|
+
const imports: RuntimeImport[] = []
|
|
115
|
+
|
|
116
|
+
for (const entry of staticRuntimeImports(parsed.syntax)) {
|
|
117
|
+
if (entry.kind === 'side-effect') {
|
|
118
|
+
imports.push({
|
|
119
|
+
specifier: entry.specifier,
|
|
120
|
+
selection: { kind: 'side-effect' },
|
|
121
|
+
})
|
|
122
|
+
continue
|
|
123
|
+
}
|
|
124
|
+
const namespace = entry.bindings.find((binding) => binding.imported === '*')
|
|
125
|
+
if (namespace) {
|
|
126
|
+
const selection = selectNamespace(parsed.syntax, namespace.local)
|
|
127
|
+
if (selection) imports.push({ specifier: entry.specifier, selection })
|
|
128
|
+
} else {
|
|
129
|
+
imports.push({
|
|
130
|
+
specifier: entry.specifier,
|
|
131
|
+
selection: {
|
|
132
|
+
kind: 'named',
|
|
133
|
+
names: [...new Set(entry.bindings.map((binding) => binding.imported))].toSorted(),
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
imports.push(...dynamicRuntimeImports(parsed.syntax, parsed.file, label))
|
|
140
|
+
return imports
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function localRuntimeBinding(
|
|
144
|
+
file: string,
|
|
145
|
+
localName: string,
|
|
146
|
+
cache: Map<string, ParsedModule>
|
|
147
|
+
): RuntimeImport | undefined {
|
|
148
|
+
const parsed = readModule(file, cache)
|
|
149
|
+
for (const entry of staticRuntimeImports(parsed.syntax)) {
|
|
150
|
+
const binding = entry.bindings.find((candidate) => candidate.local === localName)
|
|
151
|
+
if (binding)
|
|
152
|
+
return {
|
|
153
|
+
specifier: entry.specifier,
|
|
154
|
+
selection:
|
|
155
|
+
binding.imported === '*' ? { kind: 'all' } : { kind: 'named', names: [binding.imported] },
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function hasLocalRuntimeBinding(
|
|
162
|
+
file: string,
|
|
163
|
+
localName: string,
|
|
164
|
+
cache: Map<string, ParsedModule>
|
|
165
|
+
): boolean {
|
|
166
|
+
return hasRuntimeDeclaration(readModule(file, cache).syntax, localName)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function modifierExport(file: string, name: string, cache: Map<string, ParsedModule>): boolean {
|
|
170
|
+
return directRuntimeExports(readModule(file, cache).syntax).includes(name)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function collectUiSourceFiles(options: SourceDiscoveryOptions): Promise<string[]> {
|
|
174
|
+
const label = options.label ?? defaultLabel
|
|
175
|
+
const repositoryRoot = realpathSync(options.repositoryRoot)
|
|
176
|
+
const uiSourceRoot = realpathSync(options.uiSourceRoot)
|
|
177
|
+
const sourcePackageRoots = (options.sourcePackageRoots ?? []).map((root) => realpathSync(root))
|
|
178
|
+
const entrypoints = (
|
|
179
|
+
Array.isArray(options.entrypoint) ? options.entrypoint : [options.entrypoint]
|
|
180
|
+
).map((entrypoint) => realpathSync(entrypoint))
|
|
181
|
+
const cache = new Map<string, ParsedModule>()
|
|
182
|
+
const visited = new Set<string>()
|
|
183
|
+
const files = new Set<string>()
|
|
184
|
+
const publishedUiResolutions: string[] = []
|
|
185
|
+
|
|
186
|
+
const resolveImport = async (
|
|
187
|
+
specifier: string,
|
|
188
|
+
importer: string,
|
|
189
|
+
selection: Selection
|
|
190
|
+
): Promise<void> => {
|
|
191
|
+
const importerIsUi = inside(importer, uiSourceRoot)
|
|
192
|
+
const isPublishedUiImport = specifier === '@adea-ai/ui' || specifier.startsWith('@adea-ai/ui/')
|
|
193
|
+
const resolved = await options.resolveId(specifier, importer)
|
|
194
|
+
if (isPublishedUiImport)
|
|
195
|
+
publishedUiResolutions.push(`${specifier} -> ${resolved ?? '(unresolved)'}`)
|
|
196
|
+
if (!resolved) {
|
|
197
|
+
if (isPublishedUiImport || importerIsUi)
|
|
198
|
+
throw new Error(
|
|
199
|
+
`[${label}] unable to resolve runtime import '${specifier}' from ${importer}`
|
|
200
|
+
)
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const target = modulePath(resolved)
|
|
205
|
+
if (!isCodeFile(target)) return
|
|
206
|
+
if (isPublishedUiImport && !inside(target, uiSourceRoot)) {
|
|
207
|
+
throw new Error(`[${label}] '${specifier}' resolved outside published UI source: ${target}`)
|
|
208
|
+
}
|
|
209
|
+
if (
|
|
210
|
+
inside(target, uiSourceRoot) ||
|
|
211
|
+
isWorkspaceSourceFile(target, repositoryRoot, sourcePackageRoots)
|
|
212
|
+
) {
|
|
213
|
+
await visit(target, selection)
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const resolveExportTargets = async (
|
|
219
|
+
file: string,
|
|
220
|
+
name: string,
|
|
221
|
+
chain: Set<string>
|
|
222
|
+
): Promise<ExportTarget[]> => {
|
|
223
|
+
const key = `${file}\0${name}`
|
|
224
|
+
if (chain.has(key)) return []
|
|
225
|
+
const nextChain = new Set(chain).add(key)
|
|
226
|
+
const parsed = readModule(file, cache)
|
|
227
|
+
const targets: ExportTarget[] = []
|
|
228
|
+
|
|
229
|
+
if (modifierExport(file, name, cache)) targets.push({ file, selection: { kind: 'execute' } })
|
|
230
|
+
|
|
231
|
+
for (const entry of runtimeReexports(parsed.syntax)) {
|
|
232
|
+
if (entry.kind === 'empty') continue
|
|
233
|
+
if (entry.kind === 'named' && !entry.source) {
|
|
234
|
+
if (entry.exported !== name) continue
|
|
235
|
+
const imported = localRuntimeBinding(file, entry.imported, cache)
|
|
236
|
+
if (imported) {
|
|
237
|
+
const resolved = await options.resolveId(imported.specifier, file)
|
|
238
|
+
if (resolved)
|
|
239
|
+
targets.push({
|
|
240
|
+
file: modulePath(resolved),
|
|
241
|
+
selection: imported.selection,
|
|
242
|
+
})
|
|
243
|
+
else if (inside(file, uiSourceRoot))
|
|
244
|
+
throw new Error(
|
|
245
|
+
`[${label}] unable to resolve local re-export '${imported.specifier}' from ${file}`
|
|
246
|
+
)
|
|
247
|
+
} else if (hasLocalRuntimeBinding(file, entry.imported, cache))
|
|
248
|
+
targets.push({ file, selection: { kind: 'execute' } })
|
|
249
|
+
continue
|
|
250
|
+
}
|
|
251
|
+
if (
|
|
252
|
+
!entry.source ||
|
|
253
|
+
(entry.kind !== 'star' && entry.exported !== name) ||
|
|
254
|
+
(entry.kind === 'star' && name === 'default')
|
|
255
|
+
)
|
|
256
|
+
continue
|
|
257
|
+
if (entry.source === 'lucide-solid') {
|
|
258
|
+
targets.push({ file, selection: { kind: 'execute' } })
|
|
259
|
+
continue
|
|
260
|
+
}
|
|
261
|
+
const resolved = await options.resolveId(entry.source, file)
|
|
262
|
+
if (!resolved) {
|
|
263
|
+
if (inside(file, uiSourceRoot) && entry.kind !== 'namespace')
|
|
264
|
+
throw new Error(`[${label}] unable to resolve re-export '${entry.source}' from ${file}`)
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
if (entry.kind === 'star')
|
|
268
|
+
targets.push(...(await resolveExportTargets(modulePath(resolved), name, nextChain)))
|
|
269
|
+
else if (entry.kind === 'namespace')
|
|
270
|
+
targets.push({
|
|
271
|
+
file: modulePath(resolved),
|
|
272
|
+
selection: { kind: 'all' },
|
|
273
|
+
})
|
|
274
|
+
else
|
|
275
|
+
targets.push({
|
|
276
|
+
file: modulePath(resolved),
|
|
277
|
+
selection: { kind: 'named', names: [entry.imported] },
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
if (name === 'default') {
|
|
281
|
+
const entry = defaultExport(parsed.syntax)
|
|
282
|
+
if (entry?.kind === 'identifier') {
|
|
283
|
+
const imported = localRuntimeBinding(file, entry.name, cache)
|
|
284
|
+
if (imported) {
|
|
285
|
+
const resolved = await options.resolveId(imported.specifier, file)
|
|
286
|
+
if (resolved)
|
|
287
|
+
targets.push({
|
|
288
|
+
file: modulePath(resolved),
|
|
289
|
+
selection: imported.selection,
|
|
290
|
+
})
|
|
291
|
+
} else targets.push({ file, selection: { kind: 'execute' } })
|
|
292
|
+
} else if (entry) targets.push({ file, selection: { kind: 'execute' } })
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return deduplicateTargets(targets)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const runtimeExportNames = async (file: string, chain: Set<string>): Promise<string[]> => {
|
|
299
|
+
if (chain.has(file)) return []
|
|
300
|
+
const nextChain = new Set(chain).add(file)
|
|
301
|
+
const parsed = readModule(file, cache)
|
|
302
|
+
const names = new Set(directRuntimeExports(parsed.syntax))
|
|
303
|
+
for (const entry of runtimeReexports(parsed.syntax)) {
|
|
304
|
+
if (!entry.source) {
|
|
305
|
+
if (entry.kind === 'named') names.add(entry.exported)
|
|
306
|
+
continue
|
|
307
|
+
}
|
|
308
|
+
if (entry.kind === 'star' && entry.source === 'lucide-solid') continue
|
|
309
|
+
const resolved = await options.resolveId(entry.source, file)
|
|
310
|
+
if (!resolved) {
|
|
311
|
+
if (inside(file, uiSourceRoot))
|
|
312
|
+
throw new Error(`[${label}] unable to resolve re-export '${entry.source}' from ${file}`)
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
if (entry.kind === 'named' || entry.kind === 'namespace') names.add(entry.exported)
|
|
316
|
+
else if (entry.kind === 'star')
|
|
317
|
+
for (const name of await runtimeExportNames(modulePath(resolved), nextChain))
|
|
318
|
+
if (name !== 'default') names.add(name)
|
|
319
|
+
}
|
|
320
|
+
return [...names].toSorted()
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const visit = async (path: string, selection: Selection): Promise<void> => {
|
|
324
|
+
const file = modulePath(path)
|
|
325
|
+
if (!isCodeFile(file)) return
|
|
326
|
+
if (
|
|
327
|
+
!inside(file, uiSourceRoot) &&
|
|
328
|
+
!isWorkspaceSourceFile(file, repositoryRoot, sourcePackageRoots)
|
|
329
|
+
)
|
|
330
|
+
return
|
|
331
|
+
const key = `${file}\0${selection.kind === 'named' ? selection.names.join(',') : selection.kind}`
|
|
332
|
+
if (visited.has(key)) return
|
|
333
|
+
visited.add(key)
|
|
334
|
+
const parsed = readModule(file, cache)
|
|
335
|
+
const isUiSource = inside(file, uiSourceRoot)
|
|
336
|
+
if (isUiSource) files.add(file)
|
|
337
|
+
|
|
338
|
+
const moduleImports = runtimeImports(parsed, label)
|
|
339
|
+
// Follow all runtime imports of every visited module. Even when a module
|
|
340
|
+
// is reached through a side-effect import, its own imports may execute
|
|
341
|
+
// setup code or register UI components needed by the consumer.
|
|
342
|
+
for (const dependency of moduleImports) {
|
|
343
|
+
await resolveImport(dependency.specifier, file, dependency.selection)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (selection.kind === 'execute' || selection.kind === 'side-effect') return
|
|
347
|
+
const names =
|
|
348
|
+
selection.kind === 'named' ? selection.names : await runtimeExportNames(file, new Set())
|
|
349
|
+
for (const name of names) {
|
|
350
|
+
const targets = await resolveExportTargets(file, name, new Set())
|
|
351
|
+
if (!targets.length) {
|
|
352
|
+
throw new Error(`[${label}] unable to resolve runtime export '${name}' from ${file}`)
|
|
353
|
+
}
|
|
354
|
+
for (const target of targets) await visit(target.file, target.selection)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
for (const entrypoint of entrypoints) await visit(entrypoint, { kind: 'execute' })
|
|
359
|
+
if (files.size === 0)
|
|
360
|
+
throw new Error(
|
|
361
|
+
`[${label}] the client import graph resolved no shared UI source files from ${entrypoints.join(', ')}; package imports: ${publishedUiResolutions.slice(0, 8).join('; ') || '(none)'}`
|
|
362
|
+
)
|
|
363
|
+
return [...files].toSorted()
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function deduplicateTargets(targets: ExportTarget[]): ExportTarget[] {
|
|
367
|
+
const unique = new Map<string, ExportTarget>()
|
|
368
|
+
for (const target of targets) {
|
|
369
|
+
const key = `${target.file}\0${target.selection.kind === 'named' ? target.selection.names.join(',') : target.selection.kind}`
|
|
370
|
+
unique.set(key, target)
|
|
371
|
+
}
|
|
372
|
+
return [...unique.values()]
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function filesBelow(directory: string): string[] {
|
|
376
|
+
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
377
|
+
const path = resolve(directory, entry.name)
|
|
378
|
+
if (entry.isDirectory()) return filesBelow(path)
|
|
379
|
+
return entry.isFile() ? [path] : []
|
|
380
|
+
})
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Tailwind's scanner widens a file `@source` to that file's whole directory
|
|
385
|
+
* tree, so a reached `button/button.tsx` would also pull in an unused sibling
|
|
386
|
+
* (and a reached package-root barrel would pull in every component). Negate
|
|
387
|
+
* every file in those trees the import graph did not reach to keep discovery
|
|
388
|
+
* exact to the module.
|
|
389
|
+
*/
|
|
390
|
+
export function formatUiSourceDirectives(options: SourceDirectiveOptions): string {
|
|
391
|
+
const label = options.label ?? defaultLabel
|
|
392
|
+
const packageRoot = realpathSync(options.uiPackageRoot)
|
|
393
|
+
const stylesheetDirectory = dirname(resolve(options.stylesheetPath))
|
|
394
|
+
const directive = (file: string, negated: boolean) => {
|
|
395
|
+
if (!inside(file, packageRoot))
|
|
396
|
+
throw new Error(`[${label}] source is outside published package: ${file}`)
|
|
397
|
+
const stableFile = resolve(options.stablePackagePath, relative(packageRoot, file))
|
|
398
|
+
const relativePath = relative(stylesheetDirectory, stableFile).replaceAll('\\', '/')
|
|
399
|
+
if (isAbsolute(relativePath))
|
|
400
|
+
throw new Error(`[${label}] generated source path is not portable: ${relativePath}`)
|
|
401
|
+
return `@source ${negated ? 'not ' : ''}'${relativePath}';`
|
|
402
|
+
}
|
|
403
|
+
const included = [...new Set(options.files.map((path) => realpathSync(path)))].toSorted()
|
|
404
|
+
const includedSet = new Set(included)
|
|
405
|
+
const scannedTrees = [...new Set(included.map((file) => dirname(file)))]
|
|
406
|
+
.toSorted()
|
|
407
|
+
.filter((directory, index, all) => !all.slice(0, index).some((root) => inside(directory, root)))
|
|
408
|
+
const excluded = scannedTrees
|
|
409
|
+
.flatMap((directory) => filesBelow(directory))
|
|
410
|
+
.filter((file) => !includedSet.has(file))
|
|
411
|
+
.toSorted()
|
|
412
|
+
return `${[
|
|
413
|
+
...included.map((file) => directive(file, false)),
|
|
414
|
+
...excluded.map((file) => directive(file, true)),
|
|
415
|
+
].join('\n')}\n`
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* The rendered shared UI source modules (by package-relative path) that the
|
|
420
|
+
* generated directives do not cover. Ids may carry Vite query suffixes.
|
|
421
|
+
*/
|
|
422
|
+
export function missingRenderedUiSources(
|
|
423
|
+
renderedIds: string[],
|
|
424
|
+
generatedFiles: string[],
|
|
425
|
+
uiRoot: string
|
|
426
|
+
): string[] {
|
|
427
|
+
const uiSourceRoot = resolve(uiRoot, 'src')
|
|
428
|
+
const generated = new Set(generatedFiles.map((file) => modulePath(file)))
|
|
429
|
+
return [
|
|
430
|
+
...new Set(
|
|
431
|
+
renderedIds
|
|
432
|
+
.map((id) => modulePath(id))
|
|
433
|
+
.filter((file) => isCodeFile(file) && inside(file, uiSourceRoot) && !generated.has(file))
|
|
434
|
+
.map((file) => relative(uiRoot, file).replaceAll('\\', '/'))
|
|
435
|
+
),
|
|
436
|
+
].toSorted()
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function writeIfChanged(path: string, content: string) {
|
|
440
|
+
let current: string | undefined
|
|
441
|
+
try {
|
|
442
|
+
current = readFileSync(path, 'utf8')
|
|
443
|
+
} catch {
|
|
444
|
+
// The first invocation creates the ignored generated file.
|
|
445
|
+
}
|
|
446
|
+
if (current === content) return
|
|
447
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
448
|
+
writeFileSync(path, content)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The framework package's published `src` directory holding `file`: the
|
|
453
|
+
* nearest `src` ancestor whose parent is the @adea-ai/app-framework package.
|
|
454
|
+
* Its views live in subdirectories (`src/views`) and import across `src`.
|
|
455
|
+
*/
|
|
456
|
+
function frameworkSourceRoot(file: string): string {
|
|
457
|
+
for (
|
|
458
|
+
let directory = dirname(file);
|
|
459
|
+
dirname(directory) !== directory;
|
|
460
|
+
directory = dirname(directory)
|
|
461
|
+
) {
|
|
462
|
+
if (basename(directory) !== 'src') continue
|
|
463
|
+
try {
|
|
464
|
+
const manifest = JSON.parse(readFileSync(resolve(directory, '../package.json'), 'utf8')) as {
|
|
465
|
+
name?: unknown
|
|
466
|
+
}
|
|
467
|
+
if (manifest.name === '@adea-ai/app-framework') return directory
|
|
468
|
+
} catch {
|
|
469
|
+
// Not the package root; keep walking up.
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return dirname(file)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* The framework's shared views (Help Center and friends) import shared UI
|
|
477
|
+
* modules from its published source, so its graph counts as the app's.
|
|
478
|
+
*/
|
|
479
|
+
export async function frameworkSourceRoots(
|
|
480
|
+
resolveId: (specifier: string, importer: string) => Promise<string | undefined>,
|
|
481
|
+
entrypoint: string,
|
|
482
|
+
label = defaultLabel
|
|
483
|
+
): Promise<string[]> {
|
|
484
|
+
const framework = await resolveId('@adea-ai/app-framework/app-help', entrypoint)
|
|
485
|
+
if (!framework) return []
|
|
486
|
+
const root = frameworkSourceRoot(realpathSync(modulePath(framework)))
|
|
487
|
+
if (basename(root) !== 'src')
|
|
488
|
+
throw new Error(
|
|
489
|
+
`[${label}] @adea-ai/app-framework resolved outside its published source: ${root}`
|
|
490
|
+
)
|
|
491
|
+
return [root]
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export interface LinkedImport {
|
|
495
|
+
/** The shared UI source module that imports or re-exports the specifier. */
|
|
496
|
+
file: string
|
|
497
|
+
selection: Selection
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
export interface LinkedImportOptions {
|
|
501
|
+
entrypoint: string | string[]
|
|
502
|
+
repositoryRoot: string
|
|
503
|
+
uiSourceRoot: string
|
|
504
|
+
sourcePackageRoots?: string[]
|
|
505
|
+
/** The external package whose uses inside shared UI source are reported. */
|
|
506
|
+
specifier: string
|
|
507
|
+
resolveId(specifier: string, importer: string): Promise<string | undefined>
|
|
508
|
+
label?: string
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Every use of `specifier` inside shared UI source the dev server links.
|
|
513
|
+
*
|
|
514
|
+
* Vite serves the published package as native ESM, so static imports and
|
|
515
|
+
* re-exports are linked even when the consumer selects one root-barrel name,
|
|
516
|
+
* unlike the selective walk the Tailwind sources come from. Literal dynamic
|
|
517
|
+
* imports are included without executing them, so a lazy feature cannot fail
|
|
518
|
+
* later on an import the check never saw.
|
|
519
|
+
*/
|
|
520
|
+
export async function collectLinkedImports(options: LinkedImportOptions): Promise<LinkedImport[]> {
|
|
521
|
+
const label = options.label ?? defaultLabel
|
|
522
|
+
const repositoryRoot = realpathSync(options.repositoryRoot)
|
|
523
|
+
const uiSourceRoot = realpathSync(options.uiSourceRoot)
|
|
524
|
+
const sourcePackageRoots = (options.sourcePackageRoots ?? []).map((root) => realpathSync(root))
|
|
525
|
+
const entrypoints = (
|
|
526
|
+
Array.isArray(options.entrypoint) ? options.entrypoint : [options.entrypoint]
|
|
527
|
+
).map((entrypoint) => realpathSync(entrypoint))
|
|
528
|
+
const cache = new Map<string, ParsedModule>()
|
|
529
|
+
const visited = new Set<string>()
|
|
530
|
+
const linked: LinkedImport[] = []
|
|
531
|
+
const reached = (file: string) =>
|
|
532
|
+
isCodeFile(file) &&
|
|
533
|
+
(inside(file, uiSourceRoot) || isWorkspaceSourceFile(file, repositoryRoot, sourcePackageRoots))
|
|
534
|
+
|
|
535
|
+
const visit = async (path: string): Promise<void> => {
|
|
536
|
+
const file = modulePath(path)
|
|
537
|
+
if (!reached(file) || visited.has(file)) return
|
|
538
|
+
visited.add(file)
|
|
539
|
+
const inUi = inside(file, uiSourceRoot)
|
|
540
|
+
const parsed = readModule(file, cache)
|
|
541
|
+
const record = (selection: Selection) => {
|
|
542
|
+
if (inUi) linked.push({ file, selection })
|
|
543
|
+
}
|
|
544
|
+
const follow = async (specifier: string, kind: string): Promise<void> => {
|
|
545
|
+
const resolved = await options.resolveId(specifier, file)
|
|
546
|
+
if (!resolved) {
|
|
547
|
+
if (inUi || specifier === '@adea-ai/ui' || specifier.startsWith('@adea-ai/ui/'))
|
|
548
|
+
throw new Error(`[${label}] unable to resolve ${kind} '${specifier}' from ${file}`)
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
await visit(resolved)
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
for (const dependency of runtimeImports(parsed, label)) {
|
|
555
|
+
if (dependency.specifier === options.specifier) record(dependency.selection)
|
|
556
|
+
else await follow(dependency.specifier, 'runtime import')
|
|
557
|
+
}
|
|
558
|
+
for (const entry of runtimeReexports(parsed.syntax)) {
|
|
559
|
+
if (!entry.source) continue
|
|
560
|
+
if (entry.source !== options.specifier) await follow(entry.source, 're-export')
|
|
561
|
+
else if (entry.kind === 'named') record({ kind: 'named', names: [entry.imported] })
|
|
562
|
+
else if (entry.kind === 'empty') record({ kind: 'named', names: [] })
|
|
563
|
+
else record({ kind: 'all' })
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
for (const entrypoint of entrypoints) await visit(entrypoint)
|
|
568
|
+
return linked
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export interface GraphCheckContext {
|
|
572
|
+
command: ResolvedConfig['command']
|
|
573
|
+
repositoryRoot: string
|
|
574
|
+
/** The shared UI source modules the client graph reaches. */
|
|
575
|
+
files: string[]
|
|
576
|
+
/** Uses of an external package inside the shared UI source the dev server links. */
|
|
577
|
+
linkedImports(specifier: string): Promise<LinkedImport[]>
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
export interface TailwindSourcesOptions {
|
|
581
|
+
/** Names the plugin, its errors and the generated `<name>-ui-tailwind-sources` cache files. */
|
|
582
|
+
name: string
|
|
583
|
+
/** The Vite root of the consuming app. */
|
|
584
|
+
webRoot: string
|
|
585
|
+
/** Client entry modules, relative to `webRoot`. */
|
|
586
|
+
entrypoints: string[]
|
|
587
|
+
/** The stylesheet that imports the generated directives, relative to `webRoot`. */
|
|
588
|
+
styleEntry: string
|
|
589
|
+
/** The monorepo root whose `apps/*` and `packages/*` sources count as the app's. Defaults to `webRoot/../..`. */
|
|
590
|
+
repositoryRoot?: string
|
|
591
|
+
/** More paths the dev server watches, relative to `webRoot`. */
|
|
592
|
+
watch?: string[]
|
|
593
|
+
/** Runs after each generation, in dev and in builds, to enforce app-specific graph rules. */
|
|
594
|
+
check?(context: GraphCheckContext): void | Promise<void>
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
export interface UiSourcePaths {
|
|
598
|
+
entrypoints: string[]
|
|
599
|
+
repositoryRoot: string
|
|
600
|
+
stablePackagePath: string
|
|
601
|
+
stylesheetPath: string
|
|
602
|
+
manifestPath: string
|
|
603
|
+
styleEntry: string
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// The generated directives live in the ignored Vite cache next to the stable
|
|
607
|
+
// package link they reference, so every path in them stays checkout-relative.
|
|
608
|
+
export function uiSourcePaths(options: TailwindSourcesOptions): UiSourcePaths {
|
|
609
|
+
const { webRoot, name } = options
|
|
610
|
+
return {
|
|
611
|
+
entrypoints: options.entrypoints.map((entrypoint) => resolve(webRoot, entrypoint)),
|
|
612
|
+
repositoryRoot: resolve(webRoot, options.repositoryRoot ?? '../..'),
|
|
613
|
+
stablePackagePath: resolve(webRoot, 'node_modules/@adea-ai/ui'),
|
|
614
|
+
stylesheetPath: resolve(webRoot, `node_modules/.cache/${name}-ui-tailwind-sources.css`),
|
|
615
|
+
manifestPath: resolve(webRoot, `node_modules/.cache/${name}-ui-tailwind-sources.json`),
|
|
616
|
+
styleEntry: resolve(webRoot, options.styleEntry),
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
type Resolver = (specifier: string, importer: string) => Promise<string | undefined>
|
|
621
|
+
|
|
622
|
+
function viteResolver(config: ResolvedConfig): Resolver {
|
|
623
|
+
const resolver = config.createResolver()
|
|
624
|
+
return (specifier, importer) => resolver(specifier, importer, false, false)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Generate Tailwind `@source` directives for exactly the published shared UI
|
|
629
|
+
* modules the app's client graph reaches (static, re-exported and literal lazy
|
|
630
|
+
* imports), instead of scanning every component the package ships. The client
|
|
631
|
+
* build then fails if Rolldown rendered a shared UI module the directives
|
|
632
|
+
* missed, so a discovery gap can never ship unstyled.
|
|
633
|
+
*/
|
|
634
|
+
export function tailwindSources(options: TailwindSourcesOptions): Plugin {
|
|
635
|
+
const label = `${options.name}-ui-tailwind-sources`
|
|
636
|
+
const paths = uiSourcePaths(options)
|
|
637
|
+
let uiRoot = ''
|
|
638
|
+
let uiSourceRoot = ''
|
|
639
|
+
let sourcePackageRoots: string[] = []
|
|
640
|
+
let repositoryRoot = ''
|
|
641
|
+
let generatedFiles: string[] = []
|
|
642
|
+
|
|
643
|
+
const generate = async (resolveId: Resolver, config: ResolvedConfig): Promise<string[]> => {
|
|
644
|
+
const files = await collectUiSourceFiles({
|
|
645
|
+
entrypoint: paths.entrypoints,
|
|
646
|
+
repositoryRoot,
|
|
647
|
+
uiPackageRoot: uiRoot,
|
|
648
|
+
uiSourceRoot,
|
|
649
|
+
sourcePackageRoots,
|
|
650
|
+
resolveId,
|
|
651
|
+
label,
|
|
652
|
+
})
|
|
653
|
+
await options.check?.({
|
|
654
|
+
command: config.command,
|
|
655
|
+
repositoryRoot,
|
|
656
|
+
files,
|
|
657
|
+
linkedImports: (specifier) =>
|
|
658
|
+
collectLinkedImports({
|
|
659
|
+
entrypoint: paths.entrypoints,
|
|
660
|
+
repositoryRoot,
|
|
661
|
+
uiSourceRoot,
|
|
662
|
+
sourcePackageRoots,
|
|
663
|
+
specifier,
|
|
664
|
+
resolveId,
|
|
665
|
+
label,
|
|
666
|
+
}),
|
|
667
|
+
})
|
|
668
|
+
writeIfChanged(
|
|
669
|
+
paths.stylesheetPath,
|
|
670
|
+
formatUiSourceDirectives({
|
|
671
|
+
files,
|
|
672
|
+
uiPackageRoot: uiRoot,
|
|
673
|
+
stablePackagePath: paths.stablePackagePath,
|
|
674
|
+
stylesheetPath: paths.stylesheetPath,
|
|
675
|
+
label,
|
|
676
|
+
})
|
|
677
|
+
)
|
|
678
|
+
writeIfChanged(
|
|
679
|
+
paths.manifestPath,
|
|
680
|
+
`${JSON.stringify(packageRelativePaths(files, uiRoot), null, 2)}\n`
|
|
681
|
+
)
|
|
682
|
+
return files
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
return {
|
|
686
|
+
name: label,
|
|
687
|
+
enforce: 'pre',
|
|
688
|
+
async configResolved(config) {
|
|
689
|
+
repositoryRoot = realpathSync(paths.repositoryRoot)
|
|
690
|
+
const resolveId = viteResolver(config)
|
|
691
|
+
const entrypoint = paths.entrypoints[0]
|
|
692
|
+
if (!entrypoint) throw new Error(`[${label}] no client entrypoints`)
|
|
693
|
+
const manifest = await resolveId('@adea-ai/ui/package.json', entrypoint)
|
|
694
|
+
if (!manifest) throw new Error(`[${label}] unable to resolve @adea-ai/ui/package.json`)
|
|
695
|
+
uiRoot = realpathSync(dirname(modulePath(manifest)))
|
|
696
|
+
uiSourceRoot = realpathSync(resolve(uiRoot, 'src'))
|
|
697
|
+
sourcePackageRoots = await frameworkSourceRoots(resolveId, entrypoint, label)
|
|
698
|
+
// Bun links the package here; a hoisted install may not, while the
|
|
699
|
+
// generated directives reference the package through this stable path.
|
|
700
|
+
if (!existsSync(paths.stablePackagePath)) {
|
|
701
|
+
mkdirSync(dirname(paths.stablePackagePath), { recursive: true })
|
|
702
|
+
symlinkSync(uiRoot, paths.stablePackagePath, 'dir')
|
|
703
|
+
}
|
|
704
|
+
if (realpathSync(paths.stablePackagePath) !== uiRoot)
|
|
705
|
+
throw new Error(
|
|
706
|
+
`[${label}] stable package link ${paths.stablePackagePath} does not resolve to ${uiRoot}`
|
|
707
|
+
)
|
|
708
|
+
generatedFiles = await generate(resolveId, config)
|
|
709
|
+
},
|
|
710
|
+
configureServer(server) {
|
|
711
|
+
for (const path of options.watch ?? []) server.watcher.add(resolve(options.webRoot, path))
|
|
712
|
+
},
|
|
713
|
+
async handleHotUpdate({ file, modules, server }) {
|
|
714
|
+
if (!generatedFiles.length || !isCodeFile(file)) return
|
|
715
|
+
const previous = packageRelativePaths(generatedFiles, uiRoot).join('\n')
|
|
716
|
+
generatedFiles = await generate(viteResolver(server.config), server.config)
|
|
717
|
+
if (packageRelativePaths(generatedFiles, uiRoot).join('\n') === previous) return
|
|
718
|
+
// Keep lexical graph keys; aliased roots may use their physical path instead.
|
|
719
|
+
const styles =
|
|
720
|
+
server.moduleGraph.getModulesByFile(normalizePath(paths.styleEntry)) ??
|
|
721
|
+
server.moduleGraph.getModulesByFile(normalizePath(modulePath(paths.styleEntry)))
|
|
722
|
+
if (!styles) return
|
|
723
|
+
for (const module of styles) server.moduleGraph.invalidateModule(module)
|
|
724
|
+
// Returning only the stylesheet would drop the edited module from the
|
|
725
|
+
// update list, so keep the modules Vite already selected for the file.
|
|
726
|
+
return [...new Set([...modules, ...styles])]
|
|
727
|
+
},
|
|
728
|
+
generateBundle(_options, bundle) {
|
|
729
|
+
// Server bundles render shared UI the client never ships; Tailwind only
|
|
730
|
+
// styles the client.
|
|
731
|
+
const environment = (this as { environment?: { config: { consumer?: string } } }).environment
|
|
732
|
+
if (environment && environment.config.consumer !== 'client') return
|
|
733
|
+
const renderedIds: string[] = []
|
|
734
|
+
for (const output of Object.values(bundle)) {
|
|
735
|
+
if (output.type !== 'chunk') continue
|
|
736
|
+
for (const [id, module] of Object.entries(output.modules)) {
|
|
737
|
+
// Rolldown also records wholly tree-shaken modules with zero bytes.
|
|
738
|
+
if (module.renderedLength > 0) renderedIds.push(id)
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (!renderedIds.some((id) => inside(modulePath(id), uiSourceRoot)))
|
|
742
|
+
this.error(`[${label}] the bundle rendered no shared UI source modules`)
|
|
743
|
+
const missing = missingRenderedUiSources(renderedIds, generatedFiles, uiRoot)
|
|
744
|
+
if (missing.length)
|
|
745
|
+
this.error(
|
|
746
|
+
`[${label}] Tailwind source discovery missed rendered shared UI modules:\n${missing.map((path) => `- ${path}`).join('\n')}`
|
|
747
|
+
)
|
|
748
|
+
},
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
function packageRelativePaths(files: string[], uiRoot: string): string[] {
|
|
753
|
+
return files.map((file) => relative(uiRoot, file).replaceAll('\\', '/')).toSorted()
|
|
754
|
+
}
|