@@ -4,6 +4,7 @@ import path from 'path';
4
4
import webpack from 'webpack' ;
5
5
import findCacheDir from 'find-cache-dir' ;
6
6
import cacache from 'cacache' ;
7
+ import WorkboxPlugin from 'workbox-webpack-plugin' ;
7
8
8
9
import Webpack4Cache from '../src/Webpack4Cache' ;
9
10
import CompressionPlugin from '../src/index' ;
@@ -16,7 +17,6 @@ import {
16
17
getCompiler ,
17
18
getErrors ,
18
19
getWarnings ,
19
- readAsset ,
20
20
removeCache ,
21
21
} from './helpers/index' ;
22
22
@@ -37,8 +37,6 @@ describe('CompressionPlugin', () => {
37
37
} ) ;
38
38
39
39
it ( 'should work' , async ( ) => {
40
- expect . assertions ( 6 ) ;
41
-
42
40
const compiler = getCompiler (
43
41
'./entry.js' ,
44
42
{ } ,
@@ -54,23 +52,8 @@ describe('CompressionPlugin', () => {
54
52
new CompressionPlugin ( ) . apply ( compiler ) ;
55
53
56
54
const stats = await compile ( compiler ) ;
57
- const { assets, assetsInfo } = stats . compilation ;
58
-
59
- for ( const assetName of Object . keys ( assets ) ) {
60
- const info = assetsInfo . get ( assetName ) ;
61
-
62
- if ( ! info . related ) {
63
- // eslint-disable-next-line no-continue
64
- continue ;
65
- }
66
-
67
- const originalBuffer = readAsset ( assetName , compiler , stats ) ;
68
- const gzipedBuffer = readAsset ( info . related . gziped , compiler , stats ) ;
69
-
70
- expect ( zlib . gunzipSync ( gzipedBuffer ) . equals ( originalBuffer ) ) . toBe ( true ) ;
71
- }
72
55
73
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
56
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
74
57
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
75
58
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
76
59
} ) ;
@@ -94,7 +77,7 @@ describe('CompressionPlugin', () => {
94
77
95
78
const stats = await compile ( compiler ) ;
96
79
97
- expect ( getAssetsNameAndSize ( stats , true ) ) . toMatchSnapshot ( 'assets' ) ;
80
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
98
81
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
99
82
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
100
83
} ) ;
@@ -140,7 +123,7 @@ describe('CompressionPlugin', () => {
140
123
const stats = await compile ( compiler ) ;
141
124
142
125
expect ( gzipSpy ) . toHaveBeenCalledTimes ( 5 ) ;
143
- expect ( getAssetsNameAndSize ( stats , true ) ) . toMatchSnapshot ( 'assets' ) ;
126
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
144
127
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
145
128
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
146
129
@@ -185,7 +168,7 @@ describe('CompressionPlugin', () => {
185
168
186
169
const stats = await compile ( compiler ) ;
187
170
188
- expect ( getAssetsNameAndSize ( stats , true ) ) . toMatchSnapshot ( 'assets' ) ;
171
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
189
172
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
190
173
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
191
174
} ) ;
@@ -213,7 +196,7 @@ describe('CompressionPlugin', () => {
213
196
expect ( printedCompressed ? printedCompressed . length : 0 ) . toBe (
214
197
getCompiler . isWebpack4 ( ) ? 0 : 3
215
198
) ;
216
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
199
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
217
200
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
218
201
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
219
202
} ) ;
@@ -240,7 +223,7 @@ describe('CompressionPlugin', () => {
240
223
expect ( info . immutable ) . toBe ( true ) ;
241
224
}
242
225
243
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
226
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
244
227
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
245
228
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
246
229
} ) ;
@@ -268,7 +251,7 @@ describe('CompressionPlugin', () => {
268
251
expect ( stats . compilation . emittedAssets . size ) . toBe ( 7 ) ;
269
252
}
270
253
271
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
254
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
272
255
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
273
256
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
274
257
@@ -285,7 +268,9 @@ describe('CompressionPlugin', () => {
285
268
expect ( newStats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
286
269
}
287
270
288
- expect ( getAssetsNameAndSize ( newStats ) ) . toMatchSnapshot ( 'assets' ) ;
271
+ expect ( getAssetsNameAndSize ( newStats , compiler ) ) . toMatchSnapshot (
272
+ 'assets'
273
+ ) ;
289
274
expect ( getWarnings ( newStats ) ) . toMatchSnapshot ( 'errors' ) ;
290
275
expect ( getErrors ( newStats ) ) . toMatchSnapshot ( 'warnings' ) ;
291
276
@@ -329,7 +314,7 @@ describe('CompressionPlugin', () => {
329
314
expect ( stats . compilation . emittedAssets . size ) . toBe ( 7 ) ;
330
315
}
331
316
332
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
317
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
333
318
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
334
319
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
335
320
@@ -346,7 +331,9 @@ describe('CompressionPlugin', () => {
346
331
expect ( newStats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
347
332
}
348
333
349
- expect ( getAssetsNameAndSize ( newStats ) ) . toMatchSnapshot ( 'assets' ) ;
334
+ expect ( getAssetsNameAndSize ( newStats , compiler ) ) . toMatchSnapshot (
335
+ 'assets'
336
+ ) ;
350
337
expect ( getWarnings ( newStats ) ) . toMatchSnapshot ( 'errors' ) ;
351
338
expect ( getErrors ( newStats ) ) . toMatchSnapshot ( 'warnings' ) ;
352
339
@@ -390,7 +377,7 @@ describe('CompressionPlugin', () => {
390
377
expect ( stats . compilation . emittedAssets . size ) . toBe ( 7 ) ;
391
378
}
392
379
393
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
380
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
394
381
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
395
382
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
396
383
@@ -409,7 +396,9 @@ describe('CompressionPlugin', () => {
409
396
expect ( newStats . compilation . emittedAssets . size ) . toBe ( 2 ) ;
410
397
}
411
398
412
- expect ( getAssetsNameAndSize ( newStats ) ) . toMatchSnapshot ( 'assets' ) ;
399
+ expect ( getAssetsNameAndSize ( newStats , compiler ) ) . toMatchSnapshot (
400
+ 'assets'
401
+ ) ;
413
402
expect ( getWarnings ( newStats ) ) . toMatchSnapshot ( 'errors' ) ;
414
403
expect ( getErrors ( newStats ) ) . toMatchSnapshot ( 'warnings' ) ;
415
404
@@ -467,7 +456,7 @@ describe('CompressionPlugin', () => {
467
456
expect ( stats . compilation . emittedAssets . size ) . toBe ( 14 ) ;
468
457
}
469
458
470
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
459
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
471
460
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
472
461
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
473
462
@@ -484,7 +473,9 @@ describe('CompressionPlugin', () => {
484
473
expect ( newStats . compilation . emittedAssets . size ) . toBe ( 0 ) ;
485
474
}
486
475
487
- expect ( getAssetsNameAndSize ( newStats ) ) . toMatchSnapshot ( 'assets' ) ;
476
+ expect ( getAssetsNameAndSize ( newStats , compiler ) ) . toMatchSnapshot (
477
+ 'assets'
478
+ ) ;
488
479
expect ( getWarnings ( newStats ) ) . toMatchSnapshot ( 'errors' ) ;
489
480
expect ( getErrors ( newStats ) ) . toMatchSnapshot ( 'warnings' ) ;
490
481
@@ -526,7 +517,7 @@ describe('CompressionPlugin', () => {
526
517
expect ( stats . compilation . emittedAssets . size ) . toBe ( 7 ) ;
527
518
}
528
519
529
- expect ( getAssetsNameAndSize ( stats ) ) . toMatchSnapshot ( 'assets' ) ;
520
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
530
521
expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
531
522
expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
532
523
@@ -543,11 +534,63 @@ describe('CompressionPlugin', () => {
543
534
expect ( newStats . compilation . emittedAssets . size ) . toBe ( 7 ) ;
544
535
}
545
536
546
- expect ( getAssetsNameAndSize ( newStats ) ) . toMatchSnapshot ( 'assets' ) ;
537
+ expect ( getAssetsNameAndSize ( newStats , compiler ) ) . toMatchSnapshot (
538
+ 'assets'
539
+ ) ;
547
540
expect ( getWarnings ( newStats ) ) . toMatchSnapshot ( 'errors' ) ;
548
541
expect ( getErrors ( newStats ) ) . toMatchSnapshot ( 'warnings' ) ;
549
542
550
543
resolve ( ) ;
551
544
} ) ;
552
545
} ) ;
546
+
547
+ // TODO https://github.com/webpack-contrib/compression-webpack-plugin/issues/218
548
+ it . skip ( 'should work with "workbox-webpack-plugin" plugin ("GenerateSW")' , async ( ) => {
549
+ const compiler = getCompiler (
550
+ './entry.js' ,
551
+ { } ,
552
+ {
553
+ output : {
554
+ filename : '[name].js' ,
555
+ chunkFilename : '[id].[name].js' ,
556
+ } ,
557
+ }
558
+ ) ;
559
+
560
+ new WorkboxPlugin . GenerateSW ( ) . apply ( compiler ) ;
561
+ new CompressionPlugin ( ) . apply ( compiler ) ;
562
+
563
+ const stats = await compile ( compiler ) ;
564
+
565
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
566
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
567
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
568
+ } ) ;
569
+
570
+ // TODO https://github.com/webpack-contrib/compression-webpack-plugin/issues/218
571
+ it . skip ( 'should work with "workbox-webpack-plugin" plugin ("InjectManifest")' , async ( ) => {
572
+ const compiler = getCompiler (
573
+ './entry.js' ,
574
+ { } ,
575
+ {
576
+ output : {
577
+ filename : '[name].js' ,
578
+ chunkFilename : '[id].[name].js' ,
579
+ } ,
580
+ }
581
+ ) ;
582
+
583
+ new WorkboxPlugin . InjectManifest ( {
584
+ swSrc : path . resolve ( __dirname , './fixtures/sw.js' ) ,
585
+ swDest : 'sw.js' ,
586
+ exclude : [ / \. ( g z | b r ) $ / ] ,
587
+ } ) . apply ( compiler ) ;
588
+ new CompressionPlugin ( ) . apply ( compiler ) ;
589
+
590
+ const stats = await compile ( compiler ) ;
591
+
592
+ expect ( getAssetsNameAndSize ( stats , compiler ) ) . toMatchSnapshot ( 'assets' ) ;
593
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( 'warnings' ) ;
594
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( 'errors' ) ;
595
+ } ) ;
553
596
} ) ;
0 commit comments