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

时间:2016-05-11 23:09:45

标签: webpack babeljs webpack-dev-server

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

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

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}/`);
});

full source code

2 个答案:

答案 0 :(得分:4)

我必须在我的babel加载器中添加retainLines选项:

FROM ubuntu:14.04

ENTRYPOINT ["/bin/bash", "-c"]
CMD ["/virtualenv/bin/python", "/mycode/myscript.py", "--param1"]

https://babeljs.io/docs/usage/options/

文档说

  

保留行号。这将导致古怪的代码,但对于无法使用源映射的情况非常方便。

如果有人知道一种不会导致"古怪"代码(无论那意味着什么)请告诉我。

答案 1 :(得分:3)

在你的webpack配置中使用cheap-module-source-map。

const config = {
  devtool: 'cheap-module-eval-source-map',
  ...
}

了解更多 https://webpack.js.org/configuration/devtool/