Skip to content

Commit 2ff8e59

Browse files
fix: deterministic for contenthash (#702)
1 parent 6009bd2 commit 2ff8e59

File tree

12 files changed

+118
-12
lines changed

12 files changed

+118
-12
lines changed

package-lock.json

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

src/index.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ class MiniCssExtractPlugin {
280280
baseDataPath: 'options',
281281
});
282282

283+
this._sortedModulesCache = new WeakMap();
284+
283285
this.options = Object.assign(
284286
{ filename: DEFAULT_FILENAME, ignoreOrder: false },
285287
options
@@ -535,7 +537,15 @@ class MiniCssExtractPlugin {
535537
? Array.from(this.getChunkModules(chunk, chunkGraph)).filter(
536538
(module) => module.type === MODULE_TYPE
537539
)
538-
: chunkGraph.getChunkModulesIterableBySourceType(chunk, MODULE_TYPE);
540+
: this.sortModules(
541+
compilation,
542+
chunk,
543+
chunkGraph.getChunkModulesIterableBySourceType(
544+
chunk,
545+
MODULE_TYPE
546+
),
547+
compilation.runtimeTemplate.requestShortener
548+
);
539549

540550
if (modules) {
541551
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
@@ -1076,8 +1086,14 @@ class MiniCssExtractPlugin {
10761086
return obj;
10771087
}
10781088

1079-
renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
1080-
let usedModules;
1089+
sortModules(compilation, chunk, modules, requestShortener) {
1090+
let usedModules = this._sortedModulesCache.get(chunk);
1091+
1092+
if (usedModules || !modules) {
1093+
return usedModules;
1094+
}
1095+
1096+
modules = [...modules];
10811097

10821098
const [chunkGroup] = chunk.groupsIterable;
10831099
const moduleIndexFunctionName =
@@ -1215,6 +1231,19 @@ class MiniCssExtractPlugin {
12151231
usedModules = modules;
12161232
}
12171233

1234+
this._sortedModulesCache.set(chunk, usedModules);
1235+
1236+
return usedModules;
1237+
}
1238+
1239+
renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
1240+
const usedModules = this.sortModules(
1241+
compilation,
1242+
chunk,
1243+
modules,
1244+
requestShortener
1245+
);
1246+
12181247
// TODO remove after drop webpack v4
12191248
const { ConcatSource, SourceMapSource, RawSource } = compiler.webpack
12201249
? compiler.webpack.sources

test/TestCache.test.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ describe('TestCache', () => {
167167
expect(stats.compilation.errors).toHaveLength(0);
168168

169169
compiler1.close(() => {
170-
resolve();
170+
process.nextTick(() => {
171+
resolve();
172+
});
171173
});
172174
});
173175
});
@@ -207,7 +209,9 @@ describe('TestCache', () => {
207209
expect(stats.compilation.errors).toHaveLength(0);
208210

209211
compiler2.close(() => {
210-
resolve();
212+
process.nextTick(() => {
213+
resolve();
214+
});
211215
});
212216
});
213217
});
@@ -274,7 +278,9 @@ describe('TestCache', () => {
274278
expect(stats.compilation.errors).toHaveLength(0);
275279

276280
compiler1.close(() => {
277-
resolve();
281+
process.nextTick(() => {
282+
resolve();
283+
});
278284
});
279285
});
280286
});
@@ -316,7 +322,9 @@ describe('TestCache', () => {
316322
expect(stats.compilation.errors).toHaveLength(0);
317323

318324
compiler2.close(() => {
319-
resolve();
325+
process.nextTick(() => {
326+
resolve();
327+
});
320328
});
321329
});
322330
});
@@ -385,7 +393,9 @@ describe('TestCache', () => {
385393
expect(stats.compilation.errors).toHaveLength(0);
386394

387395
compiler1.close(() => {
388-
resolve();
396+
process.nextTick(() => {
397+
resolve();
398+
});
389399
});
390400
});
391401
});
@@ -428,7 +438,9 @@ describe('TestCache', () => {
428438
expect(stats.compilation.errors).toHaveLength(0);
429439

430440
compiler2.close(() => {
431-
resolve();
441+
process.nextTick(() => {
442+
resolve();
443+
});
432444
});
433445
});
434446
});
@@ -497,7 +509,9 @@ describe('TestCache', () => {
497509
expect(stats.compilation.errors).toHaveLength(0);
498510

499511
compiler1.close(() => {
500-
resolve();
512+
process.nextTick(() => {
513+
resolve();
514+
});
501515
});
502516
});
503517
});
@@ -540,7 +554,9 @@ describe('TestCache', () => {
540554
expect(stats.compilation.errors).toHaveLength(0);
541555

542556
compiler2.close(() => {
543-
resolve();
557+
process.nextTick(() => {
558+
resolve();
559+
});
544560
});
545561
});
546562
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.a {
2+
width: 100px;
3+
}
4+
5+
.b {
6+
width: 100px;
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.b {
2+
width: 100px;
3+
}
4+
5+
.a {
6+
width: 100px;
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const app1 = import('./one');
2+
const app2 = import('./two');
3+
4+
// eslint-disable-next-line no-console
5+
console.log(app1);
6+
// eslint-disable-next-line no-console
7+
console.log(app2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './style1.css';
2+
import './style2.css';
3+
4+
export default 'one';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
width: 100px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.b {
2+
width: 100px;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = () => webpack.version[0] !== '4';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './style2.css';
2+
import './style1.css';
3+
4+
export default 'two';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Self from '../../../src';
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [Self.loader, 'css-loader'],
10+
},
11+
],
12+
},
13+
output: {
14+
filename: '[name].[contenthash].js',
15+
},
16+
plugins: [
17+
new Self({
18+
filename: '[name].[contenthash].css',
19+
}),
20+
],
21+
};

0 commit comments

Comments
 (0)