@@ -104,86 +104,72 @@ class CompressionPlugin {
104104
105105 async compress ( compiler , compilation , assets ) {
106106 const cache = compilation . getCache ( "CompressionWebpackPlugin" ) ;
107- const assetsForMinify = await Promise . all (
108- Object . keys ( assets ) . reduce ( ( accumulator , name ) => {
109- const { info, source } = compilation . getAsset ( name ) ;
107+ const assetsForMinify = (
108+ await Promise . all (
109+ Object . keys ( assets ) . map ( async ( name ) => {
110+ const { info, source } = compilation . getAsset ( name ) ;
110111
111- // Skip double minimize assets from child compilation
112- if ( info . compressed ) {
113- return accumulator ;
114- }
115-
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- }
125-
126- let input = source . source ( ) ;
112+ if ( info . compressed ) {
113+ return false ;
114+ }
127115
128- if ( ! Buffer . isBuffer ( input ) ) {
129- input = Buffer . from ( input ) ;
130- }
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 false ;
124+ }
131125
132- if ( input . length < this . options . threshold ) {
133- return accumulator ;
134- }
126+ let relatedName ;
135127
136- let relatedName ;
128+ if ( typeof this . options . algorithm === "function" ) {
129+ let filenameForRelatedName = this . options . filename ;
137130
138- if ( typeof this . options . algorithm === "function" ) {
139- let filenameForRelatedName = this . options . filename ;
131+ const index = filenameForRelatedName . indexOf ( "?" ) ;
140132
141- const index = filenameForRelatedName . lastIndexOf ( "?" ) ;
133+ if ( index >= 0 ) {
134+ filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
135+ }
142136
143- if ( index >= 0 ) {
144- filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
137+ relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
138+ } else if ( this . options . algorithm === "gzip" ) {
139+ relatedName = "gzipped" ;
140+ } else {
141+ relatedName = `${ this . options . algorithm } ed` ;
145142 }
146143
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- }
144+ if ( info . related && info . related [ relatedName ] ) {
145+ return false ;
146+ }
153147
154- if ( info . related && info . related [ relatedName ] ) {
155- return accumulator ;
156- }
148+ const cacheItem = cache . getItemCache (
149+ serialize ( {
150+ name,
151+ algorithm : this . options . algorithm ,
152+ compressionOptions : this . options . compressionOptions ,
153+ } ) ,
154+ cache . getLazyHashedEtag ( source )
155+ ) ;
156+ const output = ( await cacheItem . getPromise ( ) ) || { } ;
157157
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- ) ;
158+ let buffer ;
167159
168- accumulator . push (
169- ( async ( ) => {
170- const output = await cacheItem . getPromise ( ) ;
160+ // No need original buffer for cached files
161+ if ( ! output . source ) {
162+ buffer = source . buffer ( ) ;
171163
172- return {
173- name,
174- inputSource : source ,
175- info,
176- input,
177- output,
178- cacheItem,
179- relatedName,
180- } ;
181- } ) ( )
182- ) ;
164+ if ( buffer . length < this . options . threshold ) {
165+ return false ;
166+ }
167+ }
183168
184- return accumulator ;
185- } , [ ] )
186- ) ;
169+ return { name, source, info, buffer, output, cacheItem, relatedName } ;
170+ } )
171+ )
172+ ) . filter ( ( assetForMinify ) => Boolean ( assetForMinify ) ) ;
187173
188174 const { RawSource } = compiler . webpack . sources ;
189175 const scheduledTasks = [ ] ;
@@ -193,28 +179,37 @@ class CompressionPlugin {
193179 ( async ( ) => {
194180 const {
195181 name,
196- inputSource,
197- input,
182+ source,
183+ buffer,
184+ output,
198185 cacheItem,
199186 info,
200187 relatedName,
201188 } = asset ;
202- let { output } = asset ;
203189
204- if ( ! output ) {
205- try {
206- output = new RawSource ( await this . runCompressionAlgorithm ( input ) ) ;
207- } catch ( error ) {
208- compilation . errors . push ( error ) ;
190+ if ( ! output . source ) {
191+ if ( ! output . compressed ) {
192+ try {
193+ output . compressed = await this . runCompressionAlgorithm ( buffer ) ;
194+ } catch ( error ) {
195+ compilation . errors . push ( error ) ;
196+
197+ return ;
198+ }
199+ }
200+
201+ if (
202+ output . compressed . length / buffer . length >
203+ this . options . minRatio
204+ ) {
205+ await cacheItem . storePromise ( { compressed : output . compressed } ) ;
209206
210207 return ;
211208 }
212209
213- await cacheItem . storePromise ( output ) ;
214- }
210+ output . source = new RawSource ( output . compressed ) ;
215211
216- if ( output . source ( ) . length / input . length > this . options . minRatio ) {
217- return ;
212+ await cacheItem . storePromise ( output ) ;
218213 }
219214
220215 const match = / ^ ( [ ^ ? # ] * ) ( \? [ ^ # ] * ) ? ( # .* ) ? $ / . exec ( name ) ;
@@ -260,19 +255,19 @@ class CompressionPlugin {
260255
261256 if ( this . options . deleteOriginalAssets ) {
262257 if ( this . options . deleteOriginalAssets === "keep-source-map" ) {
263- compilation . updateAsset ( name , inputSource , {
258+ compilation . updateAsset ( name , source , {
264259 related : { sourceMap : null } ,
265260 } ) ;
266261 }
267262
268263 compilation . deleteAsset ( name ) ;
269264 } else {
270- compilation . updateAsset ( name , inputSource , {
265+ compilation . updateAsset ( name , source , {
271266 related : { [ relatedName ] : newName } ,
272267 } ) ;
273268 }
274269
275- compilation . emitAsset ( newName , output , newInfo ) ;
270+ compilation . emitAsset ( newName , output . source , newInfo ) ;
276271 } ) ( )
277272 ) ;
278273 }
0 commit comments