Skip to content

Commit

Permalink
feat(terser): support source map files (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Sep 26, 2019
1 parent 66d9a40 commit d5bac48
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/terser/src/terser_minified.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Can be a .js file, a rule producing .js files as its default output, or a rule p
Note that you can pass multiple files to terser, which it will bundle together.
If you want to do this, you can pass a filegroup here.""",
allow_files = [".js"],
allow_files = [".js", ".map", ".mjs"],
mandatory = True,
),
"config_file": attr.label(
Expand Down Expand Up @@ -95,6 +95,9 @@ so that it only affects the current build.
),
}

def _filter_js(files):
return [f for f in files if f.is_directory or f.extension == "js" or f.extension == "mjs"]

def _terser(ctx):
"Generate actions to create terser config run terser"

Expand All @@ -103,17 +106,19 @@ def _terser(ctx):
inputs = ctx.files.src[:]
outputs = []

directory_srcs = [s for s in ctx.files.src if s.is_directory]
sources = _filter_js(inputs)
sourcemaps = [f for f in inputs if f.extension == "map"]
directory_srcs = [s for s in sources if s.is_directory]
if len(directory_srcs) > 0:
if len(ctx.files.src) > 1:
if len(sources) > 1:
fail("When directories are passed to terser_minified, there should be only one input")
outputs.append(ctx.actions.declare_directory(ctx.label.name))
else:
outputs.append(ctx.actions.declare_file("%s.js" % ctx.label.name))
if ctx.attr.sourcemap:
outputs.append(ctx.actions.declare_file("%s.js.map" % ctx.label.name))

args.add_all([s.path for s in ctx.files.src])
args.add_all([s.path for s in sources])
args.add_all(["--output", outputs[0].path])

debug = ctx.attr.debug or "DEBUG" in ctx.var.keys()
Expand All @@ -126,9 +131,12 @@ def _terser(ctx):
# see https://github.com/terser-js/terser#command-line-usage
source_map_opts = ["includeSources", "base=" + ctx.bin_dir.path]

# We support only inline sourcemaps for now.
# It's hard to pair up the .js inputs with corresponding .map files
source_map_opts.append("content=inline")
if len(sourcemaps) == 0:
source_map_opts.append("content=inline")
elif len(sourcemaps) == 1:
source_map_opts.append("content='%s'" % sourcemaps[0].path)
else:
fail("When sourcemap is True, there should only be one or none input sourcemaps")

# This option doesn't work in the config file, only on the CLI
args.add_all(["--source-map", ",".join(source_map_opts)])
Expand Down
26 changes: 26 additions & 0 deletions packages/terser/test/sourcemap/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
load("@npm_bazel_terser//:index.from_src.bzl", "terser_minified")

filegroup(
name = "src1_and_map",
srcs = [
"src1.js",
"src1.js.map",
],
)

terser_minified(
name = "src1.min",
src = ":src1_and_map",
)

jasmine_node_test(
name = "test",
srcs = [
"terser_spec.js",
],
data = ["@npm//source-map"],
deps = [
":src1.min",
],
)
1 change: 1 addition & 0 deletions packages/terser/test/sourcemap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The src1.js{,.map} files were generated from running `tsc --lib es2015,dom --sourcemap --noImplicitUseStrict src1.ts`.
10 changes: 10 additions & 0 deletions packages/terser/test/sourcemap/src1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/terser/test/sourcemap/src1.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/terser/test/sourcemap/src1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// clang-format off
/*a comment*/ export class MyClass {
constructor(s: string) {
console.log(s);
}
field: string|undefined;
}
20 changes: 20 additions & 0 deletions packages/terser/test/sourcemap/terser_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const sm = require('source-map');
const DIR = 'build_bazel_rules_nodejs/packages/terser/test/sourcemap';

describe('terser sourcemap handling', () => {
it('should produce a sourcemap output', async () => {
const file = require.resolve(DIR + '/src1.min.js.map');
const rawSourceMap = JSON.parse(fs.readFileSync(file, 'utf-8'));
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
const pos = consumer.originalPositionFor(
// position of MyClass in terser_minified output src1.min.js
// depends on DEBUG flag
!process.env['DEBUG'] ? {line: 1, column: 18} : {line: 3, column: 5});
expect(pos.source).toBe('src1.ts');
expect(pos.line).toBe(2);
expect(pos.column).toBe(14);
expect(pos.name).toBe('MyClass');
});
});
});

0 comments on commit d5bac48

Please sign in to comment.