我为新的React项目配置了webpack-dev-server。我正在使用babel-loader来转换ES6代码。在之前的项目中,我们能够在转换之前检测到javascript代码中的明显错误,例如缺少import语句或未定义的变量。
为什么webpack / babel-loader没有检测到明显的错误?如何配置babel-loader以不转发这些错误,而是登录到命令提示符?
这是我的package.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',
}],
}
答案 0 :(得分:1)
如何配置babel-loader以不转发这些错误,而是登录到命令提示符?
y,x = my_list[0]
具有足够的错误记录配置,可以在声明模式下提供。
只需使用以下脚本(分别命名为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
和errors
选项,这些选项将指示包含编译错误的块和源代码行。
此处https://webpack.js.org/configuration/stats/有关errorDetails
配置的读取模式,包括错误输出的过滤模式,可用于排除控制台中的冗余日志记录。