webpack-dev-server:如何获取原始文件的错误行号 [英] webpack-dev-server: how to get error line numbers of orignal files
问题描述
运行webpack-dev-server时,似乎输出中的所有错误都指向bundle.js中的行号,而不是原始源文件.如何获得原始源文件的行号? 我正在将webpack与babel一起用于ES2015 js.
With webpack-dev-server running it seems that all errors in the output are pointing to line numbers in the bundle.js and not the original source file. How do I get line numbers of the original source files? I'm using webpack with babel for ES2015 js.
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
devtool: '#source-map',
entry: [
`webpack-dev-server/client?http://${process.env.npm_package_config_host}:${process.env.npm_package_config_port}`,
'webpack/hot/only-dev-server',
'react-hot-loader/patch',
'./src/index.dev'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin({
template: 'index.html', // Load a custom template
inject: 'body' // Inject all scripts into the body
})
],
module: {
loaders: [{
test: /\.jsx?$/,
loaders: ['babel'],
include: path.join(__dirname, 'src')
}]
}
};
server.js
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.config.dev');
const port = process.env.npm_package_config_port || 3000;
const host = process.env.npm_package_config_host || 'localhost';
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
stats: {
colors: true,
chunks: false,
'errors-only': true
}
}).listen(port, host, function (err) {
if (err) {
console.log(err);
}
console.log(`Listening at http://${host}:${port}/`);
});
推荐答案
我必须在我的babel加载程序中添加keepLines选项:
I had to add retainLines option to my babel loader:
loaders: [{
test: /\.jsx?$/,
loaders: ['babel?retainLines=true'],
include: path.join(__dirname, 'src')
}]
https://babeljs.io/docs/usage/options/
文档说
保留行号.这将导致古怪的代码,但在您无法使用源地图的情况下非常方便.
Retain line numbers. This will lead to wacky code but is handy for scenarios where you can’t use source maps.
如果有人知道不会导致古怪"代码的方式(无论如何),请告诉我.
If someone knows a way that doesn't lead to "wacky" code (whatever that means) please let me know.
这篇关于webpack-dev-server:如何获取原始文件的错误行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!