Skip to content

Commit f340589

Browse files
committed
feat(rollup): ensure that sourcemaps work end-to-end
1 parent e9ba7df commit f340589

13 files changed

Lines changed: 112 additions & 19 deletions

File tree

examples/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ example_integration_test(
8787
example_integration_test(
8888
name = "examples_webapp",
8989
npm_packages = {
90+
"//packages/jasmine:npm_package": "@bazel/jasmine",
9091
"//packages/protractor:npm_package": "@bazel/protractor",
9192
"//packages/rollup:npm_package": "@bazel/rollup",
9293
"//packages/terser:npm_package": "@bazel/terser",

examples/webapp/BUILD.bazel

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@npm//http-server:index.bzl", "http_server")
2+
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
23
load("@npm_bazel_protractor//:index.bzl", "protractor_web_test_suite")
34
load(":differential_loading.bzl", "differential_loading")
45

@@ -30,10 +31,16 @@ protractor_web_test_suite(
3031
],
3132
)
3233

33-
# Just a dummy test so that we have a test target for //... on certain bazelci platforms with bazel_integration_test
34-
sh_test(
35-
name = "dummy_test",
36-
srcs = ["dummy_test.sh"],
34+
jasmine_node_test(
35+
name = "test_sourcemaps",
36+
srcs = ["sourcemaps.spec.js"],
37+
deps = [
38+
":app_chunks",
39+
":app_chunks.min",
40+
":app_chunks_es5",
41+
":app_chunks_es5.min",
42+
"@npm//source-map",
43+
],
3744
)
3845

3946
# For testing from the root workspace of this repository with bazel_integration_test.

examples/webapp/differential_loading.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def differential_loading(name, entry_point, srcs):
1212
rollup_bundle(
1313
name = name + "_chunks",
1414
srcs = srcs,
15+
sourcemap = "inline",
1516
entry_points = {
1617
entry_point: "index",
1718
},

examples/webapp/es5.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"sourceMaps": "inline",
23
"presets": [
34
[
45
"@babel/preset-env",

examples/webapp/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import('./strings.en').then(m => {
33
const msg = document.createElement('div');
44
msg.innerText = m.hello();
5+
// For sourcemap testing, keep this string literal on line 6 column 21 !!
56
msg.className = 'ts1';
67
document.body.appendChild(msg);
78
});

examples/webapp/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"@babel/cli": "^7.6.0",
55
"@babel/core": "^7.6.0",
66
"@babel/preset-env": "^7.6.0",
7-
"@bazel/rollup": "latest",
8-
"@bazel/terser": "latest",
9-
"@bazel/protractor": "latest",
7+
"@bazel/jasmine": "^0.37.1",
8+
"@bazel/protractor": "file:/tmp/tmp-164023L7r1MgY0dmka",
9+
"@bazel/rollup": "file:/tmp/tmp-164023xwqtzUemMOVT",
10+
"@bazel/terser": "file:/tmp/tmp-164023K73Ev0H45uDb",
1011
"http-server": "^0.11.1",
11-
"terser": "^4.3.1",
12-
"rollup": "1.21.4"
12+
"rollup": "1.21.4",
13+
"source-map": "^0.7.3",
14+
"terser": "^4.3.1"
1315
},
1416
"scripts": {
1517
"test": "bazel test ..."

examples/webapp/sourcemaps.spec.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Ensure we have working sourcemaps when the app runs in a browser
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const sm = require('source-map');
6+
7+
const PRAGMA = '//# sourceMappingURL=';
8+
const DATA = 'data:application/json;charset=utf-8;base64,';
9+
10+
function read(...f) {
11+
return fs.readFileSync(require.resolve(path.join(__dirname, ...f)), 'utf-8');
12+
}
13+
14+
function parseSourceMap(content) {
15+
const sourcemapLine = content.split(/\r?\n/).find(l => l.startsWith(PRAGMA));
16+
if (!sourcemapLine) {
17+
throw new Error(`no ${PRAGMA} found in ${content}`);
18+
}
19+
return JSON.parse(Buffer.from(sourcemapLine.slice(PRAGMA.length + DATA.length), 'base64'));
20+
}
21+
22+
function readSourceMap(...f) {
23+
return JSON.parse(fs.readFileSync(require.resolve(path.join(__dirname, ...f))));
24+
}
25+
26+
function find(text, s = 'ts1') {
27+
const lines = text.split(/\r?\n/);
28+
for (let line = 1; line <= lines.length; line++) {
29+
const column = lines[line - 1].indexOf(s);
30+
if (column >= 0) {
31+
return {line, column};
32+
}
33+
}
34+
}
35+
36+
function asserts(pos) {
37+
// This doesn't work because the output dir is different from input
38+
// so it actually starts with a bunch of '/../..'
39+
// expect(pos.source).toBe('index.js');
40+
41+
expect(pos.source.endsWith('index.js')).toBeTruthy();
42+
expect(pos.line).toBe(6); // one-based
43+
expect(pos.column).toBe(20); // zero-based
44+
}
45+
46+
describe('application sourcemaps in the browser', () => {
47+
it('should work after rollup', async () => {
48+
const content = read('app_chunks', 'index.js');
49+
const rawSourceMap = parseSourceMap(content);
50+
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
51+
asserts(consumer.originalPositionFor(find(content)));
52+
});
53+
});
54+
55+
it('should work after terser', async () => {
56+
const content = read('app_chunks.min', 'index.js');
57+
const rawSourceMap = readSourceMap('app_chunks.min', 'index.js.map');
58+
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
59+
asserts(consumer.originalPositionFor(find(content)));
60+
});
61+
});
62+
63+
it('should work after babel', async () => {
64+
const content = read('app_chunks_es5', 'index.js');
65+
const rawSourceMap = parseSourceMap(content);
66+
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
67+
asserts(consumer.originalPositionFor(find(content)));
68+
});
69+
});
70+
71+
it('should work after babel+terser', async () => {
72+
const content = read('app_chunks_es5.min', 'index.js');
73+
const rawSourceMap = readSourceMap('app_chunks_es5.min', 'index.js.map');
74+
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
75+
asserts(consumer.originalPositionFor(find(content)));
76+
});
77+
});
78+
});

