How webpack cache works #1111
jerrykingxyz
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Explain how webpack use cache to improve build speed, and how to implement cache
NOTE: This page is written base on webpack v5.74.0
Glossary
Cache
Global
Global cache is a core part of webpack, it exposes basic storage api to hide low-level complex storage logic. all compiler share global cache.
Local
Local cache is created by functional class, it is a local variable and is stored in memory.
Snapshot
Snapshot is lifetime information label. webpack checks the difference between before and after snapshots to determine whether the cache is valid. We can config policy as
timestamps
,content hash
ortimestamps + content hash
.PS. Snapshot not storage the cached data, it only have lifetime info. (Cache Data = Origin Data + Snapshot)
Unsafe Cache
Webpack provides resolve.unsafeCache and module.unsafeCache to configure cache strength.
PS.
resolve.unsafeCache
used in enhanced-resolve,module.unsafeCache
used inprocessDependency
.Guide-level explanation
Configuration
We can use the
cache
field to configure cache in webpack, see this documentation for more subfieldsWe can use the
snapshot
field to configure snapshot strategy in webpack, see this documentation for more subfieldsReference-level explanation
Global Cache
Webpack define a basic cache module that provides these hooks to unify the behavior of different caches.
get(
identifier
,etag
,gotHandlers: Array<(result, callback) => void>
) ->result
Get data from cache. the
gotHandlers
is a post-process function array, which handles the final result. Memory cache usegotHandlers
to store the final result.store(
identifier
,etag
,data
)Save data to cache
storeBuildDependencies(
dependencies
)Save build dependencies to cache
beginIdle()
Run hooks when the build task begin idle
endIdle()
Run hooks when the build task end idle
shutdown()
Run hooks when need clear the cache
All of those hooks are provide a same name function for easy calling. Webpack has three preset cache:
Use memory to cache intermediate results for build.
Use memory to cache intermediate results for build, but clear cache when out of lifespan (lifespan config by
cache.maxGenerations
)Use file system to cache intermediate results for build. this cache will generate a queue of tasks that write cache to fs when build is idle.
Snapshot
The snapshot is implemented in FileSystemInfo.js.
Use cache
Resolve module
Webpack read module data and snapshot from cache and skips module resolve by checking if the snapshot is valid.
Build module
Webpack use
module.needBuild
to check whether current need build. webpack will check module cacheable & snapshot in themodule.needBuild
.Process dependency
Use
module.unsafe
to config module dependencies need to skip resolve.Code generate
Skip code generation by filtering cached code by module hash and identifier.
Chunk assets
Use cached chunk assets with asset identifier.
Emit assets
Use cached assets with asset identifier.
Beta Was this translation helpful? Give feedback.
All reactions