-
Notifications
You must be signed in to change notification settings - Fork 7
/
clean-css-plugin.js
55 lines (46 loc) · 1.67 KB
/
clean-css-plugin.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
/*eslint no-console: ["error", { allow: ["warn", "error"] }] */
var fs = require('fs');
var CleanCSS = require('clean-css');
const sources = [];
class CleanCssPlugin {
constructor(options) {
this.options = Object.assign({
filename: 'bundle'
}, options);
}
apply(compiler) {
compiler.hooks.done.tap('CleanCss Plugin', (compilation) => {
var bundle = new CleanCSS({
sourceMap: true,
rebaseTo: `./dist/${this.options.filename}.min.css`
}).minify([
'./assets/css/normalize.css',
'./assets/css/base.css',
'./assets/css/layout.css',
'./assets/css/field.css',
'./assets/css/effect.css',
'./assets/css/manager.css',
'./assets/css/loader.css',
'./assets/css/editor.css',
]);
// log bundle result
outputFeedback(bundle.errors, true);
outputFeedback(bundle.warnings);
// write bundle file
output({ path: `./dist/${this.options.filename}.min.css` }, bundle.styles);
output({ path: `./dist/${this.options.filename}.min.map` }, bundle.sourceMap);
});
}
}
function outputFeedback(messages, isError) {
var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:';
messages.forEach(function (message) {
console.error('%s %s', prefix, message);
});
}
function output(options, minified) {
fs.writeFileSync(options.path, minified, 'utf8');
}
CleanCssPlugin.sources = sources;
CleanCssPlugin.loader = require.resolve('./clean-css-loader');
module.exports = CleanCssPlugin;