From ff58b03e219350a76a744f8cce1a94f1e74d5228 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 25 Nov 2026 11:38:48 +0000 Subject: [PATCH] test(webui): guard tailwind @source against missing perfect-design components Add a spec that maps every component imported from perfect-design, and every directory it imports in turn, to an @source entry in main.css. Extend the bundle gate spec to check that utilities unique to recently added components appear in the built CSS. --- webui/scripts/bundle-size.d.mts | 1 + webui/scripts/bundle-size.mjs | 7 ++ webui/src/lib/bundle-gate.spec.ts | 13 ++- webui/src/lib/tailwind-sources.spec.ts | 118 +++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 webui/src/lib/tailwind-sources.spec.ts diff --git a/webui/scripts/bundle-size.d.mts b/webui/scripts/bundle-size.d.mts index 0974642..6e15784 100644 --- a/webui/scripts/bundle-size.d.mts +++ b/webui/scripts/bundle-size.d.mts @@ -3,3 +3,4 @@ export const DIST: string export function hasDist(): boolean export function measure(dir?: string): { file: string; gzip: number }[] export function totalGzip(dir?: string): number +export function readCss(dir?: string): string diff --git a/webui/scripts/bundle-size.mjs b/webui/scripts/bundle-size.mjs index 74149b3..6903f92 100644 --- a/webui/scripts/bundle-size.mjs +++ b/webui/scripts/bundle-size.mjs @@ -9,6 +9,13 @@ export const DIST = fileURLToPath(new URL('../dist/assets', import.meta.url)) export const hasDist = () => existsSync(DIST) +export function readCss(dir = DIST) { + return readdirSync(dir) + .filter((f) => f.endsWith('.css')) + .map((f) => readFileSync(join(dir, f), 'utf8')) + .join('\n') +} + export function totalGzip(dir = DIST) { return measure(dir).reduce((n, r) => n + r.gzip, 0) } diff --git a/webui/src/lib/bundle-gate.spec.ts b/webui/src/lib/bundle-gate.spec.ts index 7934c01..c334e60 100644 --- a/webui/src/lib/bundle-gate.spec.ts +++ b/webui/src/lib/bundle-gate.spec.ts @@ -1,10 +1,21 @@ import { describe, expect, it } from 'vitest' -import { hasDist, LIMIT, totalGzip } from '../../scripts/bundle-size.mjs' +import { hasDist, LIMIT, readCss, totalGzip } from '../../scripts/bundle-size.mjs' // Needs a prior `npm run build`; skipped when dist/ is absent. describe.skipIf(!hasDist())('bundle gate', () => { it('keeps gzip JS+CSS within the size limit', () => { expect(totalGzip()).toBeLessThanOrEqual(LIMIT) }) + + // Utilities that only one perfect-design component directory emits; a missing @source entry drops them from the built CSS. + it.each([ + ['NativeSelect', '.\\[\\&_option\\]\\:bg-popover'], + ['Button', '.hover\\:bg-destructive\\/90'], + ['Badge', '.border-primary\\/50'], + ['Card', '.\\@container\\/card-header'], + ['Input', '.end-11'], + ])('emits the %s utility in the built CSS', (_name, selector) => { + expect(readCss()).toContain(selector) + }) }) diff --git a/webui/src/lib/tailwind-sources.spec.ts b/webui/src/lib/tailwind-sources.spec.ts new file mode 100644 index 0000000..63ab7a9 --- /dev/null +++ b/webui/src/lib/tailwind-sources.spec.ts @@ -0,0 +1,118 @@ +/// +import { readFileSync } from 'node:fs' + +import { describe, expect, it } from 'vitest' + +// vitest replaces CSS imports (even ?raw) with an empty string, so read the file directly. +const mainCss = readFileSync(new URL('../assets/main.css', import.meta.url), 'utf8') + +// Tailwind only emits utilities for the perfect-design component directories listed as @source in main.css. +// An imported component whose directory is missing renders unstyled in the production build. + +const PD_DIST = '/node_modules/@perfect-design/ui/dist/components/' + +const sources = import.meta.glob('/src/**/*.{vue,ts}', { query: '?raw', import: 'default', eager: true }) +const indexes = import.meta.glob('/node_modules/@perfect-design/ui/dist/components/*/*/index.d.ts', { + query: '?raw', + import: 'default', + eager: true, +}) +const scripts = import.meta.glob('/node_modules/@perfect-design/ui/dist/components/**/*.js', { query: '?raw', import: 'default' }) + +const importRe = /import\s+(type\s+)?\{([^}]*)\}\s*from\s*'@perfect-design\/ui'/g + +function usedNames(): string[] { + const names = new Set() + for (const [path, text] of Object.entries(sources)) { + if (path.endsWith('.spec.ts')) continue + for (const m of text.matchAll(importRe)) { + if (m[1]) continue + for (const part of m[2].split(',')) { + const name = part.trim().split(/\s+as\s+/)[0] + if (name && !name.startsWith('type ')) names.add(name) + } + } + } + return [...names] +} + +/** export name -> "/", read from each component directory's index.d.ts. */ +function exportOwners(): Map { + const owners = new Map() + for (const [path, text] of Object.entries(indexes)) { + const dir = path.slice(PD_DIST.length).replace(/\/index\.d\.ts$/, '') + for (const m of text.matchAll(/export\s*\{([^}]*)\}\s*from/g)) { + for (const part of m[1].split(',')) { + const name = part.trim().split(/\s+as\s+/).pop() + if (name) owners.set(name, dir) + } + } + } + return owners +} + +function coveredDirs(): { dirs: Set; groups: Set } { + const dirs = new Set() + const groups = new Set() + for (const m of mainCss.matchAll(/^@source\s+"[^"]*\/dist\/components\/([^"]+)";/gm)) { + const target = m[1].replace(/\/\*\*\/\*\.js$/, '') + if (target.endsWith('/*.js')) groups.add(target.slice(0, -'/*.js'.length)) + else dirs.add(target) + } + return { dirs, groups } +} + +/** Directories (and group roots) a component directory pulls in through its relative imports. */ +async function dependencyDirs(start: string, seen: Set, roots: Set): Promise { + if (seen.has(start)) return + seen.add(start) + const prefix = `${PD_DIST}${start}/` + for (const [path, load] of Object.entries(scripts)) { + if (!path.startsWith(prefix) || path.endsWith('.map')) continue + const text = await load() + for (const m of text.matchAll(/from\s*"(\.[^"]+)"/g)) { + const resolved = resolve(path, m[1]) + if (!resolved.startsWith(PD_DIST)) continue + const parts = resolved.slice(PD_DIST.length).split('/') + if (parts.length === 2) roots.add(parts[0]) + else await dependencyDirs(`${parts[0]}/${parts[1]}`, seen, roots) + } + } +} + +function resolve(from: string, rel: string): string { + const out = from.split('/').slice(0, -1) + for (const seg of rel.split('/')) { + if (seg === '..') out.pop() + else if (seg !== '.') out.push(seg) + } + return out.join('/') +} + +describe('main.css @source coverage', () => { + const { dirs, groups } = coveredDirs() + + it('lists component directories at all', () => { + expect(dirs.size).toBeGreaterThan(10) + }) + + it('covers the directory of every component imported from @perfect-design/ui', () => { + const owners = exportOwners() + const missing = usedNames() + .map((name) => ({ name, dir: owners.get(name) })) + .filter((e): e is { name: string; dir: string } => e.dir !== undefined && !dirs.has(e.dir)) + expect(missing).toEqual([]) + }) + + it('covers every directory those components import', async () => { + const owners = exportOwners() + const seen = new Set() + const roots = new Set() + for (const name of usedNames()) { + const dir = owners.get(name) + if (dir) await dependencyDirs(dir, seen, roots) + } + expect([...seen].filter((d) => !dirs.has(d))).toEqual([]) + expect([...roots].filter((g) => !groups.has(g))).toEqual([]) + }) +})