首页 > 解决方案 > buildDependencies targets folder instead of file

问题描述

For caching (type: 'filesystem'), when i use

buildDependencies: {
  config: [__filename],
}

and rebuild after a minimal change, I get cache to be used:

[webpack.cache.PackFileCacheStrategy] restore cache container: 75.650361 ms
[webpack.cache.PackFileCacheStrategy] check build dependencies: 38.511852 ms
[webpack.cache.PackFileCacheStrategy] restore cache content metadata: 7.851668 ms

However, if i simply add one more file to buildDependencies, e.g. like this (same, if i use a new property):

buildDependencies: {
  config: [__filename, path.resolve(__dirname, 'tsconfig.json')],
}

I suddenly get a cache invalidation (although tsconfig.json didn't change, neither did webpack.config.js, i obviously rebuilt in between):

[webpack.cache.PackFileCacheStrategy] restore cache container: 79.482314 ms
[webpack.cache.PackFileCacheStrategy/webpack.FileSystemInfo] C:\some\nice\folder invalidated because hashes differ (somehash != someotherhash)
[webpack.cache.PackFileCacheStrategy] Restored pack from C:\some\nice\folder\cachefolder\cachename.pack, but build dependencies have changed.
[webpack.cache.PackFileCacheStrategy] check build dependencies: 593.564325 ms

Note that it mentions that a path ending with a folder (the project folder) invalidated, although i supposedly specified a file as dependency. The resulting build time is around twice as long.

What am i doing wrong here? Why does webpack invalidate the cache here, although none of the dependencies changed?

标签: javascriptnode.jswebpack

解决方案


到目前为止,Webpack 不支持 中的非代码文件buildDependencies,并且ts-loader似乎没有正确声明tsconfig.json为依赖项(通过加载程序的通道),因此首先需要这样做。一个后备然后使用整个文件夹。

幸运的是,这两个都应该在未来的版本中得到修复:webpack 将允许.json文件进入buildDependencies(如果我没记错的话v5.14.0,它现在已经可用),并ts-loader希望告诉 webpack tsconfig.json(在未来的某个版本中),所以它会工作即使没有手动设置依赖关系。一些相关的日志和错误也会发生变化,以提供更多信息。


推荐阅读