Skip to content

Commit

Permalink
fix: deterministic for contenthash (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 18, 2021
1 parent 6009bd2 commit 2ff8e59
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions src/index.js
Expand Up @@ -280,6 +280,8 @@ class MiniCssExtractPlugin {
baseDataPath: 'options',
});

this._sortedModulesCache = new WeakMap();

this.options = Object.assign(
{ filename: DEFAULT_FILENAME, ignoreOrder: false },
options
Expand Down Expand Up @@ -535,7 +537,15 @@ class MiniCssExtractPlugin {
? Array.from(this.getChunkModules(chunk, chunkGraph)).filter(
(module) => module.type === MODULE_TYPE
)
: chunkGraph.getChunkModulesIterableBySourceType(chunk, MODULE_TYPE);
: this.sortModules(
compilation,
chunk,
chunkGraph.getChunkModulesIterableBySourceType(
chunk,
MODULE_TYPE
),
compilation.runtimeTemplate.requestShortener
);

if (modules) {
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
Expand Down Expand Up @@ -1076,8 +1086,14 @@ class MiniCssExtractPlugin {
return obj;
}

renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
let usedModules;
sortModules(compilation, chunk, modules, requestShortener) {
let usedModules = this._sortedModulesCache.get(chunk);

if (usedModules || !modules) {
return usedModules;
}

modules = [...modules];

const [chunkGroup] = chunk.groupsIterable;
const moduleIndexFunctionName =
Expand Down Expand Up @@ -1215,6 +1231,19 @@ class MiniCssExtractPlugin {
usedModules = modules;
}

this._sortedModulesCache.set(chunk, usedModules);

return usedModules;
}

renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
const usedModules = this.sortModules(
compilation,
chunk,
modules,
requestShortener
);

// TODO remove after drop webpack v4
const { ConcatSource, SourceMapSource, RawSource } = compiler.webpack
? compiler.webpack.sources
Expand Down
32 changes: 24 additions & 8 deletions test/TestCache.test.js
Expand Up @@ -167,7 +167,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler1.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -207,7 +209,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler2.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -274,7 +278,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler1.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -316,7 +322,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler2.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -385,7 +393,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler1.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -428,7 +438,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler2.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -497,7 +509,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler1.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down Expand Up @@ -540,7 +554,9 @@ describe('TestCache', () => {
expect(stats.compilation.errors).toHaveLength(0);

compiler2.close(() => {
resolve();
process.nextTick(() => {
resolve();
});
});
});
});
Expand Down
@@ -0,0 +1,8 @@
.a {
width: 100px;
}

.b {
width: 100px;
}

@@ -0,0 +1,8 @@
.b {
width: 100px;
}

.a {
width: 100px;
}

7 changes: 7 additions & 0 deletions test/cases/content-entries-with-same-import/index.js
@@ -0,0 +1,7 @@
const app1 = import('./one');
const app2 = import('./two');

// eslint-disable-next-line no-console
console.log(app1);
// eslint-disable-next-line no-console
console.log(app2);
4 changes: 4 additions & 0 deletions test/cases/content-entries-with-same-import/one.js
@@ -0,0 +1,4 @@
import './style1.css';
import './style2.css';

export default 'one';
3 changes: 3 additions & 0 deletions test/cases/content-entries-with-same-import/style1.css
@@ -0,0 +1,3 @@
.a {
width: 100px;
}
3 changes: 3 additions & 0 deletions test/cases/content-entries-with-same-import/style2.css
@@ -0,0 +1,3 @@
.b {
width: 100px;
}
3 changes: 3 additions & 0 deletions test/cases/content-entries-with-same-import/test.filter.js
@@ -0,0 +1,3 @@
const webpack = require('webpack');

module.exports = () => webpack.version[0] !== '4';
4 changes: 4 additions & 0 deletions test/cases/content-entries-with-same-import/two.js
@@ -0,0 +1,4 @@
import './style2.css';
import './style1.css';

export default 'two';
21 changes: 21 additions & 0 deletions test/cases/content-entries-with-same-import/webpack.config.js
@@ -0,0 +1,21 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
output: {
filename: '[name].[contenthash].js',
},
plugins: [
new Self({
filename: '[name].[contenthash].css',
}),
],
};

0 comments on commit 2ff8e59

Please sign in to comment.