首页 > 解决方案 > 可以只显示 webpack 中的错误吗?

问题描述

当我的文件发生更改时,webpack-watch 会显示大量文本(下图的 3 倍)。这让我发疯,因为我只想查看已更改的文件或可能发生的错误。我忽略了很多错误,因为文本太多。

是否可以只显示错误,或者只显示真正更改的文件?如果是这样,我怎样才能做到这一点?

在此处输入图像描述

这是我的配置:

// webpack.config.js
const webpack = require("webpack");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const config = {
  context: path.resolve(__dirname),
  entry: "./index.js",
  devServer: {
    contentBase: "./dist",
    stats: { chunks: false } 
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".json"]
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname),
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: [["@babel/env", { modules: false }], "@babel/react"]
            }
          }
        ]
      },
      {
        test: /\.tsx?$/,
        loader: "awesome-typescript-loader"
      },
      {
         test: /\.css$/,
         use: [MiniCssExtractPlugin.loader, "css-loader"]
       }
    ],
    noParse: [/aws-sdk/]
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
      "process.env.STATIC_PORT": JSON.stringify(process.env.STATIC_PORT),
      VERSION: JSON.stringify(require("./package.json").version)
    }),
    new MiniCssExtractPlugin({
      filename: 'bundle.css'
    }),
    new CopyWebpackPlugin([{ from: "./cb_icons", to: "cb_icons" }])
  ],
  node: { fs: "empty" },
  externals: [{ "./cptable": "var cptable" }, { "./jszip": "jszip" }],
  performance: { hints: false },
  watchOptions: {
   ignored: ['node_modules'],
   poll: 1000
 }
};

module.exports = config;

标签: webpackcommand-lineconfig

解决方案


是的,可以通过stats选项来完成,根据文档:

https://webpack.js.org/configuration/dev-server/#devserver-stats-

如果您只想记录错误消息:

devServer: {
    stats: 'errors-only'
  }

另外,看看这个选项:

https://webpack.js.org/configuration/dev-server/#devserver-clientloglevel


推荐阅读