Skip to content

Commit

Permalink
fix: compatibility with child compilations
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Nov 12, 2020
1 parent 7eaf1ff commit 5e3bb95
Show file tree
Hide file tree
Showing 38 changed files with 4,791 additions and 115 deletions.
458 changes: 456 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^5.4.0"
"webpack": "^5.4.0",
"workbox-webpack-plugin": "^6.0.0-alpha.3"
},
"keywords": [
"webpack"
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class CompressionPlugin {
}

relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
} else if (this.options.algorithm === 'gzip') {
relatedName = 'gzipped';
} else {
relatedName = `${this.options.algorithm}ed`;
}
Expand Down Expand Up @@ -358,7 +360,7 @@ class CompressionPlugin {
// eslint-disable-next-line global-require
const CacheEngine = require('./Webpack5Cache').default;

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

Expand Down
111 changes: 77 additions & 34 deletions test/CompressionPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import webpack from 'webpack';
import findCacheDir from 'find-cache-dir';
import cacache from 'cacache';
import WorkboxPlugin from 'workbox-webpack-plugin';

import Webpack4Cache from '../src/Webpack4Cache';
import CompressionPlugin from '../src/index';
Expand All @@ -16,7 +17,6 @@ import {
getCompiler,
getErrors,
getWarnings,
readAsset,
removeCache,
} from './helpers/index';

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

it('should work', async () => {
expect.assertions(6);

const compiler = getCompiler(
'./entry.js',
{},
Expand All @@ -54,23 +52,8 @@ describe('CompressionPlugin', () => {
new CompressionPlugin().apply(compiler);

const stats = await compile(compiler);
const { assets, assetsInfo } = stats.compilation;

for (const assetName of Object.keys(assets)) {
const info = assetsInfo.get(assetName);

if (!info.related) {
// eslint-disable-next-line no-continue
continue;
}

const originalBuffer = readAsset(assetName, compiler, stats);
const gzipedBuffer = readAsset(info.related.gziped, compiler, stats);

expect(zlib.gunzipSync(gzipedBuffer).equals(originalBuffer)).toBe(true);
}

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
Expand All @@ -94,7 +77,7 @@ describe('CompressionPlugin', () => {

const stats = await compile(compiler);

expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
Expand Down Expand Up @@ -140,7 +123,7 @@ describe('CompressionPlugin', () => {
const stats = await compile(compiler);

expect(gzipSpy).toHaveBeenCalledTimes(5);
expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

Expand Down Expand Up @@ -185,7 +168,7 @@ describe('CompressionPlugin', () => {

const stats = await compile(compiler);

expect(getAssetsNameAndSize(stats, true)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
Expand Down Expand Up @@ -213,7 +196,7 @@ describe('CompressionPlugin', () => {
expect(printedCompressed ? printedCompressed.length : 0).toBe(
getCompiler.isWebpack4() ? 0 : 3
);
expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
Expand All @@ -240,7 +223,7 @@ describe('CompressionPlugin', () => {
expect(info.immutable).toBe(true);
}

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
Expand Down Expand Up @@ -268,7 +251,7 @@ describe('CompressionPlugin', () => {
expect(stats.compilation.emittedAssets.size).toBe(7);
}

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

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

expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
'assets'
);
expect(getWarnings(newStats)).toMatchSnapshot('errors');
expect(getErrors(newStats)).toMatchSnapshot('warnings');

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

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

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

expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
'assets'
);
expect(getWarnings(newStats)).toMatchSnapshot('errors');
expect(getErrors(newStats)).toMatchSnapshot('warnings');

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

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

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

expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
'assets'
);
expect(getWarnings(newStats)).toMatchSnapshot('errors');
expect(getErrors(newStats)).toMatchSnapshot('warnings');

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

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

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

expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
'assets'
);
expect(getWarnings(newStats)).toMatchSnapshot('errors');
expect(getErrors(newStats)).toMatchSnapshot('warnings');

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

expect(getAssetsNameAndSize(stats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');

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

expect(getAssetsNameAndSize(newStats)).toMatchSnapshot('assets');
expect(getAssetsNameAndSize(newStats, compiler)).toMatchSnapshot(
'assets'
);
expect(getWarnings(newStats)).toMatchSnapshot('errors');
expect(getErrors(newStats)).toMatchSnapshot('warnings');

resolve();
});
});

// TODO https://github.com/webpack-contrib/compression-webpack-plugin/issues/218
it.skip('should work with "workbox-webpack-plugin" plugin ("GenerateSW")', async () => {
const compiler = getCompiler(
'./entry.js',
{},
{
output: {
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
}
);

new WorkboxPlugin.GenerateSW().apply(compiler);
new CompressionPlugin().apply(compiler);

const stats = await compile(compiler);

expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

// TODO https://github.com/webpack-contrib/compression-webpack-plugin/issues/218
it.skip('should work with "workbox-webpack-plugin" plugin ("InjectManifest")', async () => {
const compiler = getCompiler(
'./entry.js',
{},
{
output: {
filename: '[name].js',
chunkFilename: '[id].[name].js',
},
}
);

new WorkboxPlugin.InjectManifest({
swSrc: path.resolve(__dirname, './fixtures/sw.js'),
swDest: 'sw.js',
exclude: [/\.(gz|br)$/],
}).apply(compiler);
new CompressionPlugin().apply(compiler);

const stats = await compile(compiler);

expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot('assets');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});
Loading

0 comments on commit 5e3bb95

Please sign in to comment.