Skip to content

Commit

Permalink
fix: output .map files alongside .js within out_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Nov 10, 2022
1 parent eef380f commit f716962
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
27 changes: 27 additions & 0 deletions examples/out_dir/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@aspect_rules_swc//swc:defs.bzl", "swc")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")

swc(
name = "transpile",
srcs = [
"a.ts",
"b.ts",
],
out_dir = "out",
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 transpile rule.
# We can verify this in an action that depends on the ":transpile" rule and reads the files.
assert_outputs(
name = "check_outputs",
actual = "transpile",
expected = [
"examples/out_dir/out/a.js",
"examples/out_dir/out/a.js.map",
"examples/out_dir/out/b.js",
"examples/out_dir/out/b.js.map",
],
)
1 change: 1 addition & 0 deletions examples/out_dir/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = "a";
1 change: 1 addition & 0 deletions examples/out_dir/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b: string = "b";
11 changes: 11 additions & 0 deletions examples/out_dir/check_outputs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -o errexit

cd "$TEST_SRCDIR/$TEST_WORKSPACE/$(dirname $TEST_TARGET)"
grep "export var a" out_dir/out/a.js
grep "sourceMappingURL=a.js.map" out_dir/out/a.js
grep --fixed-strings '"sources":["../a.ts"]' out_dir/out/a.js.map

grep "export var b" out_dir/out/b.js
grep "sourceMappingURL=b.js.map" out_dir/out/b.js
grep --fixed-strings '"sources":["../b.ts"]' out_dir/out/b.js.map
2 changes: 1 addition & 1 deletion swc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def swc(name, srcs = None, args = [], data = [], output_dir = False, swcrc = Non

if not output_dir:
js_outs = _swc_lib.calculate_js_outs(srcs, out_dir)
map_outs = _swc_lib.calculate_map_outs(srcs, source_maps)
map_outs = _swc_lib.calculate_map_outs(srcs, out_dir, source_maps)

swc_transpiler(
name = name,
Expand Down
11 changes: 8 additions & 3 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ def _calculate_js_outs(srcs, out_dir = None):

return js_outs

def _calculate_map_outs(srcs, source_maps):
def _calculate_map_outs(srcs, out_dir, source_maps):
if source_maps in ["false", "inline"]:
return []
return [paths.replace_extension(f, ".js.map") for f in srcs if _is_supported_src(f)]

map_outs = [paths.replace_extension(f, ".js.map") for f in srcs if _is_supported_src(f)]
if out_dir != None:
map_outs = [paths.join(out_dir, f) for f in map_outs]

return map_outs

def _impl(ctx):
swcinfo = ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo
Expand Down Expand Up @@ -133,7 +138,7 @@ def _impl(ctx):
if len(ctx.attr.map_outs):
map_outs = ctx.outputs.map_outs
else:
map_outs = _declare_outputs(ctx, _calculate_map_outs(srcs, ctx.attr.source_maps))
map_outs = _declare_outputs(ctx, _calculate_map_outs(srcs, ctx.attr.out_dir, ctx.attr.source_maps))

output_sources = js_outs + map_outs

Expand Down

0 comments on commit f716962

Please sign in to comment.