11import os from 'os' ;
22
3- import pLimit from 'p-limit' ;
3+ import { SourceMapConsumer } from 'source-map' ;
4+ import { SourceMapSource , RawSource } from 'webpack-sources' ;
45import RequestShortener from 'webpack/lib/RequestShortener' ;
5- import cssnanoPackageJson from 'cssnano/package.json' ;
66import {
77 util ,
88 ModuleFilenameHelpers ,
99 SourceMapDevToolPlugin ,
10- javascript ,
1110 version as webpackVersion ,
1211} from 'webpack' ;
13- import { SourceMapSource , RawSource } from 'webpack-sources' ;
14- import { SourceMapConsumer } from 'source-map' ;
1512import validateOptions from 'schema-utils' ;
1613import serialize from 'serialize-javascript' ;
14+ import cssnanoPackageJson from 'cssnano/package.json' ;
15+ import pLimit from 'p-limit' ;
1716import Worker from 'jest-worker' ;
1817
1918import schema from './options.json' ;
2019
2120import { minify as minifyFn } from './minify' ;
2221
23- const warningRegex = / \[ .+ : ( [ 0 - 9 ] + ) , ( [ 0 - 9 ] + ) \] / ;
22+ const warningRegex = / \s .+ : + ( [ 0 - 9 ] + ) : + ( [ 0 - 9 ] + ) / ;
2423
2524class CssnanoPlugin {
2625 constructor ( options = { } ) {
2726 validateOptions ( schema , options , {
28- name : 'Cssnano webpack plugin ' ,
27+ name : 'Cssnano Plugin ' ,
2928 baseDataPath : 'options' ,
3029 } ) ;
3130
3231 const {
33- test = / \. c s s ( \? .* ) ? $ / i,
34- sourceMap = false ,
32+ minify,
3533 cssnanoOptions = {
3634 preset : 'default' ,
3735 } ,
36+ test = / \. c s s ( \? .* ) ? $ / i,
3837 warningsFilter = ( ) => true ,
38+ sourceMap = false ,
3939 cache = true ,
4040 cacheKeys = ( defaultCacheKeys ) => defaultCacheKeys ,
4141 parallel = true ,
4242 include,
4343 exclude,
44- minify,
4544 } = options ;
4645
4746 this . options = {
4847 test,
49- sourceMap,
50- cssnanoOptions,
5148 warningsFilter,
49+ sourceMap,
5250 cache,
5351 cacheKeys,
5452 parallel,
5553 include,
5654 exclude,
5755 minify,
56+ cssnanoOptions,
5857 } ;
5958
6059 if ( this . options . sourceMap === true ) {
@@ -74,14 +73,6 @@ class CssnanoPlugin {
7473 ) ;
7574 }
7675
77- static buildSourceMap ( inputSourceMap ) {
78- if ( ! inputSourceMap || ! CssnanoPlugin . isSourceMap ( inputSourceMap ) ) {
79- return null ;
80- }
81-
82- return new SourceMapConsumer ( inputSourceMap ) ;
83- }
84-
8576 static buildError ( error , file , sourceMap , requestShortener ) {
8677 if ( error . line ) {
8778 const original =
@@ -106,19 +97,19 @@ class CssnanoPlugin {
10697 }
10798
10899 return new Error (
109- `${ file } from Cssnano Webpack Plugin \n${ error . message } [${ file } :${
110- error . line
111- } , ${ error . column } ]${
100+ `${ file } from Cssnano \n${ error . message } [${ file } :${ error . line } , ${
101+ error . column
102+ } ]${
112103 error . stack ? `\n${ error . stack . split ( '\n' ) . slice ( 1 ) . join ( '\n' ) } ` : ''
113104 } `
114105 ) ;
115106 }
116107
117108 if ( error . stack ) {
118- return new Error ( `${ file } from Cssnano Webpack Plugin \n${ error . stack } ` ) ;
109+ return new Error ( `${ file } from Cssnano\n${ error . stack } ` ) ;
119110 }
120111
121- return new Error ( `${ file } from Cssnano Webpack Plugin \n${ error . message } ` ) ;
112+ return new Error ( `${ file } from Cssnano\n${ error . message } ` ) ;
122113 }
123114
124115 static buildWarning (
@@ -150,11 +141,11 @@ class CssnanoPlugin {
150141 requestShortener
151142 ) {
152143 ( { source } = original ) ;
153- warningMessage = `${ warningMessage . replace ( warningRegex , '' ) } ` ;
154144
155- locationMessage = `[${ requestShortener . shorten ( original . source ) } :${
145+ warningMessage = `${ warningMessage . replace ( warningRegex , '' ) } ` ;
146+ locationMessage = `${ requestShortener . shorten ( original . source ) } :${
156147 original . line
157- } , ${ original . column } ] `;
148+ } : ${ original . column } `;
158149 }
159150 }
160151 }
@@ -163,7 +154,7 @@ class CssnanoPlugin {
163154 return null ;
164155 }
165156
166- return `Cssnano Webpack Plugin: ${ warningMessage } ${ locationMessage } ` ;
157+ return `Cssnano Plugin: ${ warningMessage } ${ locationMessage } ` ;
167158 }
168159
169160 static isWebpack4 ( ) {
@@ -217,8 +208,12 @@ class CssnanoPlugin {
217208
218209 let sourceMap = null ;
219210
220- if ( error || ( warnings && warnings . length > 0 ) ) {
221- sourceMap = CssnanoPlugin . buildSourceMap ( inputSourceMap ) ;
211+ if (
212+ ( error || ( warnings && warnings . length > 0 ) ) &&
213+ inputSourceMap &&
214+ CssnanoPlugin . isSourceMap ( inputSourceMap )
215+ ) {
216+ sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
222217 }
223218
224219 // Handling results
@@ -273,12 +268,10 @@ class CssnanoPlugin {
273268 }
274269 } ;
275270
276- const postcssOptions = { to : file , from : file , map : false } ;
277-
278271 const task = {
272+ file,
279273 input,
280274 inputSourceMap,
281- postcssOptions,
282275 map : this . options . sourceMap ,
283276 cssnanoOptions : this . options . cssnanoOptions ,
284277 minify : this . options . minify ,
@@ -380,10 +373,7 @@ class CssnanoPlugin {
380373 }
381374
382375 if ( cache . isEnabled ( ) && ! taskResult . error ) {
383- taskResult = await cache . store ( task , taskResult ) . then (
384- ( ) => taskResult ,
385- ( ) => taskResult
386- ) ;
376+ await cache . store ( task , taskResult ) ;
387377 }
388378
389379 task . callback ( taskResult ) ;
@@ -392,7 +382,7 @@ class CssnanoPlugin {
392382 } ;
393383
394384 scheduledTasks . push (
395- limit ( ( ) => {
385+ limit ( async ( ) => {
396386 const task = getTaskForAsset ( assetName ) . next ( ) . value ;
397387
398388 if ( ! task ) {
@@ -401,24 +391,29 @@ class CssnanoPlugin {
401391 }
402392
403393 if ( cache . isEnabled ( ) ) {
404- return cache . get ( task ) . then (
405- ( taskResult ) => task . callback ( taskResult ) ,
406- ( ) => enqueue ( task )
407- ) ;
394+ let taskResult ;
395+
396+ try {
397+ taskResult = await cache . get ( task ) ;
398+ } catch ( ignoreError ) {
399+ return enqueue ( task ) ;
400+ }
401+
402+ task . callback ( taskResult ) ;
403+
404+ return Promise . resolve ( ) ;
408405 }
409406
410407 return enqueue ( task ) ;
411408 } )
412409 ) ;
413410 }
414411
415- return Promise . all ( scheduledTasks ) . then ( ( ) => {
416- if ( worker ) {
417- return worker . end ( ) ;
418- }
412+ await Promise . all ( scheduledTasks ) ;
419413
420- return Promise . resolve ( ) ;
421- } ) ;
414+ if ( worker ) {
415+ await worker . end ( ) ;
416+ }
422417 }
423418
424419 apply ( compiler ) {
@@ -486,38 +481,11 @@ class CssnanoPlugin {
486481 }
487482
488483 if ( CssnanoPlugin . isWebpack4 ( ) ) {
489- const { mainTemplate, chunkTemplate } = compilation ;
490- const data = serialize ( {
491- cssnano : cssnanoPackageJson . version ,
492- cssnanoOptions : this . options . cssnanoOptions ,
493- } ) ;
494-
495- // Regenerate `contenthash` for minified assets
496- for ( const template of [ mainTemplate , chunkTemplate ] ) {
497- template . hooks . hashForChunk . tap ( plugin , ( hash ) => {
498- hash . update ( 'CssnanoPlugin' ) ;
499- hash . update ( data ) ;
500- } ) ;
501- }
502-
503484 compilation . hooks . optimizeChunkAssets . tapPromise (
504485 plugin ,
505486 optimizeFn . bind ( this , compilation )
506487 ) ;
507488 } else {
508- const hooks = javascript . JavascriptModulesPlugin . getCompilationHooks (
509- compilation
510- ) ;
511- const data = serialize ( {
512- cssnano : cssnanoPackageJson . version ,
513- cssnanoOptions : this . options . cssnanoOptions ,
514- } ) ;
515-
516- hooks . chunkHash . tap ( plugin , ( chunk , hash ) => {
517- hash . update ( 'CssnanoPlugin' ) ;
518- hash . update ( data ) ;
519- } ) ;
520-
521489 compilation . hooks . optimizeAssets . tapPromise (
522490 plugin ,
523491 optimizeFn . bind ( this , compilation )
0 commit comments