Skip to content

Commit

Permalink
fix: support .js inputs without out_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Feb 28, 2024
1 parent d30c95f commit 140c6d2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
19 changes: 19 additions & 0 deletions examples/allow_js/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Simple use case for swc: transpiling .js using the `swc` rule, similar to tsc with 'allowJs'
"""

load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_swc//swc:defs.bzl", "swc")

# Runs `swc in.js > ../../bazel-bin/examples/js_outs/in.js`
swc(
name = "compile",
srcs = ["in.js"],
)

# Assert that the output of "compile" rule matches the expected file.
write_source_files(
name = "test",
# There is no pre-declared output of the "compile" rule because the input is .js
# so the the target name is used instead of output file.
files = {"expected.js": ":compile"},
)
1 change: 1 addition & 0 deletions examples/allow_js/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var a = "simple";
1 change: 1 addition & 0 deletions examples/allow_js/in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = "simple";
10 changes: 1 addition & 9 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,10 @@ def _calculate_js_out(src, out_dir, root_dir, js_outs = []):
return js_out

def _calculate_js_outs(srcs, out_dir, root_dir):
if out_dir == None:
js_srcs = []
for src in srcs:
if src.endswith(".js"):
js_srcs.append(src)
if len(js_srcs) > 0:
fail("Detected swc rule with srcs=[{}, ...] and out_dir=None. Please set out_dir when compiling .js files.".format(", ".join(js_srcs[:3])))

out = []
for f in srcs:
js_out = _calculate_js_out(f, out_dir, root_dir)
if js_out:
if js_out and js_out != f:
out.append(js_out)
return out

Expand Down

0 comments on commit 140c6d2

Please sign in to comment.