3
3
Author Tobias Koppers @sokra
4
4
*/
5
5
6
- import crypto from ' crypto' ;
7
- import path from ' path' ;
6
+ import crypto from " crypto" ;
7
+ import path from " path" ;
8
8
9
9
import webpack , {
10
10
ModuleFilenameHelpers ,
11
11
version as webpackVersion ,
12
- } from ' webpack' ;
13
- import { validate } from ' schema-utils' ;
14
- import serialize from ' serialize-javascript' ;
12
+ } from " webpack" ;
13
+ import { validate } from " schema-utils" ;
14
+ import serialize from " serialize-javascript" ;
15
15
16
- import schema from ' ./options.json' ;
16
+ import schema from " ./options.json" ;
17
17
18
18
const { RawSource } =
19
19
// eslint-disable-next-line global-require
20
- webpack . sources || require ( ' webpack-sources' ) ;
20
+ webpack . sources || require ( " webpack-sources" ) ;
21
21
22
22
class CompressionPlugin {
23
23
constructor ( options = { } ) {
24
24
validate ( schema , options , {
25
- name : ' Compression Plugin' ,
26
- baseDataPath : ' options' ,
25
+ name : " Compression Plugin" ,
26
+ baseDataPath : " options" ,
27
27
} ) ;
28
28
29
29
const {
30
30
test,
31
31
include,
32
32
exclude,
33
33
cache = true ,
34
- algorithm = ' gzip' ,
34
+ algorithm = " gzip" ,
35
35
compressionOptions = { } ,
36
- filename = ' [path][base].gz' ,
36
+ filename = " [path][base].gz" ,
37
37
threshold = 0 ,
38
38
minRatio = 0.8 ,
39
39
deleteOriginalAssets = false ,
@@ -54,9 +54,9 @@ class CompressionPlugin {
54
54
55
55
this . algorithm = this . options . algorithm ;
56
56
57
- if ( typeof this . algorithm === ' string' ) {
57
+ if ( typeof this . algorithm === " string" ) {
58
58
// eslint-disable-next-line global-require
59
- const zlib = require ( ' zlib' ) ;
59
+ const zlib = require ( " zlib" ) ;
60
60
61
61
this . algorithm = zlib [ this . algorithm ] ;
62
62
@@ -157,7 +157,7 @@ class CompressionPlugin {
157
157
158
158
async compress ( compilation , assets , CacheEngine , weakCache ) {
159
159
const assetNames = Object . keys (
160
- typeof assets === ' undefined' ? compilation . assets : assets
160
+ typeof assets === " undefined" ? compilation . assets : assets
161
161
) . filter ( ( assetName ) =>
162
162
// eslint-disable-next-line no-undefined
163
163
ModuleFilenameHelpers . matchObject . bind ( undefined , this . options ) ( assetName )
@@ -188,18 +188,18 @@ class CompressionPlugin {
188
188
189
189
let relatedName ;
190
190
191
- if ( typeof this . options . algorithm === ' function' ) {
191
+ if ( typeof this . options . algorithm === " function" ) {
192
192
let filenameForRelatedName = this . options . filename ;
193
193
194
- const index = filenameForRelatedName . lastIndexOf ( '?' ) ;
194
+ const index = filenameForRelatedName . lastIndexOf ( "?" ) ;
195
195
196
196
if ( index >= 0 ) {
197
197
filenameForRelatedName = filenameForRelatedName . substr ( 0 , index ) ;
198
198
}
199
199
200
200
relatedName = `${ path . extname ( filenameForRelatedName ) . slice ( 1 ) } ed` ;
201
- } else if ( this . options . algorithm === ' gzip' ) {
202
- relatedName = ' gzipped' ;
201
+ } else if ( this . options . algorithm === " gzip" ) {
202
+ relatedName = " gzipped" ;
203
203
} else {
204
204
relatedName = `${ this . options . algorithm } ed` ;
205
205
}
@@ -224,12 +224,12 @@ class CompressionPlugin {
224
224
cacheData . cacheKeys = {
225
225
nodeVersion : process . version ,
226
226
// eslint-disable-next-line global-require
227
- ' compression-webpack-plugin' : require ( ' ../package.json' ) . version ,
227
+ " compression-webpack-plugin" : require ( " ../package.json" ) . version ,
228
228
algorithm : this . algorithm ,
229
229
originalAlgorithm : this . options . algorithm ,
230
230
compressionOptions : this . options . compressionOptions ,
231
231
name,
232
- contentHash : crypto . createHash ( ' md4' ) . update ( input ) . digest ( ' hex' ) ,
232
+ contentHash : crypto . createHash ( " md4" ) . update ( input ) . digest ( " hex" ) ,
233
233
} ;
234
234
} else {
235
235
cacheData . name = serialize ( {
@@ -261,8 +261,8 @@ class CompressionPlugin {
261
261
262
262
const match = / ^ ( [ ^ ? # ] * ) ( \? [ ^ # ] * ) ? ( # .* ) ? $ / . exec ( name ) ;
263
263
const [ , replacerFile ] = match ;
264
- const replacerQuery = match [ 2 ] || '' ;
265
- const replacerFragment = match [ 3 ] || '' ;
264
+ const replacerQuery = match [ 2 ] || "" ;
265
+ const replacerFragment = match [ 3 ] || "" ;
266
266
const replacerExt = path . extname ( replacerFile ) ;
267
267
const replacerBase = path . basename ( replacerFile ) ;
268
268
const replacerName = replacerBase . slice (
@@ -280,12 +280,12 @@ class CompressionPlugin {
280
280
path : replacerPath ,
281
281
base : replacerBase ,
282
282
name : replacerName ,
283
- ext : replacerExt || '' ,
283
+ ext : replacerExt || "" ,
284
284
} ;
285
285
286
286
let newFilename = this . options . filename ;
287
287
288
- if ( typeof newFilename === ' function' ) {
288
+ if ( typeof newFilename === " function" ) {
289
289
newFilename = newFilename ( pathData ) ;
290
290
}
291
291
@@ -301,7 +301,7 @@ class CompressionPlugin {
301
301
}
302
302
303
303
if ( this . options . deleteOriginalAssets ) {
304
- if ( this . options . deleteOriginalAssets === ' keep-source-map' ) {
304
+ if ( this . options . deleteOriginalAssets === " keep-source-map" ) {
305
305
// TODO `...` required only for webpack@4
306
306
const updatedAssetInfo = {
307
307
...info ,
@@ -341,15 +341,15 @@ class CompressionPlugin {
341
341
}
342
342
343
343
static isWebpack4 ( ) {
344
- return webpackVersion [ 0 ] === '4' ;
344
+ return webpackVersion [ 0 ] === "4" ;
345
345
}
346
346
347
347
apply ( compiler ) {
348
348
const pluginName = this . constructor . name ;
349
349
350
350
if ( CompressionPlugin . isWebpack4 ( ) ) {
351
351
// eslint-disable-next-line global-require
352
- const CacheEngine = require ( ' ./Webpack4Cache' ) . default ;
352
+ const CacheEngine = require ( " ./Webpack4Cache" ) . default ;
353
353
const weakCache = new WeakMap ( ) ;
354
354
355
355
compiler . hooks . emit . tapPromise ( { name : pluginName } , ( compilation ) =>
@@ -358,11 +358,11 @@ class CompressionPlugin {
358
358
) ;
359
359
} else {
360
360
// eslint-disable-next-line global-require
361
- const CacheEngine = require ( ' ./Webpack5Cache' ) . default ;
361
+ const CacheEngine = require ( " ./Webpack5Cache" ) . default ;
362
362
363
363
compiler . hooks . thisCompilation . tap ( pluginName , ( compilation ) => {
364
364
// eslint-disable-next-line global-require
365
- const Compilation = require ( ' webpack/lib/Compilation' ) ;
365
+ const Compilation = require ( " webpack/lib/Compilation" ) ;
366
366
367
367
compilation . hooks . processAssets . tapPromise (
368
368
{
@@ -374,12 +374,12 @@ class CompressionPlugin {
374
374
375
375
compilation . hooks . statsPrinter . tap ( pluginName , ( stats ) => {
376
376
stats . hooks . print
377
- . for ( ' asset.info.compressed' )
377
+ . for ( " asset.info.compressed" )
378
378
. tap (
379
- ' compression-webpack-plugin' ,
379
+ " compression-webpack-plugin" ,
380
380
( compressed , { green, formatFlag } ) =>
381
381
// eslint-disable-next-line no-undefined
382
- compressed ? green ( formatFlag ( ' compressed' ) ) : undefined
382
+ compressed ? green ( formatFlag ( " compressed" ) ) : undefined
383
383
) ;
384
384
} ) ;
385
385
} ) ;
0 commit comments