I have .ts
files compiled to .js
and .js.map
files by my editor and am bundling the .js
files using webpack. (I don't want webpack to be responsible for compiling the .ts
because then the errors won't appear properly in the editor.)
If I feed the compiled .js
files to webpack it doesn't pick up the existing .js.map
files (via //# sourceMappingURL=...
in each file), and so the resulting bundle.js.map
points to the .js
files, but not to the original .ts
files.
How can I get webpack to hold onto the existing .js.map
files so the resulting bundle.js.map
points right back to the original .ts
files?
Using Webpack, specifying devtool: "source-map" in your Webpack config will enable source maps, and Webpack will output a sourceMappingURL directive in your final, minified file. You can customize the source map filename itself by specifying sourceMapFilename .
Inline source map This is a special comment placed in your normal JavaScript file to tells your browser how to link the compiled code to the original version.
They are particularly useful for you and your team because they help tremendously for debugging issues as well as day-to-day work. I'm sure I make use of them just about every day. I'd say in general, they are used for local development.
It turns out an extra webpack "preLoader" called source-map-loader does the trick:
$ npm install source-map-loader --save
Then in webpack.config.js
:
module.exports = {
devtool: 'source-map',
module: {
preLoaders: [
{
test: /\.js$/,
loader: 'source-map-loader'
}
]
}
};
Update for Webpack 2+
module.exports = {
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
use: ["source-map-loader"],
enforce: "pre"
}
]
}
};
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