首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack/Babel在转译前未检测到javascript错误

Webpack/Babel在转译前未检测到javascript错误
EN

Stack Overflow用户
提问于 2017-11-16 07:20:20
回答 1查看 153关注 0票数 2

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

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

这是我的package.json

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
   "presets": [
      "es2015", 
      "react", 
      "stage-1"
   ]
}

我的javascript文件加载器是

代码语言:javascript
复制
{
   test: /\.$|\.js$|\.jsx$/,
   exclude: /node_modules\/[^@]/,
   use: [{
     loader: 'babel-loader',
   }],
}
EN

回答 1

Stack Overflow用户

发布于 2017-11-16 15:45:57

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

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

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

代码语言:javascript
复制
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配置,包括错误输出的过滤器模式,可用于排除控制台中的冗余日志。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47318993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档