@@ -8,20 +8,41 @@ import RawSource from 'webpack-sources/lib/RawSource';
88
99class CompressionPlugin {
1010 constructor ( options = { } ) {
11- this . asset = options . asset || '[path].gz[query]' ;
12- this . algorithm = options . algorithm || 'gzip' ;
13- this . filename = options . filename || false ;
14- this . compressionOptions = { } ;
11+ const {
12+ asset = '[path].gz[query]' ,
13+ test,
14+ regExp,
15+ algorithm = 'gzip' ,
16+ filename = false ,
17+ compressionOptions = { } ,
18+ cache = false ,
19+ threshold = 0 ,
20+ minRatio = 0.8 ,
21+ deleteOriginalAssets = false ,
22+ } = options ;
1523
16- if ( typeof this . algorithm === 'string' ) {
24+ this . options = {
25+ asset,
26+ test,
27+ regExp,
28+ algorithm,
29+ filename,
30+ compressionOptions,
31+ cache,
32+ threshold,
33+ minRatio,
34+ deleteOriginalAssets,
35+ } ;
36+
37+ if ( typeof algorithm === 'string' ) {
1738 const zlib = require ( 'zlib' ) ;
18- this . algorithm = zlib [ this . algorithm ] ;
39+ this . options . algorithm = zlib [ this . options . algorithm ] ;
1940
20- if ( ! this . algorithm ) {
41+ if ( ! this . options . algorithm ) {
2142 throw new Error ( 'Algorithm not found in zlib' ) ;
2243 }
2344
24- this . compressionOptions = {
45+ this . options . compressionOptions = {
2546 level : options . level || 9 ,
2647 flush : options . flush ,
2748 chunkSize : options . chunkSize ,
@@ -31,21 +52,17 @@ class CompressionPlugin {
3152 dictionary : options . dictionary ,
3253 } ;
3354 }
34- this . test = options . test || options . regExp ;
35- this . threshold = options . threshold || 0 ;
36- this . minRatio = options . minRatio || 0.8 ;
37- this . deleteOriginalAssets = options . deleteOriginalAssets || false ;
3855 }
3956
4057 apply ( compiler ) {
4158 compiler . plugin ( 'emit' , ( compilation , callback ) => {
4259 const assets = compilation . assets ;
4360 async . forEach ( Object . keys ( assets ) , ( file , cb ) => {
44- if ( Array . isArray ( this . test ) ) {
45- if ( this . test . every ( t => ! t . test ( file ) ) ) {
61+ if ( Array . isArray ( this . options . test ) ) {
62+ if ( this . options . test . every ( t => ! t . test ( file ) ) ) {
4663 return cb ( ) ;
4764 }
48- } else if ( this . test && ! this . test . test ( file ) ) {
65+ } else if ( this . options . test && ! this . options . test . test ( file ) ) {
4966 return cb ( ) ;
5067 }
5168 const asset = assets [ file ] ;
@@ -57,14 +74,14 @@ class CompressionPlugin {
5774
5875 const originalSize = content . length ;
5976
60- if ( originalSize < this . threshold ) {
77+ if ( originalSize < this . options . threshold ) {
6178 return cb ( ) ;
6279 }
6380
64- this . algorithm ( content , this . compressionOptions , ( err , result ) => {
81+ this . options . algorithm ( content , this . options . compressionOptions , ( err , result ) => {
6582 if ( err ) { return cb ( err ) ; }
6683
67- if ( result . length / originalSize > this . minRatio ) { return cb ( ) ; }
84+ if ( result . length / originalSize > this . options . minRatio ) { return cb ( ) ; }
6885
6986 const parse = url . parse ( file ) ;
7087 const sub = {
@@ -73,13 +90,13 @@ class CompressionPlugin {
7390 query : parse . query || '' ,
7491 } ;
7592
76- let newFile = this . asset . replace ( / \[ ( f i l e | p a t h | q u e r y ) \] / g, ( p0 , p1 ) => sub [ p1 ] ) ;
93+ let newFile = this . options . asset . replace ( / \[ ( f i l e | p a t h | q u e r y ) \] / g, ( p0 , p1 ) => sub [ p1 ] ) ;
7794
78- if ( typeof this . filename === 'function' ) {
79- newFile = this . filename ( newFile ) ;
95+ if ( typeof this . options . filename === 'function' ) {
96+ newFile = this . options . filename ( newFile ) ;
8097 }
8198 assets [ newFile ] = new RawSource ( result ) ;
82- if ( this . deleteOriginalAssets ) {
99+ if ( this . options . deleteOriginalAssets ) {
83100 delete assets [ file ] ;
84101 }
85102 cb ( ) ;
0 commit comments