首页 > 解决方案 > where does webpack cache its work for rebuilding?

问题描述

This page shows a lot of compilation modes for webpack, and some of them rebuild faster than they build.

enter image description here

I am trying to measure the speed of different build-tools, but I can't figure out if I am building or rebuilding. I mean.. what files do I have to clear to make sure that I am rebuilding? Just the target directory? Or is there another cache somewhere?

For example is the second build here a build or a rebuild the second time?

node_modules/.bin/webpack --output-path ~/target
rm -r target
node_modules/.bin/webpack --output-path ~/target

if that rebuilds, what to I have to clear to make it build again?

and does the second one here build or rebuild?

node_modules/.bin/webpack --output-path ~/target
node_modules/.bin/webpack --output-path ~/target

And if that is another build, what do I have to do to rebuild?

(webpack 4)

标签: webpackbabeljs

解决方案


TL;DR: “重建”仅在watchmodedevelopmentmode期间发生。

网页包

Webpack 似乎没有缓存到文件中,而只是在内存中。

缓存

缓存生成的 webpack 模块和块以提高构建速度。在 watch 模式和 webpack 设置为开发模式时,默认情况下会自动启用缓存。

Webpack 文档 | 缓存选项

由于默认情况下它仅在开发和监视模式下启用,因此您的每个命令都应该是全新的构建。您可以确定关闭此cache选项,也可以专门针对production

node_modules/.bin/webpack --output-path ~/target --mode=production

我在查看 webpack 5 中的新更改后立即遇到了这个问题,其中之一是允许缓存到本地文件

cache:对象已删除:不再可能设置为内存缓存对象

cache.type补充:现在可以在“内存”和“文件系统”之间进行选择

我认为这意味着当前在 webpack 4 中不可能在文件系统中缓存。


编辑:你没有在问题中提到它,但我刚才看到你babel在赏金的推理中提到了。

通天塔

如果您使用babel-loader,则默认情况下有一个cacheDirectory选项false。但是,如果启用,默认目录位于node_modules/.cache/babel-loader.

这是文档中关于它的片段,强调我的:

cacheDirectory:默认为假。设置后,给定目录将用于缓存加载程序的结果。未来的 webpack 构建将尝试从缓存中读取,以避免在每次运行时都需要运行可能代价高昂的 Babel 重新编译过程。如果在 options 中将该值设置为 true ({cacheDirectory: true}),则加载程序将使用默认缓存目录,如果在任何根目录中都找不到文件夹,node_modules/.cache/babel-loader则将回退到默认 OS 临时文件目录。node_modules

babel-loader 选项


推荐阅读