packages/rollup/src/rollup_bundle.bzl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ Otherwise, the outputs are assumed to be a single file.
148148
cfg = "host",
149149
default = "@npm//rollup/bin:rollup",
150150
),
151-
"sourcemap": attr.bool(
152-
doc = """Whether to produce a .js.map output
151+
"sourcemap": attr.string(
152+
doc = """Whether to produce sourcemaps.
153153
154154
Passed to the [`--sourcemap` option](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#outputsourcemap") in Rollup
155155
""",
156-
default = True,
156+
default = "inline",
157+
values = ["inline", "true", "false"],
157158
),
158159
"deps": attr.label_list(
159160
aspects = [module_mappings_aspect],
@@ -218,7 +219,7 @@ def _rollup_outs(sourcemap, name, entry_point, entry_points, output_dir):
218219
fail("Multiple entry points require that output_dir be set")
219220
out = entry_point_outs[0]
220221
result[out] = out + ".js"
221-
if sourcemap:
222+
if sourcemap == "true":
222223
result[out + "_map"] = "%s.map" % result[out]
223224
return result
224225

@@ -273,8 +274,8 @@ def _rollup_bundle(ctx):
273274
# where the link is.
274275
args.add("--preserveSymlinks")
275276

276-
if (ctx.attr.sourcemap):
277-
args.add("--sourcemap")
277+
if (ctx.attr.sourcemap and ctx.attr.sourcemap != "false"):
278+
args.add_all(["--sourcemap", ctx.attr.sourcemap])
278279

279280
if ctx.attr.globals:
280281
args.add("--external")

packages/rollup/test/globals/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ rollup_bundle(
66
entry_point = "input.js",
77
format = "umd",
88
globals = {"some_global_var": "runtime_name_of_global_var"},
9-
sourcemap = False,
9+
sourcemap = "false",
1010
)
1111

1212
golden_file_test(

packages/rollup/test/integration_e2e_rollup/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load("@npm_bazel_rollup//:index.from_src.bzl", "rollup_bundle")
99
entry_point = "foo.js",
1010
format = format,
1111
globals = {"some_global_var": "runtime_name_of_global_var"},
12+
sourcemap = "true",
1213
deps = [
1314
"//%s/fum:fumlib" % package_name(),
1415
"@npm//hello",

0 commit comments

Comments
 (0)