@@ -3,96 +3,99 @@ var webpackSources = require('webpack-sources');
3
3
4
4
function OptimizeCssAssetsPlugin ( options ) {
5
5
this . options = options || { } ;
6
-
6
+
7
7
if ( this . options . assetNameRegExp === undefined ) {
8
8
this . options . assetNameRegExp = / \. c s s $ / g;
9
9
}
10
-
10
+
11
11
if ( this . options . cssProcessor === undefined ) {
12
12
this . options . cssProcessor = require ( 'cssnano' ) ;
13
13
}
14
-
14
+
15
15
if ( this . options . cssProcessorOptions === undefined ) {
16
16
this . options . cssProcessorOptions = { } ;
17
17
}
18
-
18
+
19
19
if ( this . options . canPrint === undefined ) {
20
20
this . options . canPrint = true ;
21
21
}
22
22
} ;
23
23
24
24
OptimizeCssAssetsPlugin . prototype . print = function ( ) {
25
25
if ( this . options . canPrint ) {
26
- console . log . apply ( console , arguments ) ;
26
+ console . log . apply ( console , arguments ) ;
27
27
}
28
28
} ;
29
29
30
30
OptimizeCssAssetsPlugin . 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 ) ) ;
32
32
} ;
33
33
34
34
OptimizeCssAssetsPlugin . prototype . createCssAsset = function ( css , originalAsset ) {
35
35
return new webpackSources . RawSource ( css ) ;
36
36
} ;
37
37
38
38
OptimizeCssAssetsPlugin . prototype . apply = function ( compiler ) {
39
-
39
+
40
40
var self = this ;
41
-
41
+
42
42
compiler . plugin ( 'emit' , function ( compilation , compileCallback ) {
43
-
43
+
44
44
self . print ( '\nStarting to optimize CSS...' ) ;
45
-
45
+
46
46
var assets = compilation . assets ;
47
-
47
+
48
48
var cssAssetNames = _ . filter (
49
- _ . keys ( assets ) ,
50
- function ( assetName ) {
49
+ _ . keys ( assets ) ,
50
+ function ( assetName ) {
51
51
return assetName . match ( self . options . assetNameRegExp ) ;
52
52
}
53
53
) ;
54
-
54
+
55
55
var hasErrors = false ;
56
56
var promises = [ ] ;
57
-
57
+
58
58
_ . each (
59
59
cssAssetNames ,
60
60
function ( assetName ) {
61
-
61
+
62
62
self . print ( 'Processing ' + assetName + '...' ) ;
63
-
63
+
64
64
var asset = assets [ assetName ] ;
65
-
65
+
66
66
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
+
76
84
}
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
+
91
92
promises . push ( promise ) ;
92
93
}
93
94
) ;
94
-
95
- Promise . all ( promises ) . then ( function ( ) { compileCallback ( ) ; } , compileCallback ) ;
95
+
96
+ Promise . all ( promises )
97
+ . then ( function ( ) { compileCallback ( ) ; } )
98
+ . catch ( compileCallback ) ;
96
99
} ) ;
97
100
} ;
98
101
0 commit comments