@@ -162,6 +162,33 @@ class CssMinimizerPlugin {
162
162
: Math . min ( Number ( parallel ) || 0 , cpus . length - 1 ) ;
163
163
}
164
164
165
+ static updateMinimizerOptions ( {
166
+ minimizerOptions,
167
+ compilation,
168
+ inputSource,
169
+ } ) {
170
+ const { source : input , map } = inputSource . sourceAndMap ( ) ;
171
+
172
+ if ( map ) {
173
+ if ( CssMinimizerPlugin . isSourceMap ( map ) ) {
174
+ // eslint-disable-next-line no-param-reassign
175
+ minimizerOptions . inputSourceMap = map ;
176
+ } else {
177
+ // eslint-disable-next-line no-param-reassign
178
+ minimizerOptions . inputSourceMap = map ;
179
+
180
+ compilation . warnings . push (
181
+ new Error ( `${ minimizerOptions . name } contains invalid source map` )
182
+ ) ;
183
+ }
184
+ }
185
+
186
+ // eslint-disable-next-line no-param-reassign
187
+ minimizerOptions . input = Buffer . isBuffer ( input ) ? input . toString ( ) : input ;
188
+
189
+ return minimizerOptions ;
190
+ }
191
+
165
192
async optimize ( compiler , compilation , assets , optimizeOptions ) {
166
193
const cache = compilation . getCache ( 'CssMinimizerWebpackPlugin' ) ;
167
194
let numberOfAssetsForMinify = 0 ;
@@ -192,15 +219,40 @@ class CssMinimizerPlugin {
192
219
. map ( async ( name ) => {
193
220
const { info, source } = compilation . getAsset ( name ) ;
194
221
222
+ const minifyFns =
223
+ typeof this . options . minify === 'function'
224
+ ? [ this . options . minify ]
225
+ : this . options . minify ;
226
+
195
227
const eTag = cache . getLazyHashedEtag ( source ) ;
196
- const cacheItem = cache . getItemCache ( name , eTag ) ;
197
- const output = await cacheItem . getPromise ( ) ;
228
+ const chainFns = [ ] ;
229
+ const cacheItems = minifyFns . map ( ( fn ) => {
230
+ chainFns . push ( fn ) ;
198
231
199
- if ( ! output ) {
232
+ const cacheKey = serialize ( {
233
+ name,
234
+ minify : chainFns ,
235
+ } ) ;
236
+
237
+ return cache . getItemCache ( cacheKey , eTag ) ;
238
+ } ) ;
239
+
240
+ const output = await Promise . all (
241
+ cacheItems . map ( ( cacheItem ) => cacheItem . getPromise ( ) )
242
+ ) ;
243
+
244
+ if ( output . filter ( ( i ) => typeof i === 'undefined' ) . length > 0 ) {
200
245
numberOfAssetsForMinify += 1 ;
201
246
}
202
247
203
- return { name, info, inputSource : source , output, cacheItem } ;
248
+ return {
249
+ name,
250
+ info,
251
+ inputSource : source ,
252
+ output,
253
+ cacheItems,
254
+ minifyFns,
255
+ } ;
204
256
} )
205
257
) ;
206
258
@@ -251,62 +303,33 @@ class CssMinimizerPlugin {
251
303
for ( const asset of assetsForMinify ) {
252
304
scheduledTasks . push (
253
305
limit ( async ( ) => {
254
- const { name, inputSource, cacheItem } = asset ;
306
+ const { name, inputSource, cacheItems , minifyFns } = asset ;
255
307
let { output } = asset ;
308
+ const minimizerOptions = { name } ;
256
309
257
- if ( ! output ) {
258
- let input ;
259
- let inputSourceMap ;
260
-
261
- const {
262
- source : sourceFromInputSource ,
263
- map,
264
- } = inputSource . sourceAndMap ( ) ;
265
-
266
- input = sourceFromInputSource ;
267
-
268
- if ( map ) {
269
- if ( CssMinimizerPlugin . isSourceMap ( map ) ) {
270
- inputSourceMap = map ;
271
- } else {
272
- inputSourceMap = map ;
273
-
274
- compilation . warnings . push (
275
- new Error ( `${ name } contains invalid source map` )
276
- ) ;
277
- }
278
- }
279
-
280
- if ( Buffer . isBuffer ( input ) ) {
281
- input = input . toString ( ) ;
282
- }
310
+ CssMinimizerPlugin . updateMinimizerOptions ( {
311
+ minimizerOptions,
312
+ compilation,
313
+ inputSource,
314
+ } ) ;
283
315
284
- const minifyFns =
285
- typeof this . options . minify === 'function'
286
- ? [ this . options . minify ]
287
- : this . options . minify ;
288
- const minimizerOptions = {
289
- name,
290
- input,
291
- inputSourceMap,
292
- } ;
293
-
294
- let warnings = [ ] ;
316
+ let warnings = [ ] ;
295
317
296
- this . options . minimizerOptions = Array . isArray (
297
- this . options . minimizerOptions
298
- )
299
- ? this . options . minimizerOptions
300
- : [ this . options . minimizerOptions ] ;
318
+ this . options . minimizerOptions = Array . isArray (
319
+ this . options . minimizerOptions
320
+ )
321
+ ? this . options . minimizerOptions
322
+ : [ this . options . minimizerOptions ] ;
301
323
302
- for await ( const [ i , minifyFunc ] of minifyFns . entries ( ) ) {
324
+ for await ( const [ i , minifyFunc ] of minifyFns . entries ( ) ) {
325
+ if ( ! output [ i ] ) {
303
326
minimizerOptions . minify = minifyFunc ;
304
327
minimizerOptions . minimizerOptions = this . options . minimizerOptions [
305
328
i
306
329
] ;
307
330
308
331
try {
309
- output = await ( getWorker
332
+ output [ i ] = await ( getWorker
310
333
? getWorker ( ) . transform ( serialize ( minimizerOptions ) )
311
334
: minifyFn ( minimizerOptions ) ) ;
312
335
} catch ( error ) {
@@ -315,59 +338,68 @@ class CssMinimizerPlugin {
315
338
error ,
316
339
name ,
317
340
compilation . requestShortener ,
318
- inputSourceMap &&
319
- CssMinimizerPlugin . isSourceMap ( inputSourceMap )
320
- ? new SourceMapConsumer ( inputSourceMap )
341
+ minimizerOptions . inputSourceMap &&
342
+ CssMinimizerPlugin . isSourceMap (
343
+ minimizerOptions . inputSourceMap
344
+ )
345
+ ? new SourceMapConsumer ( minimizerOptions . inputSourceMap )
321
346
: null
322
347
)
323
348
) ;
324
349
325
350
return ;
326
351
}
327
352
328
- minimizerOptions . input = output . code ;
329
- minimizerOptions . inputSourceMap = output . map ;
330
- warnings = warnings . concat ( output . warnings ) ;
331
- }
353
+ if ( output [ i ] . warnings && output [ i ] . warnings . length > 0 ) {
354
+ output [ i ] . warnings = output [ i ] . warnings
355
+ . map ( ( warning ) =>
356
+ CssMinimizerPlugin . buildWarning (
357
+ warning ,
358
+ name ,
359
+ minimizerOptions . inputSourceMap &&
360
+ CssMinimizerPlugin . isSourceMap (
361
+ minimizerOptions . inputSourceMap
362
+ )
363
+ ? new SourceMapConsumer ( minimizerOptions . inputSourceMap )
364
+ : null ,
365
+ compilation . requestShortener ,
366
+ this . options . warningsFilter
367
+ )
368
+ )
369
+ . filter ( Boolean ) ;
370
+ }
332
371
333
- output . warnings = warnings ;
334
-
335
- if ( output . map ) {
336
- output . source = new SourceMapSource (
337
- output . code ,
338
- name ,
339
- output . map ,
340
- input ,
341
- inputSourceMap ,
342
- true
343
- ) ;
344
- } else {
345
- output . source = new RawSource ( output . code ) ;
346
- }
372
+ if ( output [ i ] . map ) {
373
+ output [ i ] . source = new SourceMapSource (
374
+ output [ i ] . code ,
375
+ name ,
376
+ output [ i ] . map ,
377
+ minimizerOptions . input ,
378
+ minimizerOptions . inputSourceMap ,
379
+ true
380
+ ) ;
381
+ } else {
382
+ output [ i ] . source = new RawSource ( output [ i ] . code ) ;
383
+ }
347
384
348
- if ( output . warnings && output . warnings . length > 0 ) {
349
- output . warnings = output . warnings
350
- . map ( ( warning ) =>
351
- CssMinimizerPlugin . buildWarning (
352
- warning ,
353
- name ,
354
- inputSourceMap &&
355
- CssMinimizerPlugin . isSourceMap ( inputSourceMap )
356
- ? new SourceMapConsumer ( inputSourceMap )
357
- : null ,
358
- compilation . requestShortener ,
359
- this . options . warningsFilter
360
- )
361
- )
362
- . filter ( Boolean ) ;
385
+ await cacheItems [ i ] . storePromise ( {
386
+ source : output [ i ] . source ,
387
+ warnings : output [ i ] . warnings ,
388
+ } ) ;
363
389
}
364
390
365
- await cacheItem . storePromise ( {
366
- source : output . source ,
367
- warnings : output . warnings ,
391
+ CssMinimizerPlugin . updateMinimizerOptions ( {
392
+ minimizerOptions,
393
+ compilation,
394
+ inputSource : output [ i ] . source ,
368
395
} ) ;
396
+
397
+ warnings = warnings . concat ( output [ i ] . warnings ) ;
369
398
}
370
399
400
+ output = output [ output . length - 1 ] ;
401
+ output . warnings = warnings ;
402
+
371
403
if ( output . warnings && output . warnings . length > 0 ) {
372
404
output . warnings . forEach ( ( warning ) => {
373
405
const Warning = class Warning extends Error {
0 commit comments