webpack-dev-server: how to get error line numbers of orignal files
Asked Answered
C

2

14

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.

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

Candiot answered 11/5, 2016 at 23:9 Comment(0)
C
7

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/

The documentation says

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.

Candiot answered 13/5, 2016 at 9:55 Comment(3)
How does this work if i have something like: module: { loaders: [ { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, loader: 'babel-loader', query: { extends: path.join(__dirname, '.babelrc'), }, }, ..... ?Quince
Haven't tried this syntax, but maybe loader: 'babel-loader', query: { extends: path.join(__dirname, '.babelrc'), retainLines: true } ?Candiot
For babel-loader, add it within options like - { loader: "babel-loader", options: { retainLines: true, }, }Amalita
K
6

Use cheap-module-source-map in your webpack config.

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

See more: https://webpack.js.org/configuration/devtool

Kutenai answered 5/5, 2017 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.