5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import * as CleanCSS from 'clean-css' ;
8
+ import * as cssNano from 'cssnano' ;
9
+ import { ProcessOptions , Result } from 'postcss' ;
9
10
import { Compiler , compilation } from 'webpack' ;
10
11
import { RawSource , Source , SourceMapSource } from 'webpack-sources' ;
11
12
12
- export interface CleanCssWebpackPluginOptions {
13
+ export interface OptimizeCssWebpackPluginOptions {
13
14
sourceMap : boolean ;
14
15
test : ( file : string ) => boolean ;
15
16
}
@@ -22,19 +23,19 @@ function hook(
22
23
) => Promise < void | void [ ] > ,
23
24
) {
24
25
compiler . hooks . compilation . tap (
25
- 'cleancss -webpack-plugin' ,
26
+ 'optimize-css -webpack-plugin' ,
26
27
( compilation : compilation . Compilation ) => {
27
- compilation . hooks . optimizeChunkAssets . tapPromise ( 'cleancss -webpack-plugin' , chunks =>
28
+ compilation . hooks . optimizeChunkAssets . tapPromise ( 'optimize-css -webpack-plugin' , chunks =>
28
29
action ( compilation , chunks ) ,
29
30
) ;
30
31
} ,
31
32
) ;
32
33
}
33
34
34
- export class CleanCssWebpackPlugin {
35
- private readonly _options : CleanCssWebpackPluginOptions ;
35
+ export class OptimizeCssWebpackPlugin {
36
+ private readonly _options : OptimizeCssWebpackPluginOptions ;
36
37
37
- constructor ( options : Partial < CleanCssWebpackPluginOptions > ) {
38
+ constructor ( options : Partial < OptimizeCssWebpackPluginOptions > ) {
38
39
this . _options = {
39
40
sourceMap : false ,
40
41
test : file => file . endsWith ( '.css' ) ,
@@ -44,21 +45,6 @@ export class CleanCssWebpackPlugin {
44
45
45
46
apply ( compiler : Compiler ) : void {
46
47
hook ( compiler , ( compilation : compilation . Compilation , chunks : compilation . Chunk [ ] ) => {
47
- const cleancss = new CleanCSS ( {
48
- compatibility : 'ie9' ,
49
- level : {
50
- 2 : {
51
- skipProperties : [
52
- 'transition' , // Fixes #12408
53
- 'font' , // Fixes #9648
54
- ] ,
55
- } ,
56
- } ,
57
- inline : false ,
58
- returnPromise : true ,
59
- sourceMap : this . _options . sourceMap ,
60
- } ) ;
61
-
62
48
const files : string [ ] = [ ...compilation . additionalChunkAssets ] ;
63
49
64
50
chunks . forEach ( chunk => {
@@ -90,37 +76,40 @@ export class CleanCssWebpackPlugin {
90
76
return ;
91
77
}
92
78
93
- const output = await cleancss . minify ( content , map ) ;
94
-
95
- let hasWarnings = false ;
96
- if ( output . warnings && output . warnings . length > 0 ) {
97
- compilation . warnings . push ( ...output . warnings ) ;
98
- hasWarnings = true ;
99
- }
100
-
101
- if ( output . errors && output . errors . length > 0 ) {
102
- output . errors . forEach ( ( error : string ) => compilation . errors . push ( new Error ( error ) ) ) ;
103
-
104
- return ;
105
- }
106
-
107
- // generally means invalid syntax so bail
108
- if ( hasWarnings && output . stats . minifiedSize === 0 ) {
109
- return ;
79
+ const cssNanoOptions : cssNano . CssNanoOptions = {
80
+ preset : 'default' ,
81
+ } ;
82
+
83
+ const postCssOptions : ProcessOptions = {
84
+ from : file ,
85
+ map : map && { annotation : false , prev : map } ,
86
+ } ;
87
+
88
+ const output = await new Promise < Result > ( ( resolve , reject ) => {
89
+ // the last parameter is not in the typings
90
+ // tslint:disable-next-line: no-any
91
+ ( cssNano . process as any ) ( content , postCssOptions , cssNanoOptions )
92
+ . then ( resolve )
93
+ . catch ( reject ) ;
94
+ } ) ;
95
+
96
+ const warnings = output . warnings ( ) ;
97
+ if ( warnings . length ) {
98
+ compilation . warnings . push ( ...warnings . map ( ( { text } ) => text ) ) ;
110
99
}
111
100
112
101
let newSource ;
113
- if ( output . sourceMap ) {
102
+ if ( output . map ) {
114
103
newSource = new SourceMapSource (
115
- output . styles ,
104
+ output . css ,
116
105
file ,
117
106
// tslint:disable-next-line: no-any
118
- output . sourceMap . toString ( ) as any ,
107
+ output . map . toString ( ) as any ,
119
108
content ,
120
109
map ,
121
110
) ;
122
111
} else {
123
- newSource = new RawSource ( output . styles ) ;
112
+ newSource = new RawSource ( output . css ) ;
124
113
}
125
114
126
115
compilation . assets [ file ] = newSource ;
0 commit comments