@@ -137,7 +137,7 @@ class CompressionPlugin {
137
137
delete compilation . assets [ name ] ;
138
138
}
139
139
140
- compress ( input ) {
140
+ runCompressionAlgorithm ( input ) {
141
141
return new Promise ( ( resolve , reject ) => {
142
142
const { algorithm, compressionOptions } = this ;
143
143
@@ -156,128 +156,121 @@ class CompressionPlugin {
156
156
} ) ;
157
157
}
158
158
159
- * getTask ( compilation , assetName ) {
160
- const { source : assetSource , info : assetInfo } = CompressionPlugin . getAsset (
161
- compilation ,
162
- assetName
159
+ async compress ( compilation , assets , CacheEngine , weakCache ) {
160
+ const assetNames = Object . keys (
161
+ typeof assets === 'undefined' ? compilation . assets : assets
162
+ ) . filter ( ( assetName ) =>
163
+ // eslint-disable-next-line no-undefined
164
+ ModuleFilenameHelpers . matchObject . bind ( undefined , this . options ) ( assetName )
163
165
) ;
164
166
165
- if ( assetInfo . compressed ) {
166
- yield false ;
167
- }
168
-
169
- let relatedName ;
170
-
171
- if ( typeof this . options . algorithm === 'function' ) {
172
- let filenameForRelatedName = this . options . filename ;
173
-
174
- const index = filenameForRelatedName . lastIndexOf ( '?' ) ;
175
-
176
- if ( index >= 0 ) {
177
- filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
178
- }
179
-
180
- relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
181
- } else {
182
- relatedName = `${ this . options . algorithm } ed` ;
183
- }
184
-
185
- if ( assetInfo . related && assetInfo . related [ relatedName ] ) {
186
- yield false ;
167
+ if ( assetNames . length === 0 ) {
168
+ return Promise . resolve ( ) ;
187
169
}
188
170
189
- let input = assetSource . source ( ) ;
190
-
191
- if ( ! Buffer . isBuffer ( input ) ) {
192
- input = Buffer . from ( input ) ;
193
- }
171
+ const scheduledTasks = [ ] ;
172
+ const cache = new CacheEngine (
173
+ compilation ,
174
+ { cache : this . options . cache } ,
175
+ weakCache
176
+ ) ;
194
177
195
- if ( input . length < this . options . threshold ) {
196
- yield false ;
197
- }
178
+ for ( const assetName of assetNames ) {
179
+ scheduledTasks . push (
180
+ ( async ( ) => {
181
+ const { source, info } = CompressionPlugin . getAsset (
182
+ compilation ,
183
+ assetName
184
+ ) ;
198
185
199
- const task = { assetName, assetSource, assetInfo, input, relatedName } ;
186
+ if ( info . compressed ) {
187
+ return ;
188
+ }
200
189
201
- if ( CompressionPlugin . isWebpack4 ( ) ) {
202
- task . cacheKeys = {
203
- nodeVersion : process . version ,
204
- // eslint-disable-next-line global-require
205
- 'compression-webpack-plugin' : require ( '../package.json' ) . version ,
206
- algorithm : this . algorithm ,
207
- originalAlgorithm : this . options . algorithm ,
208
- compressionOptions : this . compressionOptions ,
209
- assetName,
210
- contentHash : crypto . createHash ( 'md4' ) . update ( input ) . digest ( 'hex' ) ,
211
- } ;
212
- }
190
+ let relatedName ;
213
191
214
- yield task ;
215
- }
192
+ if ( typeof this . options . algorithm === 'function' ) {
193
+ let filenameForRelatedName = this . options . filename ;
216
194
217
- afterTask ( compilation , task ) {
218
- const { output, input } = task ;
195
+ const index = filenameForRelatedName . lastIndexOf ( '?' ) ;
219
196
220
- if ( output . source ( ) . length / input . length > this . options . minRatio ) {
221
- return ;
222
- }
197
+ if ( index >= 0 ) {
198
+ filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
199
+ }
223
200
224
- const { assetSource, assetName } = task ;
225
- const newAssetName = CompressionPlugin . interpolateName (
226
- assetName ,
227
- this . options . filename
228
- ) ;
201
+ relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
202
+ } else {
203
+ relatedName = `${ this . options . algorithm } ed` ;
204
+ }
229
205
230
- CompressionPlugin . emitAsset ( compilation , newAssetName , output , {
231
- compressed : true ,
232
- } ) ;
206
+ if ( info . related && info . related [ relatedName ] ) {
207
+ return ;
208
+ }
233
209
234
- if ( this . options . deleteOriginalAssets ) {
235
- // eslint-disable-next-line no-param-reassign
236
- CompressionPlugin . deleteAsset ( compilation , assetName ) ;
237
- } else {
238
- CompressionPlugin . updateAsset ( compilation , assetName , assetSource , {
239
- related : { [ task . relatedName ] : newAssetName } ,
240
- } ) ;
241
- }
242
- }
210
+ let input = source . source ( ) ;
243
211
244
- async runTasks ( compilation , assetNames , CacheEngine , weakCache ) {
245
- const scheduledTasks = [ ] ;
246
- const cache = new CacheEngine (
247
- compilation ,
248
- {
249
- cache : this . options . cache ,
250
- } ,
251
- weakCache
252
- ) ;
212
+ if ( ! Buffer . isBuffer ( input ) ) {
213
+ input = Buffer . from ( input ) ;
214
+ }
253
215
254
- for ( const assetName of assetNames ) {
255
- scheduledTasks . push (
256
- ( async ( ) => {
257
- const task = this . getTask ( compilation , assetName ) . next ( ) . value ;
216
+ if ( input . length < this . options . threshold ) {
217
+ return ;
218
+ }
258
219
259
- if ( ! task ) {
260
- return Promise . resolve ( ) ;
220
+ const cacheData = { source } ;
221
+
222
+ if ( CompressionPlugin . isWebpack4 ( ) ) {
223
+ cacheData . cacheKeys = {
224
+ nodeVersion : process . version ,
225
+ // eslint-disable-next-line global-require
226
+ 'compression-webpack-plugin' : require ( '../package.json' ) . version ,
227
+ algorithm : this . algorithm ,
228
+ originalAlgorithm : this . options . algorithm ,
229
+ compressionOptions : this . compressionOptions ,
230
+ assetName,
231
+ contentHash : crypto . createHash ( 'md4' ) . update ( input ) . digest ( 'hex' ) ,
232
+ } ;
233
+ } else {
234
+ cacheData . assetName = assetName ;
261
235
}
262
236
263
- task . output = await cache . get ( task , { RawSource } ) ;
237
+ let output = await cache . get ( cacheData , { RawSource } ) ;
264
238
265
- if ( ! task . output ) {
239
+ if ( ! output ) {
266
240
try {
267
- // eslint-disable-next-line no-param-reassign
268
- task . output = new RawSource ( await this . compress ( task . input ) ) ;
241
+ output = new RawSource ( await this . runCompressionAlgorithm ( input ) ) ;
269
242
} catch ( error ) {
270
243
compilation . errors . push ( error ) ;
271
244
272
- return Promise . resolve ( ) ;
245
+ return ;
273
246
}
274
247
275
- await cache . store ( task ) ;
248
+ cacheData . output = output ;
249
+
250
+ await cache . store ( cacheData ) ;
276
251
}
277
252
278
- this . afterTask ( compilation , task ) ;
253
+ if ( output . source ( ) . length / input . length > this . options . minRatio ) {
254
+ return ;
255
+ }
279
256
280
- return Promise . resolve ( ) ;
257
+ const newAssetName = CompressionPlugin . interpolateName (
258
+ assetName ,
259
+ this . options . filename
260
+ ) ;
261
+
262
+ CompressionPlugin . emitAsset ( compilation , newAssetName , output , {
263
+ compressed : true ,
264
+ } ) ;
265
+
266
+ if ( this . options . deleteOriginalAssets ) {
267
+ // eslint-disable-next-line no-param-reassign
268
+ CompressionPlugin . deleteAsset ( compilation , assetName ) ;
269
+ } else {
270
+ CompressionPlugin . updateAsset ( compilation , assetName , source , {
271
+ related : { [ relatedName ] : newAssetName } ,
272
+ } ) ;
273
+ }
281
274
} ) ( )
282
275
) ;
283
276
}
@@ -291,29 +284,6 @@ class CompressionPlugin {
291
284
292
285
apply ( compiler ) {
293
286
const pluginName = this . constructor . name ;
294
- const matchObject = ModuleFilenameHelpers . matchObject . bind (
295
- // eslint-disable-next-line no-undefined
296
- undefined ,
297
- this . options
298
- ) ;
299
- const compressionFn = async (
300
- compilation ,
301
- assets ,
302
- CacheEngine ,
303
- weakCache
304
- ) => {
305
- const assetNames = Object . keys (
306
- typeof assets === 'undefined' ? compilation . assets : assets
307
- ) . filter ( ( assetName ) => matchObject ( assetName ) ) ;
308
-
309
- if ( assetNames . length === 0 ) {
310
- return Promise . resolve ( ) ;
311
- }
312
-
313
- await this . runTasks ( compilation , assetNames , CacheEngine , weakCache ) ;
314
-
315
- return Promise . resolve ( ) ;
316
- } ;
317
287
318
288
if ( CompressionPlugin . isWebpack4 ( ) ) {
319
289
// eslint-disable-next-line global-require
@@ -322,7 +292,7 @@ class CompressionPlugin {
322
292
323
293
compiler . hooks . emit . tapPromise ( { name : pluginName } , ( compilation ) =>
324
294
// eslint-disable-next-line no-undefined
325
- compressionFn ( compilation , undefined , CacheEngine , weakCache )
295
+ this . compress ( compilation , undefined , CacheEngine , weakCache )
326
296
) ;
327
297
} else {
328
298
// eslint-disable-next-line global-require
@@ -337,7 +307,7 @@ class CompressionPlugin {
337
307
name : pluginName ,
338
308
stage : Compilation . PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER ,
339
309
} ,
340
- ( assets ) => compressionFn ( compilation , assets , CacheEngine )
310
+ ( assets ) => this . compress ( compilation , assets , CacheEngine )
341
311
) ;
342
312
343
313
compilation . hooks . statsPrinter . tap ( pluginName , ( stats ) => {
0 commit comments