25 lines
1.1 KiB
JavaScript
25 lines
1.1 KiB
JavaScript
// Rejects classes in app.css that no source file uses. A class built from a
|
|
// template such as `status-${kind}` counts as used by its prefix.
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
const src = path.resolve(import.meta.dirname, '..', 'src')
|
|
|
|
test('every class in app.css is used', () => {
|
|
const code = fs.readdirSync(src, { recursive: true })
|
|
.filter((name) => /\.tsx?$/.test(name))
|
|
.map((name) => fs.readFileSync(path.join(src, name), 'utf8'))
|
|
.join('\n')
|
|
const css = fs.readFileSync(path.join(src, 'styles', 'app.css'), 'utf8')
|
|
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
.replace(/url\([^)]*\)|"[^"]*"/g, '')
|
|
|
|
const words = new Set(code.match(/[A-Za-z_][\w-]*/g))
|
|
const prefixes = [...code.matchAll(/([A-Za-z][\w-]*-)\$\{/g)].map((m) => m[1])
|
|
const classes = new Set([...css.matchAll(/\.([A-Za-z_][\w-]*)/g)].map((m) => m[1]))
|
|
assert.ok(classes.size > 300, `found only ${classes.size} classes in app.css`)
|
|
assert.deepEqual([...classes].filter((c) => !words.has(c) && !prefixes.some((p) => c.startsWith(p))), [])
|
|
})
|