Repository navigation
Expand file tree
/
Copy pathpostcss.config.js
More file actions
42 lines (39 loc) · 1.86 KB
/
Copy pathpostcss.config.js
File metadata and controls
42 lines (39 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// PostCSS pipeline — Tailwind + autoprefixer, guarded by the §2.11 twin.
//
// DES-VISION-001 §2.11: no .css file outside the token sources ships a raw
// color. ESLint enforces the contract for .ts/.tsx (eslint.config.mjs); this
// inline plugin is the build-time twin for the stylesheets — it fails the
// build on a literal hex / rgb() / hsl() in any src/ stylesheet EXCEPT
// src/styles/tokens.css and src/styles/themes/*.css (the only files allowed
// to hold raw values). It checks in `Once`, BEFORE Tailwind expands its
// directives, so it judges exactly what the repo's stylesheets say — not the
// generated utilities or preflight (not ours to police). Files outside src/
// (e.g. xterm's CSS from node_modules) are skipped for the same reason.
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
const RAW_COLOR = /#[0-9a-fA-F]{3,8}\b|\brgba?\(\s*\d|\bhsla?\(\s*[\d.]/;
const TOKEN_SOURCES = /src\/styles\/(tokens\.css|themes\/[^/]+\.css)$/;
function noRawColors() {
return {
postcssPlugin: 'no-raw-colors',
Once(root) {
// Judge each declaration by ITS OWN source file (an @import-inlined node
// keeps the imported file as its source), so the token sources stay exempt
// no matter whether the import was inlined before or after this pass.
root.walkDecls((decl) => {
const file = (decl.source?.input?.file ?? '').replace(/\\/g, '/');
if (!file.includes('/src/') || file.includes('/node_modules/')) return;
if (TOKEN_SOURCES.test(file)) return;
if (RAW_COLOR.test(decl.value)) {
throw decl.error(
`Raw color in "${decl.prop}: ${decl.value}" — use a semantic token from src/styles/tokens.css (DES-VISION-001 §2.11).`,
);
}
});
},
};
}
noRawColors.postcss = true;
export default {
plugins: [noRawColors, tailwindcss, autoprefixer],
};