diff --git a/lib/sourcemap.js b/lib/sourcemap.js index 232cfabda9a..1fdf15c5a40 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -100,21 +100,29 @@ function SourceMap(options) { var sources_content = options.content && Object.create(null); var names = create_array_map(); var mappings = ""; - if (options.orig) Object.keys(options.orig).forEach(function(name) { - var map = options.orig[name]; - var indices = [ 0, 0, 1, 0, 0 ]; - map.mappings = map.mappings.split(/;/).map(function(line) { - indices[0] = 0; - return line.split(/,/).map(function(segment) { - return indices.slice(0, vlq_decode(indices, segment)); - }); + var orig_maps; + if (options.orig) { + orig_maps = Object.create(null); + Object.keys(options.orig).forEach(function(name) { + var map = options.orig[name]; + var indices = [ 0, 0, 1, 0, 0 ]; + orig_maps[name] = { + names: map.names, + mappings: map.mappings.split(/;/).map(function(line) { + indices[0] = 0; + return line.split(/,/).map(function(segment) { + return indices.slice(0, vlq_decode(indices, segment)); + }); + }), + sources: map.sources, + }; + if (!sources_content || !map.sourcesContent) return; + for (var i = 0; i < map.sources.length; i++) { + var content = map.sourcesContent[i]; + if (content) sources_content[map.sources[i]] = content; + } }); - if (!sources_content || !map.sourcesContent) return; - for (var i = 0; i < map.sources.length; i++) { - var content = map.sourcesContent[i]; - if (content) sources_content[map.sources[i]] = content; - } - }); + } var generated_line = 1; var generated_column = 0; var source_index = 0; @@ -122,8 +130,8 @@ function SourceMap(options) { var original_column = 0; var name_index = 0; return { - add: options.orig ? function(source, gen_line, gen_col, orig_line, orig_col, name) { - var map = options.orig[source]; + add: orig_maps ? function(source, gen_line, gen_col, orig_line, orig_col, name) { + var map = orig_maps[source]; if (map) { var segments = map.mappings[orig_line - 1]; if (!segments) return; diff --git a/test/mocha/sourcemaps.js b/test/mocha/sourcemaps.js index f8e399ce585..23698c575d7 100644 --- a/test/mocha/sourcemaps.js +++ b/test/mocha/sourcemaps.js @@ -277,6 +277,12 @@ describe("sourcemaps", function() { }); describe("input sourcemaps", function() { + it("Should not modify input source map", function() { + var orig = get_map(); + var original = JSON.stringify(orig); + var map = prepare_map(orig); + assert.strictEqual(JSON.stringify(orig), original); + }); it("Should copy over original sourcesContent", function() { var orig = get_map(); var map = prepare_map(orig);