Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: patch terser to create proper source-maps [do not merge] #32277

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = "da217044d24abd16667324626a33581f3eaccabf80985b2688d6a08ed2f864be",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.37.1/rules_nodejs-0.37.1.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');