Webpack 2 - babel-loader - 如何排除node_modules?

时间:2017-03-07 02:17:57

标签: javascript babel webpack-2

自从我升级到Webpack 2后,我无法排除"排除"在我的"规则"。无法通过"排除"进入"选项"无论是。现在做正确的方法是什么?

之前:

plot(df$First, df$Measurement, pch=20, xlim=c(250,270),
    xlab="Time", ylab="Measurement")
points(df$Last, df$Measurement, pch=20)
segments(df$First, df$Measurement, df$Last, df$Measurement)

现在:

{
  test: /\.js$/,
  loader: 'babel-loader',
  exclude: /node_modules/,
}

整个配置:

{
  test: /\.js$/,
  use: [{ loader: 'babel-loader' }]
  ???
}

4 个答案:

答案 0 :(得分:7)

只需使用

module: {
    rules: [
        {
             test: /\.jsx?$/,
             include: [
                path.resolve(__dirname, "app")
              ],
             exclude: [
                path.resolve(__dirname, "app/demo-files")
             ]
        }
    ]
}

更多参考:https://webpack.js.org/configuration/

答案 1 :(得分:4)

这就是我们遇到同样问题的方法

{
  test: /\.js$/,        
  use: [{
    loader: 'babel-loader',
    options: {
      ignore: '/node_modules/'        
    }
  }]
}

来自https://babeljs.io/docs/usage/api/

答案 2 :(得分:1)

exclude中的webpack 2属性仍然与您展示的相同,但没有尝试过,它的工作原理只有

module: {
        loaders: [
        {
            test: /\.jsx?/,
            loader: 'babel-loader',
            exclude: [/node_modules/]
        },
    {
        test: /\.(jpg|png|gif|eot|woff2|woff|ttf|ico|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader'
    },
    {
        test: /\.(js)$/,
        loader: `babel-loader`,
        exclude: [/node_modules/]
    }]
}

答案 3 :(得分:1)

您是否考虑过在webpack.config.js中使用 externals 来忽略目录,在您的情况下是" node_modules"

https://webpack.js.org/guides/author-libraries/#external-limitations