|
1 | 1 | var _ = require('underscore');
|
2 |
| -var cssnano = require('cssnano'); |
3 | 2 |
|
4 |
| -function OptimizeCssAssetsPlugin() {}; |
| 3 | +function OptimizeCssAssetsPlugin(options) { |
| 4 | + this.options = options || {}; |
| 5 | + |
| 6 | + if (this.options.assetNameRegExp === undefined) { |
| 7 | + this.options.assetNameRegExp = /\.css$/g; |
| 8 | + } |
| 9 | + |
| 10 | + if (this.options.cssProcessor === undefined) { |
| 11 | + this.options.cssProcessor = require('cssnano'); |
| 12 | + } |
| 13 | + |
| 14 | + if (this.options.cssProcessorOptions === undefined) { |
| 15 | + this.options.cssProcessorOptions = {}; |
| 16 | + } |
| 17 | + |
| 18 | + if (this.options.canPrint === undefined) { |
| 19 | + this.options.canPrint = true; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +OptimizeCssAssetsPlugin.prototype.print = function() { |
| 24 | + if (this.options.canPrint) { |
| 25 | + console.log.apply(console, arguments); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +OptimizeCssAssetsPlugin.prototype.processCss = function(css) { |
| 30 | + return this.options.cssProcessor.process(css, this.options.cssProcessorOptions); |
| 31 | +}; |
| 32 | + |
| 33 | +OptimizeCssAssetsPlugin.prototype.createCssAsset = function(css, originalAsset) { |
| 34 | + return { |
| 35 | + source: function() { |
| 36 | + return css; |
| 37 | + }, |
| 38 | + size: function() { |
| 39 | + return css.length; |
| 40 | + } |
| 41 | + }; |
| 42 | +}; |
5 | 43 |
|
6 | 44 | OptimizeCssAssetsPlugin.prototype.apply = function(compiler) {
|
| 45 | + |
| 46 | + var self = this; |
| 47 | + |
7 | 48 | compiler.plugin('emit', function(compilation, compileCallback) {
|
8 | 49 |
|
9 |
| - console.log(''); |
10 |
| - console.log('Starting to optimize CSS...'); |
| 50 | + self.print('\nStarting to optimize CSS...'); |
11 | 51 |
|
12 |
| - var cssFiles = _.filter(_.keys(compilation.assets), function(fn){ return fn && fn.match && fn.match(/\.css$/g); }); |
| 52 | + var assets = compilation.assets; |
13 | 53 |
|
14 |
| - var counter = 0; |
15 |
| - function checkFinish() { |
16 |
| - if (cssFiles.length >= counter++) { |
17 |
| - console.log('CSS optimize ended.'); |
18 |
| - compileCallback(); |
| 54 | + var cssAssetNames = _.filter( |
| 55 | + _.keys(assets), |
| 56 | + function(assetName) { |
| 57 | + return assetName.match(self.options.assetNameRegExp); |
19 | 58 | }
|
20 |
| - }; |
| 59 | + ); |
| 60 | + |
| 61 | + var hasErrors = false; |
| 62 | + var promises = []; |
21 | 63 |
|
22 | 64 | _.each(
|
23 |
| - cssFiles, |
| 65 | + cssAssetNames, |
24 | 66 | function(assetName) {
|
25 |
| - console.log('Processing ' + assetName); |
26 |
| - var asset = compilation.assets[assetName]; |
| 67 | + |
| 68 | + self.print('Processing ' + assetName + '...'); |
| 69 | + |
| 70 | + var asset = assets[assetName]; |
| 71 | + |
27 | 72 | var originalCss = asset.source();
|
28 |
| - cssnano.process(originalCss, {discardComments: {removeAll: true}}).then( |
| 73 | + |
| 74 | + var promise = self.processCss(originalCss); |
| 75 | + |
| 76 | + promise.then( |
29 | 77 | function (result) {
|
| 78 | + |
| 79 | + if (hasErrors) { |
| 80 | + self.print('Skiping ' + assetName + ' because of an error.'); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
30 | 84 | var processedCss = result.css;
|
31 |
| - compilation.assets[assetName] = { |
32 |
| - source: function() { |
33 |
| - return processedCss; |
34 |
| - }, |
35 |
| - size: function() { |
36 |
| - return processedCss.length; |
37 |
| - } |
38 |
| - }; |
39 |
| - console.log('Processing ' + assetName + ' ended, before: ' + originalCss.length + ', after: ' + processedCss.length + ', ratio: ' + (Math.round(((processedCss.length * 100) / originalCss.length) * 100) / 100) + '%'); |
40 |
| - checkFinish(); |
| 85 | + |
| 86 | + assets[assetName] = self.createCssAsset(processedCss, asset); |
| 87 | + |
| 88 | + self.print('Processed ' + assetName + ', before: ' + originalCss.length + ', after: ' + processedCss.length + ', ratio: ' + (Math.round(((processedCss.length * 100) / originalCss.length) * 100) / 100) + '%'); |
| 89 | + |
41 | 90 | }, function(err) {
|
42 |
| - console.log('Error processing file: ' + assetName); |
43 |
| - console.log(err); |
44 |
| - checkFinish(); |
| 91 | + hasErrors = true; |
| 92 | + self.print('Error processing file: ' + assetName); |
| 93 | + console.error(err); |
45 | 94 | }
|
46 | 95 | );
|
| 96 | + |
| 97 | + promises.push(promise); |
47 | 98 | }
|
48 | 99 | );
|
| 100 | + |
| 101 | + Promise.all(promises).then(function () { compileCallback(); }, compileCallback); |
49 | 102 | });
|
50 | 103 | };
|
51 | 104 |
|
|
0 commit comments