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

14

在运行webpack-dev-server时,所有输出中的错误似乎都指向bundle.js中的行号,而不是原始源文件。我如何获取原始源文件的行号? 我正在使用babel处理ES2015 js的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')
        }]
    }
};

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

完整源代码

2个回答

7

我不得不在我的babel加载器中添加retainLines选项:

loaders: [{
    test: /\.jsx?$/,
    loaders: ['babel?retainLines=true'],
    include: path.join(__dirname, 'src')
}]
https://babeljs.io/docs/usage/options/文档中提到:

保留行号。这将导致代码变得奇怪,但在无法使用源映射的情况下非常方便。

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

如果我有以下代码,它是如何工作的: module: { loaders: [ { test: /.jsx?$/, exclude: /(node_modules|bower_components)/, loader: 'babel-loader', query: { extends: path.join(__dirname, '.babelrc'), }, }, ..... - Jemar Jones
还没有尝试过这个语法,但或许可以使用 loader: 'babel-loader',query: { extends: path.join(__dirname, '.babelrc'), retainLines: true } ? - Steffen
对于 babel-loader,在 options 中添加它,如下所示 - { loader: "babel-loader", options: { retainLines: true, }, } - air4x

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接