9
9
* found in the LICENSE file at https://angular.io/license
10
10
*/
11
11
12
+ import * as CleanCSS from 'clean-css' ;
12
13
import { Compiler , compilation } from 'webpack' ;
13
14
import { RawSource , Source , SourceMapSource } from 'webpack-sources' ;
14
15
15
- const CleanCSS = require ( 'clean-css' ) ;
16
-
17
16
interface Chunk {
18
17
files : string [ ] ;
19
18
}
@@ -73,10 +72,10 @@ export class CleanCssWebpackPlugin {
73
72
74
73
const actions = files
75
74
. filter ( file => this . _options . test ( file ) )
76
- . map ( file => {
75
+ . map ( async file => {
77
76
const asset = compilation . assets [ file ] as Source ;
78
77
if ( ! asset ) {
79
- return Promise . resolve ( ) ;
78
+ return ;
80
79
}
81
80
82
81
let content : string ;
@@ -90,44 +89,42 @@ export class CleanCssWebpackPlugin {
90
89
}
91
90
92
91
if ( content . length === 0 ) {
93
- return Promise . resolve ( ) ;
92
+ return ;
93
+ }
94
+
95
+ const output = await cleancss . minify ( content , map ) ;
96
+
97
+ let hasWarnings = false ;
98
+ if ( output . warnings && output . warnings . length > 0 ) {
99
+ compilation . warnings . push ( ...output . warnings ) ;
100
+ hasWarnings = true ;
101
+ }
102
+
103
+ if ( output . errors && output . errors . length > 0 ) {
104
+ output . errors
105
+ . forEach ( ( error : string ) => compilation . errors . push ( new Error ( error ) ) ) ;
106
+ return ;
107
+ }
108
+
109
+ // generally means invalid syntax so bail
110
+ if ( hasWarnings && output . stats . minifiedSize === 0 ) {
111
+ return ;
112
+ }
113
+
114
+ let newSource ;
115
+ if ( output . sourceMap ) {
116
+ newSource = new SourceMapSource (
117
+ output . styles ,
118
+ file ,
119
+ output . sourceMap . toString ( ) as any ,
120
+ content ,
121
+ map ,
122
+ ) ;
123
+ } else {
124
+ newSource = new RawSource ( output . styles ) ;
94
125
}
95
126
96
- return Promise . resolve ( )
97
- . then ( ( ) => map ? cleancss . minify ( content , map ) : cleancss . minify ( content ) )
98
- . then ( ( output : any ) => {
99
- let hasWarnings = false ;
100
- if ( output . warnings && output . warnings . length > 0 ) {
101
- compilation . warnings . push ( ...output . warnings ) ;
102
- hasWarnings = true ;
103
- }
104
-
105
- if ( output . errors && output . errors . length > 0 ) {
106
- output . errors
107
- . forEach ( ( error : string ) => compilation . errors . push ( new Error ( error ) ) ) ;
108
- return ;
109
- }
110
-
111
- // generally means invalid syntax so bail
112
- if ( hasWarnings && output . stats . minifiedSize === 0 ) {
113
- return ;
114
- }
115
-
116
- let newSource ;
117
- if ( output . sourceMap ) {
118
- newSource = new SourceMapSource (
119
- output . styles ,
120
- file ,
121
- output . sourceMap . toString ( ) ,
122
- content ,
123
- map ,
124
- ) ;
125
- } else {
126
- newSource = new RawSource ( output . styles ) ;
127
- }
128
-
129
- compilation . assets [ file ] = newSource ;
130
- } ) ;
127
+ compilation . assets [ file ] = newSource ;
131
128
} ) ;
132
129
133
130
return Promise . all ( actions ) ;
0 commit comments