17

My production webpack configuration is:

{
  output: {
    publicPath: "https://cdn.example.com/sub-directory/",
    filename: '[name]-[chunkhash].min.js'
  },

  devtool: 'source-map',

  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
}

Now I have two files app-12345.min.js and app-12345.min.js.map.

I also have automatically generated CDN URL https://cdn.example.com/sub-directory/app-12345.min.js for main script.

But sourceMappingURL is still relative path //# sourceMappingURL=app-12345.min.js.map and not accessible directly in browser.

My question is how I can set sourceMappingURL as absolute automatically generated path?

Ky6uk
  • 1,067
  • 3
  • 16
  • 26

4 Answers4

9

The SourceMapDevToolPlugin plugin is an option.

new webpack.SourceMapDevToolPlugin({
    filename: '[file].map',
    append: '\n//# sourceMappingURL=' + path + '[url]'
});
ornj
  • 720
  • 7
  • 16
  • Resulted file will contain two `sourceMappingURL` strings in this case. `//# sourceMappingURL=https://example.com/app.js.map` and `//# sourceMappingURL=app.js.map` – Ky6uk Apr 18 '17 at 23:25
  • 1
    to fix this use `config.devtool = 'hidden-source-map'` then `append: ...` – daviestar Dec 13 '17 at 11:53
9

Finally this is possible in Webpack 3 using guide from the @omj's response and following configuration

  devtool: 'hidden-source-map', // SourceMap without reference in original file

  new webpack.SourceMapDevToolPlugin({
    filename: '[file].map',
    append: `\n//# sourceMappingURL=${path}[url]`
  })

Update (Webpack v3.10.0):

A new option has been added since Webpack v3.10.0. The option called publicPath:

new webpack.SourceMapDevToolPlugin({
  filename: '[file].map',
  publicPath: 'https://example.com/dev/'
});
Ky6uk
  • 1,067
  • 3
  • 16
  • 26
6

Note the devtool: false. You need that if you want to provide custom values. This works for webpack 4.x:

module.exports = {
  // ...
  devtool: false,
  plugins: [
    new webpack.SourceMapDevToolPlugin({
       filename: 'sourcemaps/[file].map',
       publicPath: 'https://example.com/project/',
       fileContext: 'public'
  })
  ]
};
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
0

With Webpack 2 you can use hidden-source-map for devtool and banner-webpack-plugin from https://github.com/lcxfs1991/banner-webpack-plugin and set something like this:

new banner({
    chunks: {
        "main": {
            afterContent: "\n//# sourceMappingURL=custom/url/to/map\n"
         }
    }
})
J-Alex
  • 6,881
  • 10
  • 46
  • 64