从块Webpack 4中排除某些模块

Mig*_*uel 8 javascript reactjs webpack

我该如何指定我不希望Webpack 4中的模块成为块,让我们假设我不希望供应商文件中出现破折号(无论后果如何),我该怎么办?

这是实际配置:

splitChunks: {
  name: 'vendors',
  maxAsyncRequests: 1,
  maxInitialRequests: 2,
  chunks: 'initial',
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*tti 6

您可以使用负前瞻来做到这一点:

test: /[\\/]node_modules[\\/]((?!(lodash)).*)[\\/]/
Run Code Online (Sandbox Code Playgroud)


gee*_*dew 5

test也可以采取的方法。这提供了很大的灵活性。例如..

vendor: {
    test(mod/* , chunk */) {


     // Only node_modules are needed
     if (!mod.context.includes('node_modules')) {
       return false;
     }
     // But not node modules that contain these key words in the path
     if ([ 'lodash', 'react'].some(str => mod.context.includes(str))) {
       return false;
     }
     return true;
   },
   name: 'vendor',
   chunks: 'all',
   reuseExistingChunk: true
 },
Run Code Online (Sandbox Code Playgroud)


A. *_*sky 1

用于将项目的所有依赖项加载/node_modules到名为的块中的配置vendors如下所示:

optimization: {
    splitChunks: {
        cacheGroups: {
            commons: { test: /[\\/]node_modules[\\/]/, name: "vendors", chunks: "all" }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,对于您的用例,您可以修改 RegExp 以排除您想要的任何模块。当然,缺点是您必须与正则表达式纠缠在一起,并且特定测试条件的确切格式超出了我的专业知识。我知道有关的文档optimization.splitChunks仍然相当稀疏,所以我希望这至少可以帮助您指明正确的方向。