有没有办法在全局范围内从webpack中排除node_modules?

时间:2017-10-13 11:02:07

标签: javascript webpack bundle node-modules

我有简单的演示配置,用于从js和jsx文件中排除节点模块,但找不到全局排除node_modules的解决方案,因此我不需要在每个加载器中始终指定此排除

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: {
    app: path.resolve(__dirname, "../src/index.js")
  },
  output: {
    path: path.resolve(__dirname, "../dist"),
    filename: "[name].[chunkhash].js",
    publicPath: "/",
    chunkFilename: "[name].[chunkhash].js"
  },
  module: {
    rules: [
      { test: /\.js$/, loaders: ["babel-loader"], exclude: /node_modules/ },
      { test: /\.jsx$/, loaders: ["babel-loader"], exclude: /node_modules/ },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin([path.resolve(__dirname, "../dist")]),
    new HtmlWebpackPlugin({ template: path.resolve(__dirname, "../public/index.html") }),
    new ExtractTextPlugin("styles.css")
  ]
};

有没有办法实现这个目标?我在webpack 3上

1 个答案:

答案 0 :(得分:1)

您可以使用

{ test: /\.jsx?$/, loaders: ["babel-loader"], exclude: /node_modules/ },