@@ -3,96 +3,99 @@ var webpackSources = require('webpack-sources');
33
44function OptimizeCssAssetsPlugin ( options ) {
55 this . options = options || { } ;
6-
6+
77 if ( this . options . assetNameRegExp === undefined ) {
88 this . options . assetNameRegExp = / \. c s s $ / g;
99 }
10-
10+
1111 if ( this . options . cssProcessor === undefined ) {
1212 this . options . cssProcessor = require ( 'cssnano' ) ;
1313 }
14-
14+
1515 if ( this . options . cssProcessorOptions === undefined ) {
1616 this . options . cssProcessorOptions = { } ;
1717 }
18-
18+
1919 if ( this . options . canPrint === undefined ) {
2020 this . options . canPrint = true ;
2121 }
2222} ;
2323
2424OptimizeCssAssetsPlugin . prototype . print = function ( ) {
2525 if ( this . options . canPrint ) {
26- console . log . apply ( console , arguments ) ;
26+ console . log . apply ( console , arguments ) ;
2727 }
2828} ;
2929
3030OptimizeCssAssetsPlugin . prototype . processCss = function ( css , assetName ) {
31- return this . options . cssProcessor . process ( css , Object . assign ( { to : assetName } , this . options . cssProcessorOptions ) ) ;
31+ return this . options . cssProcessor . process ( css , Object . assign ( { to : assetName } , this . options . cssProcessorOptions ) ) ;
3232} ;
3333
3434OptimizeCssAssetsPlugin . prototype . createCssAsset = function ( css , originalAsset ) {
3535 return new webpackSources . RawSource ( css ) ;
3636} ;
3737
3838OptimizeCssAssetsPlugin . prototype . apply = function ( compiler ) {
39-
39+
4040 var self = this ;
41-
41+
4242 compiler . plugin ( 'emit' , function ( compilation , compileCallback ) {
43-
43+
4444 self . print ( '\nStarting to optimize CSS...' ) ;
45-
45+
4646 var assets = compilation . assets ;
47-
47+
4848 var cssAssetNames = _ . filter (
49- _ . keys ( assets ) ,
50- function ( assetName ) {
49+ _ . keys ( assets ) ,
50+ function ( assetName ) {
5151 return assetName . match ( self . options . assetNameRegExp ) ;
5252 }
5353 ) ;
54-
54+
5555 var hasErrors = false ;
5656 var promises = [ ] ;
57-
57+
5858 _ . each (
5959 cssAssetNames ,
6060 function ( assetName ) {
61-
61+
6262 self . print ( 'Processing ' + assetName + '...' ) ;
63-
63+
6464 var asset = assets [ assetName ] ;
65-
65+
6666 var originalCss = asset . source ( ) ;
67-
68- var promise = self . processCss ( originalCss , assetName ) ;
69-
70- promise . then (
71- function ( result ) {
72-
73- if ( hasErrors ) {
74- self . print ( 'Skiping ' + assetName + ' because of an error.' ) ;
75- return ;
67+
68+ var promise = self
69+ . processCss ( originalCss , assetName )
70+ . then (
71+ function ( result ) {
72+
73+ if ( hasErrors ) {
74+ self . print ( 'Skiping ' + assetName + ' because of an error.' ) ;
75+ return ;
76+ }
77+
78+ var processedCss = result . css ;
79+
80+ assets [ assetName ] = self . createCssAsset ( processedCss , asset ) ;
81+
82+ self . print ( 'Processed ' + assetName + ', before: ' + originalCss . length + ', after: ' + processedCss . length + ', ratio: ' + ( Math . round ( ( ( processedCss . length * 100 ) / originalCss . length ) * 100 ) / 100 ) + '%' ) ;
83+
7684 }
77-
78- var processedCss = result . css ;
79-
80- assets [ assetName ] = self . createCssAsset ( processedCss , asset ) ;
81-
82- self . print ( 'Processed ' + assetName + ', before: ' + originalCss . length + ', after: ' + processedCss . length + ', ratio: ' + ( Math . round ( ( ( processedCss . length * 100 ) / originalCss . length ) * 100 ) / 100 ) + '%' ) ;
83-
84- } , function ( err ) {
85- hasErrors = true ;
86- self . print ( 'Error processing file: ' + assetName ) ;
87- console . error ( err ) ;
88- }
89- ) ;
90-
85+ ) . catch ( function ( err ) {
86+ hasErrors = true ;
87+ self . print ( 'Error processing file: ' + assetName ) ;
88+ throw err ;
89+ }
90+ ) ;
91+
9192 promises . push ( promise ) ;
9293 }
9394 ) ;
94-
95- Promise . all ( promises ) . then ( function ( ) { compileCallback ( ) ; } , compileCallback ) ;
95+
96+ Promise . all ( promises )
97+ . then ( function ( ) { compileCallback ( ) ; } )
98+ . catch ( compileCallback ) ;
9699 } ) ;
97100} ;
98101
0 commit comments