title: Webpack: Modify source before generating source maps
body: <p>I'm using Webpack 4, <a href="https://webpack.js.org/plugins/source-map-dev-tool-plugin/" rel="nofollow noreferrer">SourceMapDevToolPlugin</a> and <a href="https://webpack.js.org/plugins/terser-webpack-plugin" rel="nofollow noreferrer">terser-webpack-plugin</a>.</p>
  <p>The problem is that I need to also modify the minified JS-bundles – inject specific code to the beginning of file. The mutation is made using custom webpack plugin that takes an asset source string, prepends it with my code and creates a new RawSource. And this is what obviously breaks source maps mapping. If I remove the mutation part then everything works fine.</p>
  <p>The key point here is to made this mutation <strong>after minification</strong> but <strong>before generating</strong> the source maps, while Terser makes this at the same time as I understood.</p>
  <p>Is this possible with the current stack that I use?</p>
  <p>Terser config:</p>
  <pre class="lang-js prettyprint-override"><code>optimization: {
      minimizer: [
          new TerserPlugin({
              sourceMap: true,
              terserOptions: {
                  mangle: true,
                  ecma: 6,
                  compress: {
                      inline: 1,
                      pure_funcs: ['Set', 'Map'],
                  },
              },
          }),
      ],
  },
  </code></pre>
  <p>SourceMapDevToolPlugin config:</p>
  <pre class="lang-js prettyprint-override"><code>plugins: [
      new SourceMapDevToolPlugin({
          filename: '[file].map',
          publicPath: 'https://my-custom-url.net/source-maps/',
      }),
  ],
  </code></pre>