From 994293e97229110f676dc72ad68b6b2d8a015394 Mon Sep 17 00:00:00 2001 From: Jimb Esser Date: Mon, 18 Jan 2021 15:44:24 -0800 Subject: [PATCH] Fix overwriting existing sourcesContent in sourcemaps (#4567) --- lib/sourcemap.js | 4 +++- test/mocha/sourcemaps.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/sourcemap.js b/lib/sourcemap.js index c2587c88..94966a6a 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -144,7 +144,9 @@ function SourceMap(options) { add(source, gen_line, gen_col, orig_line, orig_col, name); } : add, setSourceContent: sources_content ? function(source, content) { - sources_content[source] = content; + if (!(source in sources_content)) { + sources_content[source] = content; + } } : noop, toString: function() { return JSON.stringify({ diff --git a/test/mocha/sourcemaps.js b/test/mocha/sourcemaps.js index 91b38cd2..59642068 100644 --- a/test/mocha/sourcemaps.js +++ b/test/mocha/sourcemaps.js @@ -244,6 +244,39 @@ describe("sourcemaps", function() { assert.strictEqual(result.code, '(function(){console.log("hello")}).call(this);'); assert.strictEqual(result.map, '{"version":3,"sources":["main.coffee"],"names":["console","log"],"mappings":"CAAA,WAAAA,QAAQC,IAAI"}'); }); + it("Should not overwrite existing sourcesContent", function() { + var result = UglifyJS.minify({ + "in.js": [ + '"use strict";', + "", + "var _window$foo = window.foo,", + " a = _window$foo[0],", + " b = _window$foo[1];", + ].join("\n"), + }, { + compress: false, + mangle: false, + sourceMap: { + content: { + version: 3, + sources: [ "in.js" ], + names: [ + "window", + "foo", + "a", + "b", + ], + mappings: ";;kBAAaA,MAAM,CAACC,G;IAAfC,C;IAAGC,C", + file: "in.js", + sourcesContent: [ "let [a, b] = window.foo;\n" ], + }, + includeSources: true, + }, + }); + if (result.error) throw result.error; + assert.strictEqual(result.code, '"use strict";var _window$foo=window.foo,a=_window$foo[0],b=_window$foo[1];'); + assert.strictEqual(result.map, '{"version":3,"sources":["in.js"],"sourcesContent":["let [a, b] = window.foo;\\n"],"names":["window","foo","a","b"],"mappings":"6BAAaA,OAAOC,IAAfC,E,eAAGC,E"}'); + }); }); describe("sourceMapInline", function() { -- 2.34.1