@@ -105,85 +105,101 @@ class CompressionPlugin {
105105 async compress ( compiler , compilation , assets ) {
106106 const cache = compilation . getCache ( "CompressionWebpackPlugin" ) ;
107107 const assetsForMinify = await Promise . all (
108- Object . keys ( assets )
109- . filter ( ( name ) => {
110- const { info } = compilation . getAsset ( name ) ;
108+ Object . keys ( assets ) . reduce ( ( accumulator , name ) => {
109+ const { info, source } = compilation . getAsset ( name ) ;
111110
112- // Skip double minimize assets from child compilation
113- if ( info . compressed ) {
114- return false ;
115- }
111+ // Skip double minimize assets from child compilation
112+ if ( info . compressed ) {
113+ return accumulator ;
114+ }
116115
117- if (
118- ! compiler . webpack . ModuleFilenameHelpers . matchObject . bind (
119- // eslint-disable-next-line no-undefined
120- undefined ,
121- this . options
122- ) ( name )
123- ) {
124- return false ;
125- }
116+ if (
117+ ! compiler . webpack . ModuleFilenameHelpers . matchObject . bind (
118+ // eslint-disable-next-line no-undefined
119+ undefined ,
120+ this . options
121+ ) ( name )
122+ ) {
123+ return accumulator ;
124+ }
126125
127- return true ;
128- } )
129- . map ( async ( name ) => {
130- const { info, source } = compilation . getAsset ( name ) ;
126+ let input = source . source ( ) ;
131127
132- const eTag = cache . getLazyHashedEtag ( source ) ;
133- const cacheItem = cache . getItemCache (
134- serialize ( {
135- name,
136- algorithm : this . options . algorithm ,
137- compressionOptions : this . options . compressionOptions ,
138- } ) ,
139- eTag
140- ) ;
141- const output = await cacheItem . getPromise ( ) ;
128+ if ( ! Buffer . isBuffer ( input ) ) {
129+ input = Buffer . from ( input ) ;
130+ }
142131
143- return { name , info , inputSource : source , output , cacheItem } ;
144- } )
145- ) ;
132+ if ( input . length < this . options . threshold ) {
133+ return accumulator ;
134+ }
146135
147- const { RawSource } = compiler . webpack . sources ;
148- const scheduledTasks = [ ] ;
136+ let relatedName ;
149137
150- for ( const asset of assetsForMinify ) {
151- scheduledTasks . push (
152- ( async ( ) => {
153- const { name, inputSource, cacheItem, info } = asset ;
154- let { output } = asset ;
138+ if ( typeof this . options . algorithm === "function" ) {
139+ let filenameForRelatedName = this . options . filename ;
155140
156- let input = inputSource . source ( ) ;
141+ const index = filenameForRelatedName . lastIndexOf ( "?" ) ;
157142
158- if ( ! Buffer . isBuffer ( input ) ) {
159- input = Buffer . from ( input ) ;
143+ if ( index >= 0 ) {
144+ filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
160145 }
161146
162- if ( input . length < this . options . threshold ) {
163- return ;
164- }
147+ relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
148+ } else if ( this . options . algorithm === "gzip" ) {
149+ relatedName = "gzipped" ;
150+ } else {
151+ relatedName = `${ this . options . algorithm } ed` ;
152+ }
165153
166- let relatedName ;
154+ if ( info . related && info . related [ relatedName ] ) {
155+ return accumulator ;
156+ }
167157
168- if ( typeof this . options . algorithm === "function" ) {
169- let filenameForRelatedName = this . options . filename ;
158+ const eTag = cache . getLazyHashedEtag ( source ) ;
159+ const cacheItem = cache . getItemCache (
160+ serialize ( {
161+ name,
162+ algorithm : this . options . algorithm ,
163+ compressionOptions : this . options . compressionOptions ,
164+ } ) ,
165+ eTag
166+ ) ;
170167
171- const index = filenameForRelatedName . lastIndexOf ( "?" ) ;
168+ accumulator . push (
169+ ( async ( ) => {
170+ const output = await cacheItem . getPromise ( ) ;
172171
173- if ( index >= 0 ) {
174- filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
175- }
172+ return {
173+ name,
174+ inputSource : source ,
175+ info,
176+ input,
177+ output,
178+ cacheItem,
179+ relatedName,
180+ } ;
181+ } ) ( )
182+ ) ;
176183
177- relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
178- } else if ( this . options . algorithm === "gzip" ) {
179- relatedName = "gzipped" ;
180- } else {
181- relatedName = `${ this . options . algorithm } ed` ;
182- }
184+ return accumulator ;
185+ } , [ ] )
186+ ) ;
183187
184- if ( info . related && info . related [ relatedName ] ) {
185- return ;
186- }
188+ const { RawSource } = compiler . webpack . sources ;
189+ const scheduledTasks = [ ] ;
190+
191+ for ( const asset of assetsForMinify ) {
192+ scheduledTasks . push (
193+ ( async ( ) => {
194+ const {
195+ name,
196+ inputSource,
197+ input,
198+ cacheItem,
199+ info,
200+ relatedName,
201+ } = asset ;
202+ let { output } = asset ;
187203
188204 if ( ! output ) {
189205 try {
0 commit comments