-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (135 loc) · 5.2 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const stringCalc = require('@johnrob1880/string-calc')
const Rule = '\"[^\"]+\"|\'[^\']+\'|url\\([^)]+\\)|var\\([^)]+\\)|(\\d*\\.?\\d+)';
const computeUnit = (math = '', value = 0, unit = '', decimal = 5) => {
try {
let number = stringCalc(math.replace(/\$word/g, value));
if (typeof number === 'number') {
let powNumber = Math.pow(10, decimal + 1);
let floorNumber = Math.floor(number * powNumber);
let roundNumber = (Math.round(floorNumber / 10) * 10) / powNumber;
return `${roundNumber}${roundNumber !== 0 ? unit : ''}`;
} else {
throw new Error('The formula should not carry strings.');
}
} catch (error) {
return undefined;
}
};
const replaceUnit = (rule = '', math = '', unit = '', value = '') => {
return value.replace(rule, (item, content) => {
if (content !== undefined) {
return computeUnit(math, content, unit) || 0;
} else {
return item;
}
});
};
const verifyPropList = (propList = []) => {
let isHasWild = propList.indexOf('*') !== -1;
let isIgnoreMatch = isHasWild && propList.length === 1;
let isMatchExact = propList.filter(item => item.match(/^[^*!]+$/));
let isMatchBetween = propList.filter(item => item.match(/^\*.+\*$/)).map(item => item.substr(1, item.length - 2));
let isMatchStart = propList.filter(item => item.match(/^[^*!]+\*$/)).map(item => item.substr(0, item.length - 1));
let isMatchEnd = propList.filter(item => item.match(/^\*[^*]+$/)).map(item => item.substr(1));
let isNotMatchExact = propList.filter(item => item.match(/^![^*]+$/)).map(item => item.substr(1));
let isNotMatchBetween = propList.filter(item => item.match(/^!\*.+\*$/)).map(item => item.substr(2, item.length - 3));
let isNotMatchStart = propList.filter(item => item.match(/^![^*]+\*$/)).map(item => item.substr(1, item.length - 2));
let isNotMatchEnd = propList.filter(item => item.match(/^!\*[^*]+$/)).map(item => item.substr(2));
return prop => {
if (isIgnoreMatch) { return true; }
if (isNotMatchExact.indexOf(prop) !== -1) {
return false;
} else if (isNotMatchBetween.some(item => prop.indexOf(item) !== -1)) {
return false;
} else if (isNotMatchStart.some(item => prop.indexOf(item) === 0)) {
return false;
} else if (isNotMatchEnd.some(item => prop.indexOf(item) === prop.length - item.length)) {
return false;
} else if (isMatchExact.indexOf(prop) !== -1) {
return true;
} else if (isMatchBetween.some(item => prop.indexOf(item) !== -1)) {
return true;
} else if (isMatchStart.some(item => prop.indexOf(item) === 0)) {
return true;
} else if (isMatchEnd.some(item => prop.indexOf(item) === prop.length - item.length)) {
return true;
} else {
return isHasWild;
}
};
};
const initOptions = options => {
let defaultOptions = { unitList: [], propList: ['*'], replace: true, media: true, exclude: options.exclude || /node_modules/i };
if (typeof options === 'object') {
if (Array.isArray(options.unitList)) {
options.unitList.forEach(item => {
if (item.math && item.word && item.unit) {
item.rule = new RegExp(Rule + item.word, 'g');
if (computeUnit(item.math, 0, item.unit) !== undefined) {
defaultOptions.unitList.push(item);
}
}
});
}
if (Array.isArray(options.propList)) {
defaultOptions.propList = options.propList;
}
if (typeof options.replace === 'boolean') {
defaultOptions.replace = options.replace;
}
if (typeof options.media === 'boolean') {
defaultOptions.media = options.media;
}
}
return defaultOptions;
};
module.exports = (options = {}) => {
let isExcludeFile = false;
let that = this;
that.options = initOptions(options);
that.verifyProp = verifyPropList(that.options.propList);
return {
postcssPlugin: 'postcss-unitlist',
Once (css) {
if (that.options.exclude && css.source.input.file) {
if (typeof that.options.exclude === 'function') {
isExcludeFile = that.options.exclude(css.source.input.file);
} else if (typeof that.options.exclude === 'string') {
isExcludeFile = css.source.input.file.indexOf(that.options.exclude) !== -1;
} else {
isExcludeFile = css.source.input.file.match(that.options.exclude) !== null;
}
}
},
Declaration (decl) {
if (isExcludeFile) return false;
that.options.unitList.some(item => {
if (item.rule.test(decl.value) && that.verifyProp(decl.prop)) {
let value = replaceUnit(item.rule, item.math, item.unit, decl.value);
if (that.options.replace) {
decl.value = value;
} else {
decl.cloneAfter({ value: value });
}
return true;
} else {
return false;
}
});
},
AtRule (atRule) {
if (isExcludeFile) return false;
if (that.options.media && atRule.name === 'media') {
that.options.unitList.some(item => {
if (item.rule.test(atRule.params)) {
atRule.params = replaceUnit(item.rule, item.math, item.unit, atRule.params);
return true;
} else {
return false;
}
});
}
},
};
};
module.exports.postcss = true;