-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
30 lines (28 loc) · 960 Bytes
/
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
const loaderUtils = require('loader-utils');
// 默认参数
const defaultopts = {
remUnit: 100, // rem unit value (default: 100)
remFixed: 2, // rem value precision (default: 2)
};
module.exports = function (source) {
// 获取webpack配置好的参数
const opts = loaderUtils.getOptions(this);
// 将参数组合
const config = Object.assign({}, defaultopts, opts);
const ZPXRegExp = /\b(\d+(\.\d+)?)SUPX\b/;
let pxGlobalRegExp = new RegExp(ZPXRegExp.source, 'g');
if (this.cacheable) {
this.cacheable();
}
// 先test下有没有符合的如果有再进行替换
if (pxGlobalRegExp.test(source)) {
return source.replace(pxGlobalRegExp, ($0, $1) => {
let val = $1 / config.remUnit;
// 精确到几位
val = parseFloat(val.toFixed(config.remFixed));
return val === 0 ? val : val + 'rem';
});
} else {
return source;
}
};