-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (53 loc) · 1.61 KB
/
index.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
53
54
55
56
57
58
59
60
const varConvert = require("./css-vars2sassVars");
const postcss = require("postcss");
const fs = require("fs");
const sass = require("node-sass");
/**
* css-vars2value
*
* default options
* @param rootSelector string - global vars inject selector
* @param data string - css text
* @param input string - input css file path
* @param output string - output path
* @params defaultVars - object set default global vars
* @params sassRenderOptions - object send options to sass render
*/
module.exports = (opt) => {
const options = Object.assign(
{
rootSelector: undefined,
data: undefined,
input: undefined,
output: undefined,
defaultVars: {
"tw-empty": "none",
},
sassRenderOptions: {},
},
opt
);
const cssContent = options.data
? options.data
: fs.readFileSync(options.input, "utf-8");
if (!cssContent)
throw new Error(
"css-vars2value: cannot resolve css content, pls make sure provide data or input args"
);
let defaultValuesString = "";
Object.keys(options.defaultVars).map((key) => {
defaultValuesString += `$${key}:${options.defaultVars[key]};`;
});
const sassContent = postcss([
varConvert({ rootSelector: options.rootSelector }),
]).process(cssContent).css;
// fs.writeFileSync('./output.scss',sassContent)
const result = sass.renderSync(
Object.assign(options.sassRenderOptions, {
data: `${defaultValuesString}${sassContent}`,
})
);
return options.output
? fs.writeFileSync(options.output, result.css, "utf-8")
: result.css;
};