Skip to content

Commit

Permalink
fix: set sourcemap root to the workspace relative root_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Mar 1, 2023
1 parent d99e862 commit 2773de5
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 23 deletions.
1 change: 1 addition & 0 deletions examples/filegroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ filegroup(
srcs = [
"a.ts",
"b.ts",
"sub/c.ts",
],
)

Expand Down
13 changes: 10 additions & 3 deletions examples/filegroup/check_outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ cd "$TEST_SRCDIR/$TEST_WORKSPACE/$(dirname $TEST_TARGET)"

grep "export var a" filegroup/a.js
grep "sourceMappingURL=a.js.map" filegroup/a.js
grep --fixed-strings '"sourceRoot":"examples/filegroup"' filegroup/a.js.map
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/a.js.map
grep --fixed-strings '"sources":["a.ts"]' filegroup/a.js.map

grep "export var b" filegroup/b.js
grep "sourceMappingURL=b.js.map" filegroup/b.js
grep --fixed-strings '"sourceRoot":"examples/filegroup"' filegroup/b.js.map
grep --fixed-strings '"sources":["b.ts"]' filegroup/b.js.map
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/b.js.map
grep --fixed-strings '"sources":["b.ts"]' filegroup/b.js.map

grep "export var c" filegroup/sub/c.js
grep "sourceMappingURL=c.js.map" filegroup/sub/c.js
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/sub/c.js.map
grep --fixed-strings '"sources":["sub/c.ts"]' filegroup/sub/c.js.map

echo "SUCCESS"
1 change: 1 addition & 0 deletions examples/filegroup/sub/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c: string = "c";
2 changes: 1 addition & 1 deletion examples/rc/src/expected.js.map

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

28 changes: 13 additions & 15 deletions examples/root_dir/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_swc//swc:defs.bzl", "swc")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")

swc(
name = "compile",
srcs = [
"src/a.ts",
"src/b.ts",
"src/sub/c.ts",
],
out_dir = "out",
root_dir = "src",
source_maps = True,
)

# Since the srcs were in a filegroup, the swc macro cannot pre-declare the outputs.
# So there is no label ":a.js" that we can reference from the build file.
# However, a.js is still produced as one of the default outputs of the compile rule.
# We can verify this in an action that depends on the ":compile" rule and reads the files.
assert_outputs(
name = "check_outputs",
actual = "compile",
expected = [
"examples/root_dir/out/a.js",
"examples/root_dir/out/a.js.map",
"examples/root_dir/out/b.js",
"examples/root_dir/out/b.js.map",
],
)
write_source_files(
name = "test",
files = {
"expected/a.js": ":out/a.js",
"expected/a.js.map": ":out/a.js.map",
"expected/b.js": ":out/b.js",
"expected/b.js.map": ":out/b.js.map",
"expected/sub/c.js": ":out/sub/c.js",
"expected/sub/c.js.map": ":out/sub/c.js.map",
},
)
3 changes: 3 additions & 0 deletions examples/root_dir/expected/a.js

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

1 change: 1 addition & 0 deletions examples/root_dir/expected/a.js.map

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

3 changes: 3 additions & 0 deletions examples/root_dir/expected/b.js

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

1 change: 1 addition & 0 deletions examples/root_dir/expected/b.js.map

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

3 changes: 3 additions & 0 deletions examples/root_dir/expected/sub/c.js

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

1 change: 1 addition & 0 deletions examples/root_dir/expected/sub/c.js.map

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

1 change: 1 addition & 0 deletions examples/root_dir/src/sub/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c: string = "c";
13 changes: 9 additions & 4 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _relative_to_package(path, ctx):
return path

def _strip_root_dir(path, root_dir):
if not root_dir:
return path

replace_pattern = root_dir + "/"
if path.startswith("./"):
path = path[len("./"):]
Expand Down Expand Up @@ -244,13 +247,15 @@ def _impl(ctx):
else:
output_sources = []

for src in ctx.files.srcs:
src_args = ctx.actions.args()
src_args.add("--source-file-name", src.basename)
src_args.add("--source-root", src.dirname)
root_dir = paths.join(ctx.label.package, ctx.attr.root_dir)

for src in ctx.files.srcs:
src_path = _relative_to_package(src.path, ctx)

src_args = ctx.actions.args()
src_args.add("--source-file-name", _strip_root_dir(src_path, ctx.attr.root_dir))
src_args.add("--source-root", root_dir)

js_out_path = _calculate_js_out(src_path, ctx.attr.out_dir, ctx.attr.root_dir, [_relative_to_package(f.path, ctx) for f in ctx.outputs.js_outs])
if not js_out_path:
# This source file is not a supported src
Expand Down

0 comments on commit 2773de5

Please sign in to comment.