UNPKG

1.4 kBJavaScriptView Raw
1import fs from 'fs'
2
3export const buildPropsStylesheet = ({filename,props}, {selector,prefix}) => {
4 const file = fs.createWriteStream("../src/" + filename)
5
6 let appendedMeta = ''
7
8 if (filename.includes('shadows')) {
9 file.write(`@import 'props.media.css';\n\n`)
10 let dark_propsMeta = ``
11 let dark_props = Object.entries(props)
12 .filter(([prop, val]) =>
13 prop.includes('-@media:dark'))
14
15 dark_props.forEach(([prop, val], index) => {
16 let v = props[prop]
17 let extract = prop.slice(2, prop.length-('-@media:dark'.length))
18 let p = prefix && prefix !== "''"
19 ? `--${prefix}-` + extract
20 : `--${extract}`
21
22 dark_propsMeta += ` ${p}: ${val};${index !== dark_props.length-1 ? '\n' : ''}`
23 })
24 appendedMeta += `
25@media (--OSdark) {
26 ${selector} {
27${dark_propsMeta}
28 }
29}`
30 }
31
32 file.write(`${selector} {\n`)
33
34 Object.entries(props).forEach(([prop, val]) => {
35 if (prop.includes('-@'))
36 return
37
38 if (prop.includes('animation')) {
39 let keyframes = props[`${prop}-@`]
40 appendedMeta += keyframes
41 }
42
43 if (prefix && prefix !== "''") {
44 prop = `--${prefix}-` + prop.slice(2)
45 if (typeof(val) == "string" && val.includes("var(--"))
46 val = val.replace(/var\(--/g, `var(--${prefix}-`)
47 }
48
49 file.write(` ${prop}: ${val};\n`)
50 })
51
52 file.write('}\n')
53 file.end(appendedMeta)
54}
\No newline at end of file