Skip to content

fix: improve contenthash generation #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,21 @@ class MiniCssExtractPlugin {

compilation.hooks.contentHash.tap(pluginName, (chunk) => {
const { outputOptions } = compilation;
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
const {
hashSalt,
hashDigest,
hashDigestLength,
hashFunction,
} = outputOptions;
const hash = createHash(hashFunction);

if (hashSalt) {
hash.update(hashSalt);
}

hash.update(`${chunk.id} `);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are not part of the content of the css file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra without it, difference async chunks have same contenthash, but difference content, here example #454

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra @evilebottnawi any update on this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, CSS modules don't have id property set so the hash is going to be the same. Would it be better to add hash.update(m.index2) (since index2 is used to sort the modules before rendering content asset) below after m.updateHash(hash) ?

hash.update(chunk.ids ? chunk.ids.join(',') : '');

for (const m of chunk.modulesIterable) {
if (m.type === MODULE_TYPE) {
m.updateHash(hash);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.a {
width: 100px;
}

.b {
width: 100px;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.b {
width: 100px;
}

.a {
width: 100px;
}

5 changes: 5 additions & 0 deletions test/cases/content-async-entries-with-same-import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const app1 = import('./one');
const app2 = import('./two');

console.log(app1);
console.log(app2);
4 changes: 4 additions & 0 deletions test/cases/content-async-entries-with-same-import/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style1.css';
import './style2.css';

export default 'one';
3 changes: 3 additions & 0 deletions test/cases/content-async-entries-with-same-import/style1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a {
width: 100px;
}
3 changes: 3 additions & 0 deletions test/cases/content-async-entries-with-same-import/style2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.b {
width: 100px;
}
4 changes: 4 additions & 0 deletions test/cases/content-async-entries-with-same-import/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style2.css';
import './style1.css';

export default 'two';
Original file line number Diff line number Diff line change
@@ -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',
}),
],
};