-
Notifications
You must be signed in to change notification settings - Fork 1
/
postcss.config.js
79 lines (67 loc) · 2.02 KB
/
postcss.config.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
'use strict'
const qs = require('querystring');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const toCamelCase = (str) => str.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
module.exports = function(options = {}) {
const plugins = {
'postcss-import': {},
'postcss-move-props-to-bg-image-query': {
transform({ name, value }) {
return {
name: name.replace(/^-svg-mixer-/, ''),
value: encodeURIComponent(value)
}
}
},
'postcss-pxtorem': {
rootValue: 75,
unitPrecision: 6,
replace: true,
mediaQuery: false,
minPixelValue: 2,
propList: ['*'],
selectorBlackList: ['no-rem-', 'van-', 'mint-', 'weui-', '__nuxt-']
},
'autoprefixer': {}
}
if (options.webpack) {
const webpack = options.webpack;
const compiler = webpack._compiler;
const entry = compiler.options.entry;
const entryConfig = entry[Object.keys(entry)[0]];
let entryFile;
if (Array.isArray(entryConfig)) {
entryFile = entryConfig[entryConfig.length - 1];
}
else {
entryFile = entryConfig;
}
// 取入口所在目录的构建配置
const projectConfigFile = path.join(path.dirname(entryFile), 'project.config.js');
if (fs.existsSync(projectConfigFile)) {
const projectConfig = require(projectConfigFile);
if (_.isPlainObject(projectConfig.px2rem)) {
_.merge(plugins['postcss-pxtorem'], projectConfig.px2rem);
}
else if (projectConfig.px2rem === false) {
plugins['postcss-pxtorem'] = false;
}
}
if (webpack.resourceQuery) {
const config = qs.parse(webpack.resourceQuery.slice(1));
if (config['no-px2rem'] !== undefined || config.noPx2rem !== undefined) {
plugins['postcss-pxtorem'] = false;
}
else if (plugins['postcss-pxtorem']) {
for (const key in config) {
plugins['postcss-pxtorem'][toCamelCase(key)] = config[key];
}
}
}
}
return {
plugins
}
}