Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack use existing source map from previous build step

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?

like image 578
Jesse Avatar asked Nov 19 '15 04:11

Jesse


People also ask

How do you use source maps in Webpack?

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 .

What is inline Sourcemap?

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.

Should I use Sourcemaps production?

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.


1 Answers

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"
      }
    ]
  }
};
like image 110
Jesse Avatar answered Dec 15 '22 06:12

Jesse