-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild-theme.js
More file actions
64 lines (56 loc) · 2.23 KB
/
build-theme.js
File metadata and controls
64 lines (56 loc) · 2.23 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import postcss from 'postcss';
import presetEnv from 'postcss-preset-env';
import path from 'path';
import { colors, troikaAliases, textColors } from './src/utils/tailwind/colors.js';
import drnm from 'drnm'
// NB - after running this script you need to change tabs to spaces, and add ", :host" to the top ":root" declaration in theme.css
// also this thing should just get automated at some point and theme.css should be a build artifact, but whatever
const __dirname = drnm(import.meta.url)
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? `${parseInt(result[1], 16)},${parseInt(result[2], 16)},${parseInt(result[3], 16)}` : null;
}
const generateCustomColorProperties = (obj, toRgb = false) =>
Object.entries(obj).reduce((acc, [k, v]) => {
if (v === Object(v))
Object.entries(v).forEach(
([level, hex]) =>
(acc[`--f-${k.toLowerCase()}-${level}${toRgb ? '-rgb' : ''}`] = toRgb
? hexToRgb(hex)
: hex),
);
// handle single-color definitions like 'white: #fff'
else acc[`--f-${k}`] = v;
return acc;
}, {});
const generateNamespacedProperties = (obj, namespace) =>
Object.entries(obj).reduce((acc, [k, v]) => {
acc[`--f-${namespace}-${k}`] = v;
return acc;
}, {});
const colorProperties = generateCustomColorProperties(colors);
const textProperties = generateNamespacedProperties(textColors, 'text');
const rgbColorProperties = generateCustomColorProperties(colors, true);
const troikaProperties = generateCustomColorProperties(troikaAliases);
async function main() {
await postcss(
presetEnv({
stage: 0,
browsers: 'ie 11', // ensures variables are exported
importFrom: {
customProperties: {
...colorProperties,
...rgbColorProperties,
...troikaProperties,
...textProperties,
},
},
exportTo: path.resolve(__dirname, './src/theme.css'),
}),
).process('', { from: undefined });
}
try {
main();
} catch (err) {
console.error(err);
}