【问题标题】:WebPack Loader Config: To exclude or to include node_modules?WebPack Loader Config:排除或包含 node_modules?
【发布时间】:2017-12-01 01:24:45
【问题描述】:

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

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

我不明白你为什么要或不包括它。显然它会引入代码并将其包含在输出构建中,我猜这只是加载器是否处理它。我只遇到过一个节点模块,如果加载器处理了它,它就不能工作,到目前为止,还没有一个不能以一种或另一种方式工作的节点模块。

除了一个包之外,其他包似乎都不在乎它们是包含还是排除。它对输出/浏览器有什么影响?

例如:

'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') ]
  };
};

【问题讨论】:

    标签: webpack loader webpack-2 babeljs


    【解决方案1】:

    这是我遵循的内容以及原因。

    我排除的所有.js 文件node_modules

    • 一般.js加载器链是ESlint然后是Babel
    • 不需要 Linting,因为您无法对 node_modules 的 Linting 结果做任何事情,除非您修复所有 lint 警告/错误(不认为会有任何)并且该模块的创建者接受这些更改并发布。
    • 大部分场景下发布的代码都是ES5代码,不需要babel。

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-23
      • 2017-04-14
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2019-03-29
      • 2018-07-12
      相关资源
      最近更新 更多