/
index.js
134 lines (124 loc) · 4.99 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
/**
* @param {object} unitToConvert unit lits to convert, by default, it is {rem:"rx",vw:"wx",vh:"hx"}
* @param {Number} viewportWidth The width of the viewport.Default is 1920. -viewportWidth = the width of UI design
* @param {Number} viewportHeight The height of the viewport.Default is 1080.-viewportHeight = the height of UI design
* @param {Number} rootValue The root element font size. Default is 100. {html:100wx=100px*100/1920vw}
* @param {object} viewportwidthUnit Expected with units.
* @param {object} viewportheightUnit Expected height units.
* @param {object} viewportfontUnit Expected units for font.
* @param {Number} unitPrecision The decimal numbers to allow the REM or VW OR VH units to grow to.
* @param {Number} minPixelValue Set the minimum pixel value to replace.
* @param {Number} exclude (Reg) a way to exclude some folder,eg. /(node_module)/.
* @param {Number} mediaQuery Allow unit(rx\wx\hx) to be converted in media queries
* @param {Number} replace replaces rules containing unit(rx\wx\hx) instead of adding fallbacks.
*
*/
var postcss = require('postcss');
var defaultOpts = {
unitToConvert: {rem:"rx",vw:"wx",vh:"hx"},
viewportWidth: 1920,
viewportHeight: 1080,
rootValue:100,
viewportwidthUnit:'vw',
viewportheightUnit: 'vh',
viewportfontUnit: 'rem',
unitPrecision: 5,
minPixelValue: 1,
exclude:false,
mediaQuery: false,
replace:true
};
/**
* @param {Object} number
* @param {Number} precision //defaultOpts.unitPrecision
*/
const toFixed = (number, precision) => {
const multiplier = Math.pow(10, precision + 1);
const wholeNumber = Math.floor(number * multiplier);
return Math.round(wholeNumber / 10) * 10 / multiplier;
};
//Round To -Infinite
const isObject = o => typeof o === 'object' && o !== null;
/**
* @param {String} unit(rx\wx\hx)
*/
const getUnitRegexp = unit => {
const pattern = `"[^"]+"|\'[^\']+\'|url\\([^\\)]+\\)|(\\d*\\.?\\d+)(${unit})`;
return new RegExp(pattern, 'ig');
}//正则匹配符合条件css
/**
* @param {Object} decls //Special style
* @param {String} prop //Style properties
* @param {String} value //Style value
*/
const declarationExists = (decls, prop, value) => decls.some(decl =>
decl.prop === prop && decl.value === value
);
/** unit(rx/vw/vh) to unit(rem/vw/vh)
* @param {Object} opts //defaultOpts
* @param {String} defunit //unit
*/
const createPxReplace = (opts,defunit) => (m, $1) => {
if (!$1) return m;
const pixels = parseFloat($1);
if (pixels < opts.minPixelValue) return m;
let baseValue;
switch(defunit) {
case opts.viewportfontUnit:
baseValue = opts.rootValue
break;
case opts.viewportwidthUnit:
baseValue = opts.viewportWidth
break;
case opts.viewportheightUnit:
baseValue = opts.viewportHeight
break;
default:
baseValue = opts.rootValue
}
const fixedVal = toFixed((pixels / baseValue)*(defunit==="rem"?1:100), opts.unitPrecision);
return `${fixedVal}${defunit}`;
};
/**The recursive single subunit of a CSS file transformed by postcss Parser
* root(css),rule, nodes, decl, prop, value
* @param {object} root //the entire CSS snippet, which contains multiple rules
* @param {object} rule //the snippet within the scope of a CSS
* @param {object} nodes //Refers to multiple decl parts in the middle of {} in the rule
* @param {object} decl //Special style,
* @param {String} prop //Style properties
* @param {String} value //Style value
*/
module.exports = postcss.plugin('postcss-plugin-px2units', options => {
const opts = { ...defaultOpts, ...options };
return css => {
console.log(css.walkDecls,"css")
for (let defunit in opts.unitToConvert) {
let unit = opts.unitToConvert[defunit]
const pxReplace = createPxReplace(opts,defunit);
css.walkDecls((decl, i) => {
const _decl = decl;
// 1st check exclude
if (opts.exclude && css.source.input.file && css.source.input.file.match(opts.exclude) !== null) return;
// 2st check 'rx\wx\hx' unit
if (_decl.value.indexOf(unit) === -1) return;
const value = _decl.value.replace(getUnitRegexp(unit), pxReplace);
// if rem unit already exists, do not add or replace
if (declarationExists(_decl.parent, _decl.prop, value)) return;
if (opts.replace) {
_decl.value = value;
} else {
_decl.parent.insertAfter(i, _decl.clone({
value,
}));
}
});
if (opts.mediaQuery) {
css.walkAtRules('media', rule => {
const _rule = rule;
if (_rule.params.indexOf(unit) === -1) return;
_rule.params = _rule.params.replace(getUnitRegexp(unit), pxReplace);
});
}
}
};
});