@@ -104,86 +104,72 @@ class CompressionPlugin {
104
104
105
105
async compress ( compiler , compilation , assets ) {
106
106
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 ) ;
110
111
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
+ }
127
115
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
+ }
131
125
132
- if ( input . length < this . options . threshold ) {
133
- return accumulator ;
134
- }
126
+ let relatedName ;
135
127
136
- let relatedName ;
128
+ if ( typeof this . options . algorithm === "function" ) {
129
+ let filenameForRelatedName = this . options . filename ;
137
130
138
- if ( typeof this . options . algorithm === "function" ) {
139
- let filenameForRelatedName = this . options . filename ;
131
+ const index = filenameForRelatedName . indexOf ( "?" ) ;
140
132
141
- const index = filenameForRelatedName . lastIndexOf ( "?" ) ;
133
+ if ( index >= 0 ) {
134
+ filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
135
+ }
142
136
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` ;
145
142
}
146
143
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
+ }
153
147
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 ( ) ) || { } ;
157
157
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 ;
167
159
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 ( ) ;
171
163
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
+ }
183
168
184
- return accumulator ;
185
- } , [ ] )
186
- ) ;
169
+ return { name, source, info, buffer, output, cacheItem, relatedName } ;
170
+ } )
171
+ )
172
+ ) . filter ( ( assetForMinify ) => Boolean ( assetForMinify ) ) ;
187
173
188
174
const { RawSource } = compiler . webpack . sources ;
189
175
const scheduledTasks = [ ] ;
@@ -193,28 +179,37 @@ class CompressionPlugin {
193
179
( async ( ) => {
194
180
const {
195
181
name,
196
- inputSource,
197
- input,
182
+ source,
183
+ buffer,
184
+ output,
198
185
cacheItem,
199
186
info,
200
187
relatedName,
201
188
} = asset ;
202
- let { output } = asset ;
203
189
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 } ) ;
209
206
210
207
return ;
211
208
}
212
209
213
- await cacheItem . storePromise ( output ) ;
214
- }
210
+ output . source = new RawSource ( output . compressed ) ;
215
211
216
- if ( output . source ( ) . length / input . length > this . options . minRatio ) {
217
- return ;
212
+ await cacheItem . storePromise ( output ) ;
218
213
}
219
214
220
215
const match = / ^ ( [ ^ ? # ] * ) ( \? [ ^ # ] * ) ? ( # .* ) ? $ / . exec ( name ) ;
@@ -260,19 +255,19 @@ class CompressionPlugin {
260
255
261
256
if ( this . options . deleteOriginalAssets ) {
262
257
if ( this . options . deleteOriginalAssets === "keep-source-map" ) {
263
- compilation . updateAsset ( name , inputSource , {
258
+ compilation . updateAsset ( name , source , {
264
259
related : { sourceMap : null } ,
265
260
} ) ;
266
261
}
267
262
268
263
compilation . deleteAsset ( name ) ;
269
264
} else {
270
- compilation . updateAsset ( name , inputSource , {
265
+ compilation . updateAsset ( name , source , {
271
266
related : { [ relatedName ] : newName } ,
272
267
} ) ;
273
268
}
274
269
275
- compilation . emitAsset ( newName , output , newInfo ) ;
270
+ compilation . emitAsset ( newName , output . source , newInfo ) ;
276
271
} ) ( )
277
272
) ;
278
273
}
0 commit comments