Skip to content

Commit

Permalink
build: patch terser to create proper source-maps
Browse files Browse the repository at this point in the history
Currently terser is in a broken state where technically unmapped
bytes in the input file are randomly mapped back to source files.

"unmapped bytes" are bytes in the input file (that should be minified),
which cannot be mapped back to any source file. This is totally
valid as some code will be generated by rollup in order to create
bundles of multiple files. These bytes do not originate from any
source-file and therefore do not map.

In the current state of Terser where unmapped bytes are mapped to
random source-files, size tracking tools are not able to determine
accurate payload size contribution numbers of individual source-files,
nor do the broken source-maps help with debugging Angular packages
as some bytes are incorrectly mapping to a wrong file (this is not a
big deal though as the generated code is not likely to be debugged).

For context why we try to fix the terser bug on our side now: A PR
to fix this bug on the terser side has been submitted but it had to
be reverted due to a bug in the latest `mozilla/source-map` library.
Terser reverted the fix that hit the bug in `mozilla/source-map` as
they wanted to unblock consumers that use the latest `source-map`
package. Problem is that the bug in the `source-map` package is widely
spread and there doesn't seem to be a fix for it any time soon. Meaning
that source-maps needs to stay broken/incorrect as fixing them would
always cause developers to hit the `source-map` bug...

See `source-map` bug: mozilla/source-map#385
See Terser fix: terser/terser#342.
  • Loading branch information
devversion committed Aug 22, 2019
1 parent 0287b23 commit 56ca1ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ http_archive(
name = "build_bazel_rules_nodejs",
patch_args = ["-p1"],
# Patch https://github.com/bazelbuild/rules_nodejs/pull/903
patches = ["//tools:rollup_bundle_commonjs_ignoreGlobal.patch"],
# Patch "terser" used by the rollup bazel rule to create proper
# source-maps: https://github.com/terser-js/terser/pull/342
patches = [
"//tools:rollup_bundle_commonjs_ignoreGlobal.patch",
"//tools:rollup_terser_incorrect_source_maps.patch",
],
sha256 = "7c4a690268be97c96f04d505224ec4cb1ae53c2c2b68be495c9bd2634296a5cd",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.34.0/rules_nodejs-0.34.0.tar.gz"],
)
Expand Down
25 changes: 25 additions & 0 deletions tools/rollup_terser_incorrect_source_maps.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/internal/rollup/postinstall-patches.js b/internal/rollup/postinstall-patches.js
index 3e0720e..0f6f8e7 100644
--- a/internal/rollup/postinstall-patches.js
+++ b/internal/rollup/postinstall-patches.js
@@ -43,3 +43,20 @@ console.log(
'\n# patch: @buxlabs/amd-to-es6 to generate namespace imports instead of default imports');
sed('-i', 'ImportDefaultSpecifier', 'ImportNamespaceSpecifier',
'node_modules/@buxlabs/amd-to-es6/src/lib/getImportDeclaration.js');
+
+// Fix terser bug where generated code (e.g. from rollup) is incorrectly mapped to
+// source files. See: https://github.com/terser-js/terser/pull/342
+const terminateSegmentSnippet = `
+ var generatedPos = { line: gen_line + options.dest_line_diff, column: gen_col };
+ if (generatedPos.column !== 0) {
+ generator.addMapping({
+ generated: generatedPos,
+ original: null,
+ source: null,
+ name: null
+ });
+ }
+`
+sed('-i', /if \(info.source === null\) {/, `$& ${terminateSegmentSnippet}`,
+ 'node_modules/terser/dist/bundle.js');
+sed('-i', 'process.env.TERSER_NO_BUNDLE', 'true', 'node_modules/terser/bin/uglifyjs');

0 comments on commit 56ca1ea

Please sign in to comment.