Skip to content

Commit fa047c8

Browse files
committed
feat: chain input source maps through built-in minimizers
terser, uglify-js, and cssnano now receive the previous minimizer's source map as their input source map, so chained minimizers (e.g. \`minify: [TerserPlugin.terserMinify, TerserPlugin.uglifyJsMinify]\` with \`devtool: "source-map"\`) emit a single combined source map that points back to the original sources. Adds three test cases that exercise multi-minimizer chains with source maps: terser+terser, terser+uglify-js, and cssnano+cssnano. Updates existing TerserPlugin and worker snapshots that previously captured the un-chained intermediate map.
1 parent 7fc0a91 commit fa047c8

5 files changed

Lines changed: 233 additions & 59 deletions

File tree

src/utils.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,14 @@ async function terserMinify(
289289
// Copy `terser` options
290290
const terserOptions = buildTerserOptions(minimizerOptions);
291291

292-
// Let terser generate a SourceMap
292+
// Let terser generate a SourceMap. Pass the input source map so that
293+
// chained minimizers produce a map back to the original sources.
293294
if (sourceMap) {
294-
terserOptions.sourceMap = { asObject: true };
295+
terserOptions.sourceMap =
296+
/** @type {import("terser").SourceMapOptions} */ ({
297+
asObject: true,
298+
content: sourceMap,
299+
});
295300
}
296301

297302
/** @type {ExtractedComments} */
@@ -538,9 +543,13 @@ async function uglifyJsMinify(
538543
// Copy `uglify-js` options
539544
const uglifyJsOptions = buildUglifyJsOptions(minimizerOptions);
540545

541-
// Let terser generate a SourceMap
546+
// Let `uglify-js` generate a SourceMap, chaining through the input
547+
// map so that combined minimizers map back to original sources.
542548
if (sourceMap) {
543-
uglifyJsOptions.sourceMap = true;
549+
uglifyJsOptions.sourceMap =
550+
/** @type {import("uglify-js").SourceMapOptions} */ (
551+
/** @type {unknown} */ ({ content: sourceMap })
552+
);
544553
}
545554

546555
/** @type {ExtractedComments} */
@@ -740,7 +749,7 @@ async function swcMinify(input, sourceMap, minimizerOptions, extractComments) {
740749
swcOptions.format = {};
741750
}
742751

743-
// Let `swc` generate a SourceMap
752+
// Let `swc` generate a SourceMap.
744753
if (sourceMap) {
745754
swcOptions.sourceMap = true;
746755
}
@@ -1301,7 +1310,7 @@ async function cssnanoMinify(
13011310
}
13021311

13031312
if (sourceMap) {
1304-
postcssOptions.map = { annotation: false };
1313+
postcssOptions.map = { annotation: false, prev: sourceMap };
13051314
}
13061315

13071316
const result = await postcss

0 commit comments

Comments
 (0)