WebPack Loader配置:要排除或包含node_modules?

时间:2017-06-27 20:51:05

标签: webpack babeljs loader webpack-2 babel

我应该在WebPack中为各种加载器包含或排除node_modules吗?

我已经看到了一些隐含包含它的配置,还有一些在各种加载器中明确排除它(通过排除node_modules或仅包括src或客户端文件夹)(JS,TS,CSS,SCSS,文件,网址,原始等。)

我不明白为什么你会或不会包含它。显然它会引入代码并将其包含在输出构建中,无论哪种方式,我都在猜测它是否是加载程序处理它。我只遇到过一个节点模块,如果加载程序处理它,它就不起作用,到目前为止,没有一个节点模块不会以这种或那种方式工作。

除了一个包之外,其他人似乎都不关心他们是否被包括或排除。它对输出/浏览器有什么不同?

例如:

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

1 个答案:

答案 0 :(得分:4)

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

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

  • 常规.js加载链是ESlint,然后是Babel
  • 不需要Linting,因为你不能对node_modules的Linting结果做任何事情,除非你修复了所有lint警告/错误(不认为会有任何错误)并且该模块的创建者接受这些变化并将其发布。
  • 大多数情况下发布的代码都是ES5代码,您不需要使用babel。

如果您有任何偏离此的内容,可以包含这些模块。

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

对于其他文件:我发现您可能不会为node_modules添加排除,因为例如CSS会在节点模块库中require()它也必须捆绑在一起。

相关问题