WebPack 加载器配置:排除还是包含 node_modules?

WebPack Loader Config: To exclude or to include node_modules?

对于 WebPack 中的各种加载器,我应该包含还是排除 node_modules?

我在各种加载器(JS、TS、 CSS、SCSS、文件、url、原始等)

我不明白你为什么要或不包括它。显然它引入了代码并将其包含在输出构建中,我猜这只是加载程序是否处理它。我只遇到过一个节点模块,如果加载器处理它,它就无法工作,到目前为止 none 无论如何都无法工作。

除了一个包裹外,none 其他包裹似乎很在意它们是被包含还是被排除在外。它对 output/browser 有什么区别?

例如:

'use strict';

const path = require('path');

module.exports = (root) => {
  return {
    // BABEL LOADER
    // Reference: https://github.com/babel/babel-loader
    // Transpile .js files using babel-loader
    // Compiles ES6 and ES7 into ES5 code

    // Run on .js files
    test: /\.js$/,

    // Use the babel-loader
    use: [
      // Babel transpiler, see .babelrc for configuration
      {
        loader: 'babel-loader',
        options: {
          sourceMap: true, // Emit sourcemaps
          cacheDirectory: true // Cache compilation
        }
      }
    ],
    // Aside from one package, none of the others seem to care if they're included or excluded.
    include: [ path.resolve(root, 'client') ]
  };
};

这是我遵循的内容和原因。

我排除的所有 .js 个文件 node_modules

  • 一般 .js 加载程序链是 ESlint 然后 Babel
  • 不需要 Linting,因为你不能对 node_modules 的 Linting 结果做任何事情,除非你修复所有的 lint warnings/errors(不认为会有任何)和创建者的模块接受这些更改并发布它。
  • 大部分场景发布的代码都是ES5代码,不需要babel

如果您有任何与此不同的地方,您可以包含这些模块。

结果:我的构建时间增加了 20 倍,因为我的项目中使用了大约 500-600 个 npm 模块(包括所有依赖项)。它会穿越 1000-2000 .js 不需要的地方。

对于其他文件:我发现您可能没有为 node_modules 添加排除项,因为例如 CSS 本来是 require()在节点模块库中编辑它也必须捆绑。