Undistraction
Undistraction

Reputation: 43402

Why is no sourcemap generated when minimizing source?

I need to add source-maps to a Webpack 4 production build.

I am using the following config:

{
  …
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        sourceMap: true,
      }),
    ],
  },
  devtool: 'source-map'
  …
}

However, although this minimises the bundle, it results in a virtually empty source-map at main.js.map:

{"version":3,"file":"main.js","sources":["webpack://AdminFrontend/main.js"],"mappings":";AAAA","sourceRoot":""}

If i set minimize to false, I get a full source-map, but the bundle is enormous.

What do I need to do to both minimize the source AND generate a full sourcemap?

Upvotes: 3

Views: 965

Answers (1)

Alix Wang
Alix Wang

Reputation: 11

you can customize webpack minimizer config like this

const minify = (input, sourceMap, minimizerOptions, extractsComments) => {
  const { sourceMap: uglifySourceMap, code } = require('uglify-js').minify(input);
  return { map: sourceMap, code, warnings: [], errors: [], extractedComments: [] };
}
optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        sourceMap: true,
        parallel: true,
        minify: minify,
      }),
    ],
  },

after do that you will get full content map file

Upvotes: 1

Related Questions