-
Notifications
You must be signed in to change notification settings - Fork 11
/
css-props-generator.js
52 lines (41 loc) · 1.23 KB
/
css-props-generator.js
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
const fs = require('fs');
const prettier = require('prettier');
const config = require('./tailwind.config.js');
/*
Converts the tailwind config elements into custom props.
*/
const generateCSSProps = () => {
let result = '';
const groups = [
{key: 'colors', prefix: 'color'},
{key: 'spacing', prefix: 'space'},
{key: 'fontSize', prefix: 'size'}
];
// Add a note that this is auto generated
result += `
/* VARIABLES GENERATED WITH TAILWIND CONFIG ON ${new Date().toLocaleDateString()}.
Tokens location: ./tailwind.config.js */
:root {
`;
// Loop each group's keys, use that and the associated
// property to define a :root custom prop
groups.forEach(({key, prefix}) => {
const group = config.theme[key];
if (!group) {
return;
}
Object.keys(group).forEach(key => {
result += `--${prefix}-${key}: ${group[key]};`;
});
});
// Close the :root block
result += `
}
`;
// Make the CSS readable to help people with auto-complete in their editors
result = prettier.format(result, {parser: 'scss'});
// Push this file into the CSS dir, ready to go
fs.writeFileSync('./src/css/custom-props.css', result);
};
generateCSSProps();
module.exports = generateCSSProps;