8

I read this question source maps files in production - is it safe?. I want to generate source map files in different folders with source code. Is there any way to do this with webpack? I also read this thread Relative SourceMap paths for debugging in WebStorm and tested but failed again.

Community
  • 1
  • 1
frankxu0203
  • 115
  • 1
  • 6

2 Answers2

13

You can use output.sourceMapFilename like so:

output: {
    filename: "bundles/[name].[chunkhash].min.js",
    sourceMapFilename: 'maps/[name].[chunkhash].map.js'
},

and then serve only from the bundles folder.

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
Ivan
  • 5,803
  • 2
  • 29
  • 46
  • ohhh thanks a lot. Really good answer and I believe I need to read the webpack guide from the very beginning again... – frankxu0203 Mar 28 '17 at 03:04
  • 1
    Ivan thanks for your answer. Maybe you can do it even better by adding a link to the [docs](https://webpack.js.org/configuration/output/#output-sourcemapfilename). – Mosh Feu Nov 15 '18 at 06:00
13

As Ivan wrote, the solution is to use output.sourceMapFilename

However, recommended solution from webpack doc is to only use [file] placeholder (default value is [file].map).

It is then best to define the value as following:

output: {
    filename: 'js/[name]-[chunkhash].js',
    sourceMapFilename: 'sourceMap/[file].map'
},

You will then preserve structure and it will also work for css sourceMaps

D0m3
  • 1,471
  • 2
  • 13
  • 19
  • I find this suggestion very important, because sourcmap file name must match what referenced inside the minified file. – Townsheriff Oct 18 '21 at 14:38