-
Notifications
You must be signed in to change notification settings - Fork 2
/
tailwind-theme-replacements.js
48 lines (41 loc) · 1.16 KB
/
tailwind-theme-replacements.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
const fs = require('fs')
const path = require('path')
const postcss = require('postcss')
function isInsideRoot(rule) {
return rule.selectors.length !== 1 || rule.selectors[0] !== ':root' || rule.parent.type !== 'root'
}
function isVariableDeclaration(decl) {
return Boolean(decl.value) && decl.prop.startsWith('--')
}
function parse(css, options) {
const root = postcss.parse(css, {
from: options.from,
parser: options.parser,
})
const variables = {}
root.walkRules(rule => {
if (isInsideRoot(rule)) {
return
}
rule.each(decl => {
if (isVariableDeclaration(decl)) {
const name = decl.prop.slice(2)
variables[name] = decl.value
}
})
})
return variables
}
function parseFileSync(fileName, options) {
const css = fs.readFileSync(fileName, 'utf-8')
return parse(css, { ...options, from: fileName })
}
function themeReplacements() {
const fileName = path.resolve(__dirname, './styles/theme.scss')
const variables = parseFileSync(fileName, {})
return Object.entries(variables).reduce((memo, e) => {
memo[e[0]] = `var(--${e[0]})`
return memo
}, {})
}
module.exports = { themeReplacements }