forked from mzohaibqc/antd-theme-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postscc-less-parser.js
77 lines (75 loc) · 1.78 KB
/
postscc-less-parser.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const fs = require('fs');
const path = require('path');
const postcss = require('postcss');
const syntax = require('postcss-less');
const cssss = `
@prefix: 'ant';
@{prefix}-btn {
color: red;
}
`;
const result = syntax.parse(antdComponentsLess);
const properties = [
"background",
"background-color",
"border",
"border-left",
"border-right",
"border-bottom",
"border-top",
"text-shadow",
"box-shadow",
];
const invalidColors = []; //['none', 'transparent'];
const numericValueRegex = /^\d+(%|ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)$/;
const processNode = ({
type,
name,
prop,
value,
selector,
nodes,
variable,
...rest
}) => {
if (type === "atrule" && !variable) {
// console.log('processNode -> name', name, variable, selector)
return "";
}
if (type === "decl") {
if (numericValueRegex.test(value)) {
return "";
}
if (invalidColors.indexOf(value) > -1) {
return "";
}
if (properties.indexOf(prop) === -1 && !prop.includes("color")) {
return "";
} else {
return `${prop}: ${value};`;
}
} else if (type === "comment") {
// ignore
return "";
} else if (type === "rule") {
const innerContent = nodes.map(processNode).join("");
if (!innerContent) return "";
return `${selector} {
${innerContent}
}`;
} else {
// alrule i.e, @media etc.
// console.log('@atrule', type, prop, value, selector, nodes, variable, rest)
// if (variable) {
// return `@${name}: ${value};\n`;
// }
return "";
}
};
let out = result.nodes.map(processNode).join("\n");
// console.log('out', out)
out = out
.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "")
.replace(/^\s*$(?:\r\n?|\n)/gm, "");
// console.log('out', out)
fs.writeFileSync("./results.less", out);