Webpack/Babel 在转译之前没有检测到 JavaScript 错误



我已经为一个新的 React 项目配置了 webpack-dev-server。我正在使用 babel-loader 来转译 ES6 代码。在以前的项目中,我们能够在转译之前检测到 javascript 代码中的明显错误,例如缺少导入语句或未定义的变量。

为什么 webpack/babel-loader 没有检测到明显的错误?我如何将 babel-loader 配置为不转译这些错误,而是记录到命令提示符?

这是我的包.json

{
  "name": "advent-calendar",
  "version": "1.0.0",
  "description": "",
  "main": "src/entry.js",
  "scripts": {
    "dev": "webpack-dev-server --config config/webpack.config.dev.js",
    "build:webpack": "cross-env NODE_ENV=production && webpack --config config/webpack.config.prod.js",
    "build": "npm run build:webpack --verbose --color",
    "test": "cross-env NODE_ENV=development ./node_modules/.bin/mocha --opts mocha.opts",
    "test:watch": "npm run test -- --watch",
    "lint": "npm run -s lint:css:raw ; npm run -s lint:js:raw",
    "lint:css": "npm run -s lint:css:raw",
    "lint:css:raw": "stylelint "src/**/*.{css,less,scss}"",
    "lint:js": "npm run -s lint:js:raw",
    "lint:js:raw": "eslint  "src/**/*.js"",
    "lint:js:raw:fix": "./node_modules/.bin/eslint --fix "src/**/*.js"",
    "lint:js:watch": "esw "src/**/*.js" --watchyar",
    "lint:watch": "npm run lint:js:watch"
  },
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.0.2",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "chai": "^4.1.2",
    "chai-enzyme": "^0.8.0",
    "cross-env": "^5.1.1",
    "css-loader": "^0.28.7",
    "enzyme": "^3.1.1",
    "enzyme-adapter-react-16": "^1.0.4",
    "eslint": "^4.10.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-chai-friendly": "^0.4.0",
    "eslint-plugin-eslint-comments": "^2.0.1",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.4.0",
    "file-loader": "^1.1.5",
    "html-webpack-plugin": "^2.30.1",
    "jsdom": "^9.9.1",
    "less": "^2.7.3",
    "less-loader": "^4.0.5",
    "mocha": "^4.0.1",
    "node-sass": "^4.6.0",
    "nyc": "~9.0.1",
    "path": "^0.12.7",
    "prop-types": "^15.6.0",
    "sass-loader": "^6.0.6",
    "simple-jsdom": "^3.0.0",
    "sinon": "^4.1.2",
    "sinon-chai": "^2.14.0",
    "style-loader": "^0.19.0",
    "stylelint": "^8.2.0",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.4"
  },
  "dependencies": {
    "classnames": "^2.2.5",
    "crypto-js": "^3.1.9-1",
    "css-reset": "^0.0.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-transition-group": "^2.2.1",
    "redux": "^3.7.2",
    "redux-devtools-extension": "^2.13.2",
    "redux-saga": "^0.16.0"
  }
}

这是我的 .babelrc

{
   "presets": [
      "es2015", 
      "react", 
      "stage-1"
   ]
}

我的 javascript 文件的加载器是

{
   test: /.$|.js$|.jsx$/,
   exclude: /node_modules/[^@]/,
   use: [{
     loader: 'babel-loader',
   }],
}

我如何将 babel-loader 配置为不转译这些错误,而是记录到命令提示符?

Webpack-dev-server有足够的配置用于错误日志记录,可以在声明模式下提供。

只需使用以下脚本(分别命名为 run-dev.js 和补丁package.json(即可启动客户端部分:

import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import config from './webpack.dev.config';
const outputConfig = {
    colors: true,
    hash: false,
    version: false,
    timings: false,
    assets: false,
    chunks: false,
    modules: false,
    reasons: false,
    children: false,
    source: false,
    errors: true,
    errorDetails: true,
    warnings: true,
    publicPath: false
};
const clientCompiler = webpack(config);
const clientDevServer = new WebpackDevServer(clientCompiler, {
    stats: outputConfig
});
clientDevServer.listen(3000, '127.0.0.1');

然后,您可以根据要求和输出的详细程度自定义outputConfig对象。不要忘记打开errorserrorDetails选项,这将指示带有编译错误的块和源代码行。

此处的读取模式 https://webpack.js.org/configuration/stats/有关outputConfig配置的信息,包括用于错误输出的筛选器模式,可用于排除控制台中的冗余日志记录。

最新更新