Plugin to replace webpack chunkhash with an md5 hash of the final file conent.
npm install webpack-plugin-hash-output --save-dev
yarn add webpack-plugin-hash-output --dev
There are other webpack plugins for hashing out there. But when they run, they don't "see" the final form of the code, because they run
before plugins like
webpack.optimize.UglifyJsPlugin. In other words, if you change
webpack.optimize.UglifyJsPlugin config, your
hashes won't change, creating potential conflicts with cached resources.
The main difference is that
webpack-plugin-hash-output runs in the last compilation step. So any change in webpack or any other plugin
that actually changes the output, will be "seen" by this plugin, and therefore that change will be reflected in the hash.
It will generate files like
<hash> is always a hash of the content (well, a subset of). Example:
$ md5 entry.32f1718dd08eccda2791.js MD5 (entry.32f1718dd08eccda2791.js) = 32f1718dd08eccda2791ff7ed466bd98
All other assets (common files, manifest files, HTML output...) will use the new md5 hash to reference the asset.
Requires webpack >=4
Just add this plugin as usual.
// webpack.config.jsvar HashOutput = ;moduleexports =// ...output://...filename: '[name].[chunkhash].js'plugins:options;
This plugin partially supports sourcemaps. When a chunk hash is recomputed, it will update the name of the chunk in the chunks's sourcemap, but it won't recompute the name of the hash file. This has the side effect that the name of the sourcemap will differ from the name of the chunk. Example:
// app.<oldhash>.js// ...code...//# sourceMappingURL=entry.<oldhash>.js.map
// app.<oldhash>.js.map// ..."file":"app.<oldhash>.js"// ...
// app.<newhash>.js// ...code...//# sourceMappingURL=entry.<oldhash>.js.map
// app.<oldhash>.js.map// ..."file":"app.<newhash>.js"// ...
We can't fully support sourcemaps (i.e. recomute sourcemap hash) because the circular reference: we can't compute sourcemap hash without computing the file hash first, and we can't compute the file hash without the sourcemap hash.
Note: Use Webpack's own hash output options to configure hash function and length.
boolean, defaults to
After webpack has compiled and generated all the assets, checks that the hash of the content is included in the file. If it is not, it will throw an error and the webpack process will fail.
regex, defaults to
When validating the output (see
options.validateOutput), only evaluate hashes for files matching this regexp.
Useful for skipping source maps or other output. Example:
moduleexports =entry:main: './src/app.js'output:filename: 'assets/[name].[chunkhash].js'plugins:filename: 'fragments/app.html'chunks: 'main'validateOutput: true// Check that md5(assets/main.<hash>.js) === <hash>, but doesn't check fragments/app.htmlvalidateOutputRegex: /^assets\/.*\.js$/;
Tests can be run by:
After running the tests, you might want to run
yarn run clean to clean up temp files generated by the tests.