Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
fix(index): skip not changed chunk files in watch mode (fix webpack-c…
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov committed Oct 11, 2019
1 parent af883d8 commit 6af2b3e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CompressionPlugin {
constructor(options = {}) {
validateOptions(schema, options, 'Compression Plugin');

this.chunkVersions = {};

const {
test,
include,
Expand Down Expand Up @@ -85,12 +87,27 @@ class CompressionPlugin {
? findCacheDir({ name: 'compression-webpack-plugin' }) ||
os.tmpdir()
: cache;

const skipFiles = {};
for (const chunk of compilation.chunks) {
const oldVersion = this.chunkVersions[chunk.name];
this.chunkVersions[chunk.name] = chunk.hash;
if (chunk.hash === oldVersion) {
for (const file of chunk.files) {
skipFiles[file] = true;
}
}
}

const { assets } = compilation;

// eslint-disable-next-line consistent-return
async.forEach(
Object.keys(assets),
(file, cb) => {
if (skipFiles[file]) {
return cb();
}
if (!ModuleFilenameHelpers.matchObject(this.options, file)) {
return cb();
}
Expand Down
65 changes: 65 additions & 0 deletions test/__snapshots__/cache-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ Array [
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`false\` value ({Boolean}): chunks 1`] = `
Array [
Object {
"chunkFile": "0.async.dbe59b7ea0e1a63e45d4.js",
"chunkHash": "dbe59b7ea0e1a63e45d4b03b7cdf344f",
},
Object {
"chunkFile": "js.b329671af0217355535e.js",
"chunkHash": "b329671af0217355535e727af91e2bff",
},
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`false\` value ({Boolean}): errors 1`] = `Array []`;

exports[`when applied with \`cache\` option matches snapshot for \`false\` value ({Boolean}): warnings 1`] = `Array []`;
Expand Down Expand Up @@ -136,6 +149,32 @@ Array [
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`other-cache-directory\` value ({String}): chunks 1`] = `
Array [
Object {
"chunkFile": "0.async.dbe59b7ea0e1a63e45d4.js",
"chunkHash": "dbe59b7ea0e1a63e45d4b03b7cdf344f",
},
Object {
"chunkFile": "js.b329671af0217355535e.js",
"chunkHash": "b329671af0217355535e727af91e2bff",
},
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`other-cache-directory\` value ({String}): chunks 2`] = `
Array [
Object {
"chunkFile": "0.async.dbe59b7ea0e1a63e45d4.js",
"chunkHash": "dbe59b7ea0e1a63e45d4b03b7cdf344f",
},
Object {
"chunkFile": "js.b329671af0217355535e.js",
"chunkHash": "b329671af0217355535e727af91e2bff",
},
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`other-cache-directory\` value ({String}): errors 1`] = `Array []`;

exports[`when applied with \`cache\` option matches snapshot for \`other-cache-directory\` value ({String}): errors 2`] = `Array []`;
Expand Down Expand Up @@ -246,6 +285,32 @@ Array [
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`true\` value ({Boolean}): chunks 1`] = `
Array [
Object {
"chunkFile": "0.async.dbe59b7ea0e1a63e45d4.js",
"chunkHash": "dbe59b7ea0e1a63e45d4b03b7cdf344f",
},
Object {
"chunkFile": "js.b329671af0217355535e.js",
"chunkHash": "b329671af0217355535e727af91e2bff",
},
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`true\` value ({Boolean}): chunks 2`] = `
Array [
Object {
"chunkFile": "0.async.dbe59b7ea0e1a63e45d4.js",
"chunkHash": "dbe59b7ea0e1a63e45d4b03b7cdf344f",
},
Object {
"chunkFile": "js.b329671af0217355535e.js",
"chunkHash": "b329671af0217355535e727af91e2bff",
},
]
`;

exports[`when applied with \`cache\` option matches snapshot for \`true\` value ({Boolean}): errors 1`] = `Array []`;

exports[`when applied with \`cache\` option matches snapshot for \`true\` value ({Boolean}): errors 2`] = `Array []`;
Expand Down
18 changes: 16 additions & 2 deletions test/cache-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
compile,
cleanErrorStack,
getAssetsInfo,
getChunkInfo,
} from './helpers';

const cacheDir = findCacheDir({ name: 'compression-webpack-plugin' });
Expand Down Expand Up @@ -48,6 +49,7 @@ describe('when applied with `cache` option', () => {
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot('assets');
expect(getChunkInfo(stats.compilation.chunks)).toMatchSnapshot('chunks');

// Cache disabled so we don't run `get` or `put`
expect(cacache.get.mock.calls.length).toBe(0);
Expand Down Expand Up @@ -76,6 +78,7 @@ describe('when applied with `cache` option', () => {
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot('assets');
expect(getChunkInfo(stats.compilation.chunks)).toMatchSnapshot('chunks');

const countAssets = Object.keys(stats.compilation.assets).length;

Expand Down Expand Up @@ -121,12 +124,17 @@ describe('when applied with `cache` option', () => {
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot(
'assets'
);
expect(getChunkInfo(stats.compilation.chunks)).toMatchSnapshot(
'chunks'
);

const newCountAssets = Object.keys(newStats.compilation.assets)
.length;

// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2);
expect(cacache.get.mock.calls.length).toBe(
(newCountAssets - 2) / 2
);
expect(cacache.put.mock.calls.length).toBe(0);
})
);
Expand All @@ -146,6 +154,7 @@ describe('when applied with `cache` option', () => {
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot('assets');
expect(getChunkInfo(stats.compilation.chunks)).toMatchSnapshot('chunks');

const countAssets = Object.keys(stats.compilation.assets).length;

Expand Down Expand Up @@ -191,12 +200,17 @@ describe('when applied with `cache` option', () => {
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot(
'assets'
);
expect(getChunkInfo(stats.compilation.chunks)).toMatchSnapshot(
'chunks'
);

const newCountAssets = Object.keys(newStats.compilation.assets)
.length;

// Now we have cached files so we get their and don't put
expect(cacache.get.mock.calls.length).toBe(newCountAssets / 2);
expect(cacache.get.mock.calls.length).toBe(
(newCountAssets - 2) / 2
);
expect(cacache.put.mock.calls.length).toBe(0);
})
);
Expand Down
9 changes: 9 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ export function getAssetsInfo(assets, size = true) {
size ? assets[assetName].size() : 'size was skipped by test',
]);
}

export function getChunkInfo(chunks) {
return chunks
.sort((c, c2) => c.files[0] - c2.files[0])
.map((chunk) => ({
chunkFile: chunk.files[0],
chunkHash: chunk.hash,
}));
}

0 comments on commit 6af2b3e

Please sign in to comment.