Skip to content

Commit 5e3bb95

Browse files
fix: compatibility with child compilations
1 parent 7eaf1ff commit 5e3bb95

38 files changed

+4791
-115
lines changed

package-lock.json

+456-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"npm-run-all": "^4.1.5",
7272
"prettier": "^2.1.2",
7373
"standard-version": "^9.0.0",
74-
"webpack": "^5.4.0"
74+
"webpack": "^5.4.0",
75+
"workbox-webpack-plugin": "^6.0.0-alpha.3"
7576
},
7677
"keywords": [
7778
"webpack"

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ class CompressionPlugin {
198198
}
199199

200200
relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
201+
} else if (this.options.algorithm === 'gzip') {
202+
relatedName = 'gzipped';
201203
} else {
202204
relatedName = `${this.options.algorithm}ed`;
203205
}
@@ -358,7 +360,7 @@ class CompressionPlugin {
358360
// eslint-disable-next-line global-require
359361
const CacheEngine = require('./Webpack5Cache').default;
360362

361-
compiler.hooks.compilation.tap(pluginName, (compilation) => {
363+
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
362364
// eslint-disable-next-line global-require
363365
const Compilation = require('webpack/lib/Compilation');
364366

test/CompressionPlugin.test.js

+77-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44
import webpack from 'webpack';
55
import findCacheDir from 'find-cache-dir';
66
import cacache from 'cacache';
7+
import WorkboxPlugin from 'workbox-webpack-plugin';
78

89
import Webpack4Cache from '../src/Webpack4Cache';
910
import CompressionPlugin from '../src/index';
@@ -16,7 +17,6 @@ import {
1617
getCompiler,
1718
getErrors,
1819
getWarnings,
19-
readAsset,
2020
removeCache,
2121
} from './helpers/index';
2222

@@ -37,8 +37,6 @@ describe('CompressionPlugin', () => {
3737
});
3838

3939
it('should work', async () => {
40-
expect.assertions(6);
41-
4240
const compiler = getCompiler(
4341
'./entry.js',
4442
{},
@@ -54,23 +52,8 @@ describe('CompressionPlugin', () => {
5452
new CompressionPlugin().apply(compiler);
5553

5654
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-
}
7255

73-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
56+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
7457
expect(getWarnings(stats)).toMatchSnapshot('warnings');
7558
expect(getErrors(stats)).toMatchSnapshot('errors');
7659
});
@@ -94,7 +77,7 @@ describe('CompressionPlugin', () => {
9477

9578
const stats = await compile(compiler);
9679

97-
expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
80+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
9881
expect(getWarnings(stats)).toMatchSnapshot('warnings');
9982
expect(getErrors(stats)).toMatchSnapshot('errors');
10083
});
@@ -140,7 +123,7 @@ describe('CompressionPlugin', () => {
140123
const stats = await compile(compiler);
141124

142125
expect(gzipSpy).toHaveBeenCalledTimes(5);
143-
expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
126+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
144127
expect(getWarnings(stats)).toMatchSnapshot('warnings');
145128
expect(getErrors(stats)).toMatchSnapshot('errors');
146129

@@ -185,7 +168,7 @@ describe('CompressionPlugin', () => {
185168

186169
const stats = await compile(compiler);
187170

188-
expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
171+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
189172
expect(getWarnings(stats)).toMatchSnapshot('warnings');
190173
expect(getErrors(stats)).toMatchSnapshot('errors');
191174
});
@@ -213,7 +196,7 @@ describe('CompressionPlugin', () => {
213196
expect(printedCompressed ? printedCompressed.length : 0).toBe(
214197
getCompiler.isWebpack4() ? 0 : 3
215198
);
216-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
199+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
217200
expect(getWarnings(stats)).toMatchSnapshot('warnings');
218201
expect(getErrors(stats)).toMatchSnapshot('errors');
219202
});
@@ -240,7 +223,7 @@ describe('CompressionPlugin', () => {
240223
expect(info.immutable).toBe(true);
241224
}
242225

243-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
226+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
244227
expect(getWarnings(stats)).toMatchSnapshot('warnings');
245228
expect(getErrors(stats)).toMatchSnapshot('errors');
246229
});
@@ -268,7 +251,7 @@ describe('CompressionPlugin', () => {
268251
expect(stats.compilation.emittedAssets.size).toBe(7);
269252
}
270253

271-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
254+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
272255
expect(getWarnings(stats)).toMatchSnapshot('warnings');
273256
expect(getErrors(stats)).toMatchSnapshot('errors');
274257

@@ -285,7 +268,9 @@ describe('CompressionPlugin', () => {
285268
expect(newStats.compilation.emittedAssets.size).toBe(0);
286269
}
287270

288-
expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
271+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
272+
'assets'
273+
);
289274
expect(getWarnings(newStats)).toMatchSnapshot('errors');
290275
expect(getErrors(newStats)).toMatchSnapshot('warnings');
291276

@@ -329,7 +314,7 @@ describe('CompressionPlugin', () => {
329314
expect(stats.compilation.emittedAssets.size).toBe(7);
330315
}
331316

332-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
317+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
333318
expect(getWarnings(stats)).toMatchSnapshot('warnings');
334319
expect(getErrors(stats)).toMatchSnapshot('errors');
335320

@@ -346,7 +331,9 @@ describe('CompressionPlugin', () => {
346331
expect(newStats.compilation.emittedAssets.size).toBe(0);
347332
}
348333

349-
expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
334+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
335+
'assets'
336+
);
350337
expect(getWarnings(newStats)).toMatchSnapshot('errors');
351338
expect(getErrors(newStats)).toMatchSnapshot('warnings');
352339

@@ -390,7 +377,7 @@ describe('CompressionPlugin', () => {
390377
expect(stats.compilation.emittedAssets.size).toBe(7);
391378
}
392379

393-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
380+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
394381
expect(getWarnings(stats)).toMatchSnapshot('warnings');
395382
expect(getErrors(stats)).toMatchSnapshot('errors');
396383

@@ -409,7 +396,9 @@ describe('CompressionPlugin', () => {
409396
expect(newStats.compilation.emittedAssets.size).toBe(2);
410397
}
411398

412-
expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
399+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
400+
'assets'
401+
);
413402
expect(getWarnings(newStats)).toMatchSnapshot('errors');
414403
expect(getErrors(newStats)).toMatchSnapshot('warnings');
415404

@@ -467,7 +456,7 @@ describe('CompressionPlugin', () => {
467456
expect(stats.compilation.emittedAssets.size).toBe(14);
468457
}
469458

470-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
459+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
471460
expect(getWarnings(stats)).toMatchSnapshot('warnings');
472461
expect(getErrors(stats)).toMatchSnapshot('errors');
473462

@@ -484,7 +473,9 @@ describe('CompressionPlugin', () => {
484473
expect(newStats.compilation.emittedAssets.size).toBe(0);
485474
}
486475

487-
expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
476+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
477+
'assets'
478+
);
488479
expect(getWarnings(newStats)).toMatchSnapshot('errors');
489480
expect(getErrors(newStats)).toMatchSnapshot('warnings');
490481

@@ -526,7 +517,7 @@ describe('CompressionPlugin', () => {
526517
expect(stats.compilation.emittedAssets.size).toBe(7);
527518
}
528519

529-
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
520+
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
530521
expect(getWarnings(stats)).toMatchSnapshot('warnings');
531522
expect(getErrors(stats)).toMatchSnapshot('errors');
532523

@@ -543,11 +534,63 @@ describe('CompressionPlugin', () => {
543534
expect(newStats.compilation.emittedAssets.size).toBe(7);
544535
}
545536

546-
expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
537+
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
538+
'assets'
539+
);
547540
expect(getWarnings(newStats)).toMatchSnapshot('errors');
548541
expect(getErrors(newStats)).toMatchSnapshot('warnings');
549542

550543
resolve();
551544
});
552545
});
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: [/\.(gz|br)$/],
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+
});
553596
});

0 commit comments

Comments
 (0)