Skip to content

Commit

Permalink
Merge pull request #875 from parcel-bundler/sourcemap-improvements
Browse files Browse the repository at this point in the history
Uglify Sourcemap improvements
  • Loading branch information
Jasper De Moor authored Feb 21, 2018
2 parents c25f9bc + c51f834 commit 59436cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/SourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class SourceMap {
let middleIndex = Math.floor((stopIndex + startIndex) / 2);

while (
this.mappings[middleIndex][key].line !== line &&
startIndex < stopIndex
startIndex < stopIndex &&
this.mappings[middleIndex][key].line !== line
) {
if (line < this.mappings[middleIndex][key].line) {
stopIndex = middleIndex - 1;
Expand All @@ -211,8 +211,9 @@ class SourceMap {
middleIndex = Math.floor((stopIndex + startIndex) / 2);
}

if (this.mappings[middleIndex][key].line !== line) {
return middleIndex;
let mapping = this.mappings[middleIndex];
if (!mapping || mapping[key].line !== line) {
return this.mappings.length - 1;
}

while (
Expand All @@ -221,13 +222,15 @@ class SourceMap {
) {
middleIndex--;
}

while (
middleIndex < this.mappings.length - 1 &&
this.mappings[middleIndex + 1][key].line === line &&
column > this.mappings[middleIndex][key].column
) {
middleIndex++;
}

return middleIndex;
}

Expand Down
33 changes: 27 additions & 6 deletions src/transforms/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,32 @@ module.exports = async function(asset) {
warnings: true,
mangle: {
toplevel: true
},
sourceMap: asset.options.sourceMaps ? {filename: asset.relativeName} : false
}
};

let sourceMap;
if (asset.options.sourceMap) {
sourceMap = new SourceMap();
options.output = {
source_map: {
add(source, gen_line, gen_col, orig_line, orig_col, name) {
sourceMap.addMapping({
source,
name,
original: {
line: orig_line,
column: orig_col
},
generated: {
line: gen_line,
column: gen_col
}
});
}
}
};
}

if (customConfig) {
options = Object.assign(options, customConfig);
}
Expand All @@ -26,15 +48,14 @@ module.exports = async function(asset) {
throw result.error;
}

if (result.map) {
result.map = await new SourceMap().addMap(JSON.parse(result.map));
if (sourceMap) {
if (asset.sourceMap) {
asset.sourceMap = await new SourceMap().extendSourceMap(
asset.sourceMap,
result.map
sourceMap
);
} else {
asset.sourceMap = result.map;
asset.sourceMap = sourceMap;
}
}

Expand Down

0 comments on commit 59436cd

Please sign in to comment.