-
Notifications
You must be signed in to change notification settings - Fork 69
/
index.js
99 lines (70 loc) · 2.72 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
var _ = require('underscore');
var webpackSources = require('webpack-sources');
function OptimizeCssAssetsPlugin(options) {
this.options = options || {};
if (this.options.assetNameRegExp === undefined) {
this.options.assetNameRegExp = /\.css$/g;
}
if (this.options.cssProcessor === undefined) {
this.options.cssProcessor = require('cssnano');
}
if (this.options.cssProcessorOptions === undefined) {
this.options.cssProcessorOptions = {};
}
if (this.options.canPrint === undefined) {
this.options.canPrint = true;
}
};
OptimizeCssAssetsPlugin.prototype.print = function() {
if (this.options.canPrint) {
console.log.apply(console, arguments);
}
};
OptimizeCssAssetsPlugin.prototype.processCss = function(css, assetName) {
return this.options.cssProcessor.process(css, Object.assign({ to: assetName }, this.options.cssProcessorOptions));
};
OptimizeCssAssetsPlugin.prototype.createCssAsset = function(css, originalAsset) {
return new webpackSources.RawSource(css);
};
OptimizeCssAssetsPlugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('emit', function(compilation, compileCallback) {
self.print('\nStarting to optimize CSS...');
var assets = compilation.assets;
var cssAssetNames = _.filter(
_.keys(assets),
function(assetName) {
return assetName.match(self.options.assetNameRegExp);
}
);
var hasErrors = false;
var promises = [];
_.each(
cssAssetNames,
function(assetName) {
self.print('Processing ' + assetName + '...');
var asset = assets[assetName];
var originalCss = asset.source();
var promise = self.processCss(originalCss, assetName);
promise.then(
function (result) {
if (hasErrors) {
self.print('Skiping ' + assetName + ' because of an error.');
return;
}
var processedCss = result.css;
assets[assetName] = self.createCssAsset(processedCss, asset);
self.print('Processed ' + assetName + ', before: ' + originalCss.length + ', after: ' + processedCss.length + ', ratio: ' + (Math.round(((processedCss.length * 100) / originalCss.length) * 100) / 100) + '%');
}, function(err) {
hasErrors = true;
self.print('Error processing file: ' + assetName);
console.error(err);
}
);
promises.push(promise);
}
);
Promise.all(promises).then(function () { compileCallback(); }, compileCallback);
});
};
module.exports = OptimizeCssAssetsPlugin;