@@ -234,9 +234,9 @@ class CssMinimizerPlugin {
234
234
}
235
235
236
236
const task = {
237
+ assetName,
237
238
assetSource,
238
239
assetInfo,
239
- assetName,
240
240
input,
241
241
inputSourceMap,
242
242
map : this . options . sourceMap ,
@@ -263,91 +263,15 @@ class CssMinimizerPlugin {
263
263
yield task ;
264
264
}
265
265
266
- afterTask ( compiler , compilation , task , weakCache ) {
267
- const {
268
- error,
269
- inputSourceMap,
270
- assetName,
271
- input,
272
- assetInfo,
273
- assetSource,
274
- output,
275
- } = task ;
276
-
277
- let sourceMap = null ;
278
-
279
- if (
280
- ( error || ( output && output . warnings && output . warnings . length > 0 ) ) &&
281
- inputSourceMap &&
282
- CssMinimizerPlugin . isSourceMap ( inputSourceMap )
283
- ) {
284
- sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
285
- }
286
-
287
- // Handling results
288
- // Error case: add errors, and go to next file
289
- if ( error ) {
290
- compilation . errors . push (
291
- CssMinimizerPlugin . buildError (
292
- error ,
293
- assetName ,
294
- sourceMap ,
295
- new RequestShortener ( compiler . context )
296
- )
297
- ) ;
298
-
299
- return ;
300
- }
301
-
302
- const { css : code , map, warnings } = output ;
303
-
304
- let weakOutput = weakCache . get ( assetSource ) ;
305
-
306
- if ( ! weakOutput ) {
307
- if ( map ) {
308
- weakOutput = new SourceMapSource (
309
- code ,
310
- assetName ,
311
- map ,
312
- input ,
313
- inputSourceMap ,
314
- true
315
- ) ;
316
- } else {
317
- weakOutput = new RawSource ( code ) ;
318
- }
319
-
320
- weakCache . set ( assetSource , weakOutput ) ;
321
- }
322
-
323
- CssMinimizerPlugin . updateAsset ( compilation , assetName , weakOutput , {
324
- ...assetInfo ,
325
- minimized : true ,
326
- } ) ;
327
-
328
- // Handling warnings
329
- if ( warnings && warnings . length > 0 ) {
330
- warnings . forEach ( ( warning ) => {
331
- const builtWarning = CssMinimizerPlugin . buildWarning (
332
- warning ,
333
- assetName ,
334
- sourceMap ,
335
- new RequestShortener ( compiler . context ) ,
336
- this . options . warningsFilter
337
- ) ;
338
-
339
- if ( builtWarning ) {
340
- compilation . warnings . push ( builtWarning ) ;
341
- }
342
- } ) ;
343
- }
344
- }
345
-
346
266
// eslint-disable-next-line class-methods-use-this
347
267
async runTasks ( compiler , compilation , assetNames , CacheEngine , weakCache ) {
348
- const cache = new CacheEngine ( compilation , {
349
- cache : this . options . cache ,
350
- } ) ;
268
+ const cache = new CacheEngine (
269
+ compilation ,
270
+ {
271
+ cache : this . options . cache ,
272
+ } ,
273
+ weakCache
274
+ ) ;
351
275
const availableNumberOfCores = CssMinimizerPlugin . getAvailableNumberOfCores (
352
276
this . options . parallel
353
277
) ;
@@ -385,24 +309,6 @@ class CssMinimizerPlugin {
385
309
const scheduledTasks = [ ] ;
386
310
387
311
for ( const assetName of assetNames ) {
388
- const enqueue = async ( task ) => {
389
- try {
390
- // eslint-disable-next-line no-param-reassign
391
- task . output = worker
392
- ? await worker . transform ( serialize ( task ) )
393
- : await minifyFn ( task ) ;
394
- } catch ( error ) {
395
- // eslint-disable-next-line no-param-reassign
396
- task . error = error ;
397
- }
398
-
399
- if ( cache . isEnabled ( ) && typeof task . output !== 'undefined' ) {
400
- await cache . store ( task ) ;
401
- }
402
-
403
- this . afterTask ( compiler , compilation , task , weakCache ) ;
404
- } ;
405
-
406
312
scheduledTasks . push (
407
313
limit ( async ( ) => {
408
314
const task = this . getTask ( compiler , compilation , assetName ) . next ( )
@@ -412,23 +318,83 @@ class CssMinimizerPlugin {
412
318
return Promise . resolve ( ) ;
413
319
}
414
320
415
- if ( cache . isEnabled ( ) ) {
321
+ let resultOutput = await cache . get ( task , {
322
+ RawSource,
323
+ SourceMapSource,
324
+ } ) ;
325
+
326
+ if ( ! resultOutput ) {
416
327
try {
417
- task . output = await cache . get ( task ) ;
418
- } catch ( ignoreError ) {
419
- return enqueue ( task ) ;
328
+ // eslint-disable-next-line no-param-reassign
329
+ resultOutput = await ( worker
330
+ ? worker . transform ( serialize ( task ) )
331
+ : minifyFn ( task ) ) ;
332
+ } catch ( error ) {
333
+ compilation . errors . push (
334
+ CssMinimizerPlugin . buildError (
335
+ error ,
336
+ assetName ,
337
+ task . inputSourceMap &&
338
+ CssMinimizerPlugin . isSourceMap ( task . inputSourceMap )
339
+ ? new SourceMapConsumer ( task . inputSourceMap )
340
+ : null ,
341
+ new RequestShortener ( compiler . context )
342
+ )
343
+ ) ;
344
+
345
+ return Promise . resolve ( ) ;
420
346
}
421
347
422
- if ( ! task . output ) {
423
- return enqueue ( task ) ;
348
+ task . css = resultOutput . css ;
349
+ task . map = resultOutput . map ;
350
+ task . warnings = resultOutput . warnings ;
351
+
352
+ if ( task . map ) {
353
+ task . source = new SourceMapSource (
354
+ task . css ,
355
+ assetName ,
356
+ task . map ,
357
+ task . input ,
358
+ task . inputSourceMap ,
359
+ true
360
+ ) ;
361
+ } else {
362
+ task . source = new RawSource ( task . css ) ;
424
363
}
425
364
426
- this . afterTask ( compiler , compilation , task , weakCache ) ;
365
+ await cache . store ( task ) ;
366
+ } else {
367
+ task . source = resultOutput . source ;
368
+ task . warnings = resultOutput . warnings ;
369
+ }
427
370
428
- return Promise . resolve ( ) ;
371
+ if ( task . warnings && task . warnings . length > 0 ) {
372
+ task . warnings . forEach ( ( warning ) => {
373
+ const builtWarning = CssMinimizerPlugin . buildWarning (
374
+ warning ,
375
+ assetName ,
376
+ task . inputSourceMap &&
377
+ CssMinimizerPlugin . isSourceMap ( task . inputSourceMap )
378
+ ? new SourceMapConsumer ( task . inputSourceMap )
379
+ : null ,
380
+ new RequestShortener ( compiler . context ) ,
381
+ this . options . warningsFilter
382
+ ) ;
383
+
384
+ if ( builtWarning ) {
385
+ compilation . warnings . push ( builtWarning ) ;
386
+ }
387
+ } ) ;
429
388
}
430
389
431
- return enqueue ( task ) ;
390
+ const { source, assetInfo } = task ;
391
+
392
+ CssMinimizerPlugin . updateAsset ( compilation , assetName , source , {
393
+ ...assetInfo ,
394
+ minimized : true ,
395
+ } ) ;
396
+
397
+ return Promise . resolve ( ) ;
432
398
} )
433
399
) ;
434
400
}
@@ -507,7 +473,8 @@ class CssMinimizerPlugin {
507
473
const CacheEngine = require ( './Webpack4Cache' ) . default ;
508
474
509
475
compilation . hooks . optimizeChunkAssets . tapPromise ( pluginName , ( ) =>
510
- optimizeFn ( compilation , CacheEngine )
476
+ // eslint-disable-next-line no-undefined
477
+ optimizeFn ( compilation , CacheEngine , undefined , weakCache )
511
478
) ;
512
479
} else {
513
480
if ( this . options . sourceMap ) {
0 commit comments