简体   繁体   English

如何从缓存中排除特定文件

[英]How to exclude specific files from caching

I'm using config-overrides.js with string-replace-loader in order to change the content of a file before being compiled by webpack.我将config-overrides.jsstring-replace-loader一起使用,以便在 webpack 编译之前更改文件的内容。

But because of the caching system of webpack ( cache option ) the loader is not being executed, if I set cache = false everything works just fine (exactly like the production build).但是由于 webpack (缓存选项)的缓存系统,加载程序没有被执行,如果我设置cache = false一切正常(就像生产构建一样)。

I've spent several hours trying to understand how to exclude a file from caching, but whatever I've found does not work as expected and I'm kinda out of ideas.我花了几个小时试图了解如何从缓存中排除文件,但无论我发现什么都没有按预期工作,而且我有点没有想法。

Is there any way to achieve this without completely disabling the caching system ?有没有办法在不完全禁用缓存系统的情况下实现这一点?

[EDIT] I know the question asks about files , but my problem was related to the index.js file which imports a component conditionally based on a specific value inside an external config file. [编辑]我知道这个问题是关于files的,但我的问题与index.js文件有关,该文件根据外部配置文件中的特定值有条件地导入组件。

After some more couple of hours of investigating I've found what I was looking for.经过几个小时的调查,我找到了我想要的东西。

Here is the official documentation regarding this exactly use case. 是关于这个确切用例的官方文档。

Basically what we can do to invalidate the cache is to use the cache object as below:基本上我们可以使缓存失效的方法是使用cache object,如下所示:

cache = {
      type: 'filesystem',
      version: `${any external variable/value which may change in the future}`, // Keep the `${}`
};

cache.version is a string. cache.version是一个字符串。 Passing a different string will invalidate the persistent cache.传递不同的字符串将使持久缓存无效。

I'm using it like this and it's working great so far.我正在像这样使用它,到目前为止它工作得很好。

 const invalidateCacheValues = [ config.my_value_0, config.my_value_1 ]; webpackConfig['cache'] = { type: 'filesystem', version: `${invalidateCacheValues.join('|')}`, };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM