hard-source-webpack-plugin
hard-source-webpack-plugin copied to clipboard
rebuild from cache changes contenthash
reproduction repo https://github.com/dreyks/hswp_fail/tree/contenthash
Expected Behavior
Rebuilding unchanged code from cache should result in exact same file and filename
Actual Behavior
Files are same but filenames containing [contenthash] are different.
Steps to Reproduce
https://github.com/dreyks/hswp_fail/tree/contenthash
- run
yarn webpack
- output:main-2f138c469b6d1cc42329.js 3.8 KiB 0 [emitted] main
- run
yarn webpack
again - output:main-80bb0d27ee34d918376b.js 3.8 KiB 0 [emitted] main
Additional info
- this only happens in 'production'
mode
- [chunkhash] has the same issue
- I've tried changing webpack versions and figured out that when running with
[email protected]
does not produce this issue
ok i think i'm getting closer
when HS freezes modules it calls _initBuildHash
which recalculates the module hash even if it was already calculated before.
moreover at this stage module.buildMeta is changed since module hash was calculated now containing providedExports
key, (i think webpack adds this information only in production build) so the recalculated hash is different from original. and when such module is read back from cache on the next run it has a new module hash which affects contenthash of the whole chunk
possible solution would be to check if module._buildHash
is present and not call _initBuildHash
if that is the case. or modify the _initBuildHash
itself not to recalculate the hash but that's on webpack
my current workaround is a monkeypatch
const NormalModule = require('webpack/lib/NormalModule')
const original = NormalModule.prototype._initBuildHash
NormalModule.prototype._initBuildHash = (compilation) => {
if (this._buildHash !== '')
return
original(compilation)
}
I think I'm also running into this. For me, I build and it generates the cache. Then I build again, and it builds from cache. Then I build again, and it says the last cache didn't finish saving.