hard-source-webpack-plugin icon indicating copy to clipboard operation
hard-source-webpack-plugin copied to clipboard

rebuild from cache changes contenthash

Open dreyks opened this issue 5 years ago • 2 comments

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

  1. run yarn webpack - output: main-2f138c469b6d1cc42329.js 3.8 KiB 0 [emitted] main
  2. 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

dreyks avatar Sep 29 '18 21:09 dreyks

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)
}

dreyks avatar Sep 30 '18 10:09 dreyks

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.

catskull avatar Jan 10 '19 01:01 catskull