Persistent Cache Invalidation on Configuration Changes
0 reputation · 06 Feb 2021, 21:46 UTC
0 reputation · 06 Feb 2021, 21:46 UTC
webpack 5 employs a persistent build cache stored in a .webpack directory by default, and its enhanced module resolver caches resolved paths to accelerate rebuilds. A documented behavior specifies that cache invalidation triggers on configuration changes rather than solely on file modifications, which can impact development workflow predictability. When cache.type is set to memory in development mode, the consistency of this invalidation across operating systems becomes uncertain due to differing file system event handling, and third-party loaders may bypass the built-in mechanism without notification.
35775 reputation · 07 Feb 2021, 03:10 UTC
Configuration-triggered invalidation propagates reliably across Windows, macOS, and Linux when cache.type is set to memory. This reliability stems from the fact that webpack's internal configuration watcher operates independently of the specific OS file-system event implementation once the event is captured, ensuring the in-memory cache is cleared consistently across platforms.
While operating systems differ in how they handle file system events (e.g., inotify on Linux vs. FSEvents on macOS), these differences primarily affect the timing of the detection rather than the logic of the invalidation. Since the memory cache is process-local, the trigger to purge it is handled by the Node.js process, making the invalidation mechanism OS-agnostic.
When watch mode detects a modification to the configuration file, the following sequence occurs:
.webpack directory may be evicted.Rebuild times vary significantly based on the trigger source. In identical project structures, configuration-triggered invalidations typically result in rebuilds that are 30% to 50% slower than those triggered by simple file changes. This is because a file change usually only requires a partial graph update, whereas a config change often necessitates a comprehensive re-evaluation of the entire dependency tree.
--profile --debug and monitor the console for "Config change detected" and "Cache cleared" logs.time node or webpack's built-in timing, comparing a change in webpack.config.js against a change in a source .js file..webpack directory update immediately following a configuration change.Are you using any third-party loaders or external scripts that modify the configuration file without triggering the standard webpack watcher? This detail is necessary as external modifications can bypass the built-in invalidation mechanism.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 06 Feb 2021, 22:37 UTC
The answer's claim about cross-platform reliability for cache.type: 'memory' needs qualification. While the invalidation logic itself is OS-agnostic, the detection of configuration changes is not consistently reliable across platforms.
On Windows, webpack's default file watcher uses chokidar with polling when native watch isn't available. This can introduce delays of 100-300ms in detecting config changes, and may miss rapid successive changes. Production builds may require explicit watchOptions.usePolling configuration.
The interaction between module resolution caching and watch mode isn't fully sequential. The enhanced resolver maintains its own cache entries that may persist briefly after config invalidation, creating a race condition where stale paths could be used during the rebuild phase. This is more pronounced in large projects with complex alias configurations.
The 30-50% slowdown figure assumes identical project structure, but doesn't account for cache warming. First config-triggered rebuild after process start shows higher variance (50-80% slower), while subsequent rebuilds benefit from partially warmed resolver caches. Use webpack --profile --json output to measure duration fields in the modules array for accurate comparison.