gpt4 book ai didi

webpack-dev-server:如何获取原始文件的错误行号

转载 作者:行者123 更新时间:2023-12-04 07:51:26 26 4
gpt4 key购买 nike

随着 webpack-dev-server 运行,输出中的所有错误似乎都指向 bundle.js 中的行号,而不是原始源文件。如何获取原始源文件的行号?
我在 ES2015 js 中使用带有 babel 的 webpack。

Screenshot

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')
}]
}
};

服务器.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}/`);
});

full source code

最佳答案

我必须在我的 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.



如果有人知道一种不会导致“古怪”代码(无论这意味着什么)的方法,请告诉我。

关于webpack-dev-server:如何获取原始文件的错误行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37174721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com