-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Dependency extraction: improve calculation of contentHash #40930
Conversation
Size Change: -1 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
const contentHash = createHash( 'sha512' ) | ||
.update( assetString ) | ||
.digest( 'hex' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be curious to learn whether we could also refactor this usage to let webpack control it:
const contentHash = createHash( 'sha512' ) | |
.update( assetString ) | |
.digest( 'hex' ); | |
const contentHash = createHash( compilation.outputOptions.hashFunction ) | |
.update( assetString ) | |
.digest( compilation.outputOptions.hashDigest ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I pushed a commit that does that with the .asset.php
file content hash, too 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code changes look great. I will run some tests tomorrow with Node.js v18 and a sample Create a Block project to ensure it still works correctly 👍
I can confirm that it works with Node 18 🎉 I can confirm the same I can also confirm that the content hash generated by webpack is the same as the saved as Unrelated to this PR: I used the following config: output: {
filename: '[name].[contenthash].js',
path: resolve( process.cwd(), 'build' ),
}, By the way, the asset file should probably use the same It's also longer than the one used with the JS file. |
Future considerationsI'm testing with the default project scaffolde with The same applies when importing gutenberg/packages/scripts/config/webpack.config.js Lines 104 to 123 in 326e178
However, I was able to resolve that part by changing the way files are collected for hashing: const hash = createHash( hashFunction );
- for ( const filename of entrypoint.getFiles().sort() ) {
+ for ( const filename of Array.from( entrypointChunk.files ).sort() ) {
const asset = compilation.getAsset( filename );
hash.update( asset.source.buffer() );
} |
Thanks for your observations! By default, webpack outputs hashes that are 20 chars long, which is the default value of the The finding that when importing a CSS file, the hashes no longer match, is also a very good one. I'll work on that. |
It's something you can address in another patch. I'm happy with the improvements applied so far, too 😄 |
Follow-up to #34969 that implements suggestions from https://github.com/WordPress/gutenberg/pull/34969/files#r867737688:
createHash
exported by webpack rather than Node'scrypto
moduleRawSource
prefixesThe resulting hash is identical to what webpack itself would compute for filename like
[name].[contenthash].js
, and it's also identical to simply hashing the file contents with, e.g.,