From 7f20f12860733ad664d9093035f68bf2dbafd300 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Tue, 20 Feb 2018 23:36:23 -0800 Subject: [PATCH 1/3] SourceMap bugfixes --- src/SourceMap.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/SourceMap.js b/src/SourceMap.js index acc4f9222c5..f3a06654a38 100644 --- a/src/SourceMap.js +++ b/src/SourceMap.js @@ -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; @@ -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 ( @@ -221,6 +222,7 @@ class SourceMap { ) { middleIndex--; } + while ( middleIndex < this.mappings.length - 1 && this.mappings[middleIndex + 1][key].line === line && @@ -228,6 +230,7 @@ class SourceMap { ) { middleIndex++; } + return middleIndex; } From 8e8a60ea17e635ca9982e62aa149c530e98b2f5c Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Tue, 20 Feb 2018 23:50:09 -0800 Subject: [PATCH 2/3] Faster uglify sourcemaps Use raw mappings rather than generating and parsing the sourcemap from uglify. --- src/transforms/uglify.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/transforms/uglify.js b/src/transforms/uglify.js index 044460b2280..235caef2033 100644 --- a/src/transforms/uglify.js +++ b/src/transforms/uglify.js @@ -8,12 +8,32 @@ module.exports = async function(asset) { let source = (await asset.generate()).js; let customConfig = await asset.getConfig(['.uglifyrc']); + let sourceMap = asset.options.sourceMap && new SourceMap(); let options = { warnings: true, mangle: { toplevel: true }, - sourceMap: asset.options.sourceMaps ? {filename: asset.relativeName} : false + output: sourceMap + ? { + 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) { @@ -26,15 +46,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; } } From c51f83425890d6daea525eb2053e9afb250af17d Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Wed, 21 Feb 2018 00:01:41 -0800 Subject: [PATCH 3/3] prettier is not prettier --- src/transforms/uglify.js | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/transforms/uglify.js b/src/transforms/uglify.js index 235caef2033..49e45e52465 100644 --- a/src/transforms/uglify.js +++ b/src/transforms/uglify.js @@ -8,33 +8,35 @@ module.exports = async function(asset) { let source = (await asset.generate()).js; let customConfig = await asset.getConfig(['.uglifyrc']); - let sourceMap = asset.options.sourceMap && new SourceMap(); let options = { warnings: true, mangle: { toplevel: true - }, - output: sourceMap - ? { - 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 - } - }); + } + }; + + 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);