webpack-dev-server:如何获取原始文件的错误行号
webpack-dev-server: how to get error line numbers of orignal files
使用 webpack-dev-server 运行 似乎输出中的所有错误都指向 bundle.js 中的行号而不是原始源文件。如何获取原始源文件的行号?
我正在为 ES2015 js 使用带有 babel 的 webpack。
webpack.config.dev.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 加载程序添加 retainLines 选项:
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.
如果有人知道不会导致 "wacky" 代码的方法(无论那是什么意思),请告诉我。
在你的 webpack 配置中使用 cheap-module-source-map。
const config = {
devtool: 'cheap-module-eval-source-map',
...
}
使用 webpack-dev-server 运行 似乎输出中的所有错误都指向 bundle.js 中的行号而不是原始源文件。如何获取原始源文件的行号? 我正在为 ES2015 js 使用带有 babel 的 webpack。
webpack.config.dev.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 加载程序添加 retainLines 选项:
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.
如果有人知道不会导致 "wacky" 代码的方法(无论那是什么意思),请告诉我。
在你的 webpack 配置中使用 cheap-module-source-map。
const config = {
devtool: 'cheap-module-eval-source-map',
...
}