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.
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')
}]
}
};
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
Using Watch Mode You can instruct webpack to "watch" all files within your dependency graph for changes. If one of these files is updated, the code will be recompiled so you don't have to run the full build manually. Now run npm run watch from the command line and see how webpack compiles your code.
Just navigate the browser to http://<host>:<port>/webpack-dev-server/<path> . I. e. with the above configuration http://localhost:8080/webpack-dev-server/index.html .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With