55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
8- import * as CleanCSS from 'clean-css' ;
8+ import * as cssNano from 'cssnano' ;
9+ import { ProcessOptions , Result } from 'postcss' ;
910import { Compiler , compilation } from 'webpack' ;
1011import { RawSource , Source , SourceMapSource } from 'webpack-sources' ;
1112
12- export interface CleanCssWebpackPluginOptions {
13+ export interface OptimizeCssWebpackPluginOptions {
1314 sourceMap : boolean ;
1415 test : ( file : string ) => boolean ;
1516}
@@ -22,19 +23,19 @@ function hook(
2223 ) => Promise < void | void [ ] > ,
2324) {
2425 compiler . hooks . compilation . tap (
25- 'cleancss -webpack-plugin' ,
26+ 'optimize-css -webpack-plugin' ,
2627 ( compilation : compilation . Compilation ) => {
27- compilation . hooks . optimizeChunkAssets . tapPromise ( 'cleancss -webpack-plugin' , chunks =>
28+ compilation . hooks . optimizeChunkAssets . tapPromise ( 'optimize-css -webpack-plugin' , chunks =>
2829 action ( compilation , chunks ) ,
2930 ) ;
3031 } ,
3132 ) ;
3233}
3334
34- export class CleanCssWebpackPlugin {
35- private readonly _options : CleanCssWebpackPluginOptions ;
35+ export class OptimizeCssWebpackPlugin {
36+ private readonly _options : OptimizeCssWebpackPluginOptions ;
3637
37- constructor ( options : Partial < CleanCssWebpackPluginOptions > ) {
38+ constructor ( options : Partial < OptimizeCssWebpackPluginOptions > ) {
3839 this . _options = {
3940 sourceMap : false ,
4041 test : file => file . endsWith ( '.css' ) ,
@@ -44,21 +45,6 @@ export class CleanCssWebpackPlugin {
4445
4546 apply ( compiler : Compiler ) : void {
4647 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-
6248 const files : string [ ] = [ ...compilation . additionalChunkAssets ] ;
6349
6450 chunks . forEach ( chunk => {
@@ -90,37 +76,40 @@ export class CleanCssWebpackPlugin {
9076 return ;
9177 }
9278
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 ) ) ;
11099 }
111100
112101 let newSource ;
113- if ( output . sourceMap ) {
102+ if ( output . map ) {
114103 newSource = new SourceMapSource (
115- output . styles ,
104+ output . css ,
116105 file ,
117106 // tslint:disable-next-line: no-any
118- output . sourceMap . toString ( ) as any ,
107+ output . map . toString ( ) as any ,
119108 content ,
120109 map ,
121110 ) ;
122111 } else {
123- newSource = new RawSource ( output . styles ) ;
112+ newSource = new RawSource ( output . css ) ;
124113 }
125114
126115 compilation . assets [ file ] = newSource ;
0 commit comments