@@ -7,12 +7,18 @@ import crypto from 'crypto';
77import url from 'url' ;
88import path from 'path' ;
99
10- import RawSource from 'webpack-sources/lib/RawSource' ;
11- import { ModuleFilenameHelpers , version as webpackVersion } from 'webpack' ;
10+ import webpack , {
11+ ModuleFilenameHelpers ,
12+ version as webpackVersion ,
13+ } from 'webpack' ;
1214import validateOptions from 'schema-utils' ;
1315
1416import schema from './options.json' ;
1517
18+ const { RawSource } =
19+ // eslint-disable-next-line global-require
20+ webpack . sources || require ( 'webpack-sources' ) ;
21+
1622class CompressionPlugin {
1723 constructor ( options = { } ) {
1824 validateOptions ( schema , options , {
@@ -203,29 +209,20 @@ class CompressionPlugin {
203209 yield task ;
204210 }
205211
206- afterTask ( compilation , task , weakCache ) {
212+ afterTask ( compilation , task ) {
207213 const { output, input } = task ;
208214
209- if ( output . length / input . length > this . options . minRatio ) {
215+ if ( output . source ( ) . length / input . length > this . options . minRatio ) {
210216 return ;
211217 }
212218
213219 const { assetSource, assetName } = task ;
214-
215- let weakOutput = weakCache . get ( assetSource ) ;
216-
217- if ( ! weakOutput ) {
218- weakOutput = new RawSource ( output ) ;
219-
220- weakCache . set ( assetSource , weakOutput ) ;
221- }
222-
223220 const newAssetName = CompressionPlugin . interpolateName (
224221 assetName ,
225222 this . options . filename
226223 ) ;
227224
228- CompressionPlugin . emitAsset ( compilation , newAssetName , weakOutput , {
225+ CompressionPlugin . emitAsset ( compilation , newAssetName , output , {
229226 compressed : true ,
230227 } ) ;
231228
@@ -241,28 +238,15 @@ class CompressionPlugin {
241238
242239 async runTasks ( compilation , assetNames , CacheEngine , weakCache ) {
243240 const scheduledTasks = [ ] ;
244- const cache = new CacheEngine ( compilation , {
245- cache : this . options . cache ,
246- } ) ;
241+ const cache = new CacheEngine (
242+ compilation ,
243+ {
244+ cache : this . options . cache ,
245+ } ,
246+ weakCache
247+ ) ;
247248
248249 for ( const assetName of assetNames ) {
249- const enqueue = async ( task ) => {
250- try {
251- // eslint-disable-next-line no-param-reassign
252- task . output = await this . compress ( task . input ) ;
253- } catch ( error ) {
254- compilation . errors . push ( error ) ;
255-
256- return ;
257- }
258-
259- if ( cache . isEnabled ( ) ) {
260- await cache . store ( task ) ;
261- }
262-
263- this . afterTask ( compilation , task , weakCache ) ;
264- } ;
265-
266250 scheduledTasks . push (
267251 ( async ( ) => {
268252 const task = this . getTask ( compilation , assetName ) . next ( ) . value ;
@@ -271,23 +255,24 @@ class CompressionPlugin {
271255 return Promise . resolve ( ) ;
272256 }
273257
274- if ( cache . isEnabled ( ) ) {
258+ task . output = await cache . get ( task , { RawSource } ) ;
259+
260+ if ( ! task . output ) {
275261 try {
276- task . output = await cache . get ( task ) ;
277- } catch ( ignoreError ) {
278- return enqueue ( task ) ;
279- }
262+ // eslint-disable-next-line no-param-reassign
263+ task . output = new RawSource ( await this . compress ( task . input ) ) ;
264+ } catch ( error ) {
265+ compilation . errors . push ( error ) ;
280266
281- if ( ! task . output ) {
282- return enqueue ( task ) ;
267+ return Promise . resolve ( ) ;
283268 }
284269
285- this . afterTask ( compilation , task , weakCache ) ;
286-
287- return Promise . resolve ( ) ;
270+ await cache . store ( task ) ;
288271 }
289272
290- return enqueue ( task ) ;
273+ this . afterTask ( compilation , task ) ;
274+
275+ return Promise . resolve ( ) ;
291276 } ) ( )
292277 ) ;
293278 }
@@ -306,8 +291,12 @@ class CompressionPlugin {
306291 undefined ,
307292 this . options
308293 ) ;
309- const weakCache = new WeakMap ( ) ;
310- const compressionFn = async ( compilation , CacheEngine , assets ) => {
294+ const compressionFn = async (
295+ compilation ,
296+ assets ,
297+ CacheEngine ,
298+ weakCache
299+ ) => {
311300 const assetNames = Object . keys (
312301 typeof assets === 'undefined' ? compilation . assets : assets
313302 ) . filter ( ( assetName ) => matchObject ( assetName ) ) ;
@@ -324,11 +313,11 @@ class CompressionPlugin {
324313 if ( CompressionPlugin . isWebpack4 ( ) ) {
325314 // eslint-disable-next-line global-require
326315 const CacheEngine = require ( './Webpack4Cache' ) . default ;
316+ const weakCache = new WeakMap ( ) ;
327317
328- compiler . hooks . emit . tapPromise (
329- { name : pluginName } ,
318+ compiler . hooks . emit . tapPromise ( { name : pluginName } , ( compilation ) =>
330319 // eslint-disable-next-line no-undefined
331- ( compilation ) => compressionFn ( compilation , CacheEngine )
320+ compressionFn ( compilation , undefined , CacheEngine , weakCache )
332321 ) ;
333322 } else {
334323 // eslint-disable-next-line global-require
@@ -343,7 +332,7 @@ class CompressionPlugin {
343332 name : pluginName ,
344333 stage : Compilation . PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER ,
345334 } ,
346- ( assets ) => compressionFn ( compilation , CacheEngine , assets )
335+ ( assets ) => compressionFn ( compilation , assets , CacheEngine )
347336 ) ;
348337
349338 compilation . hooks . statsPrinter . tap ( pluginName , ( stats ) => {
0 commit comments