/
postcss.js
50 lines (39 loc) 路 1.35 KB
/
postcss.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
const localRequire = require('../utils/localRequire');
const loadPlugins = require('../utils/loadPlugins');
const postcss = require('postcss');
const Config = require('../utils/config');
const cssnano = require('cssnano');
module.exports = async function (asset) {
let config = await getConfig(asset);
if (!config) {
return;
}
await asset.parseIfNeeded();
let res = await postcss(config.plugins).process(asset.getCSSAst(), config);
asset.ast.css = res.css;
asset.ast.dirty = false;
}
async function getConfig(asset) {
let config = asset.package.postcss || await Config.load(asset.name, ['.postcssrc', '.postcssrc.js', 'postcss.config.js']);
if (!config && !asset.options.minify) {
return;
}
config = config || {};
let postcssModulesConfig = {
getJSON: (filename, json) => (asset.cssModules = json)
};
if (config.plugins && config.plugins['postcss-modules']) {
postcssModulesConfig = Object.assign(config.plugins['postcss-modules'], postcssModulesConfig);
delete config.plugins['postcss-modules'];
}
config.plugins = loadPlugins(config.plugins, asset.name);
if (config.modules) {
config.plugins.push(localRequire('postcss-modules', asset.name)(postcssModulesConfig));
}
if (asset.options.minify) {
config.plugins.push(cssnano());
}
config.from = asset.name;
config.to = asset.name;
return config;
}