diff --git a/examples/allow_js/BUILD.bazel b/examples/allow_js/BUILD.bazel new file mode 100644 index 0000000..b335d09 --- /dev/null +++ b/examples/allow_js/BUILD.bazel @@ -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"}, +) diff --git a/examples/allow_js/expected.js b/examples/allow_js/expected.js new file mode 100644 index 0000000..54732d1 --- /dev/null +++ b/examples/allow_js/expected.js @@ -0,0 +1 @@ +export var a = "simple"; diff --git a/examples/allow_js/in.js b/examples/allow_js/in.js new file mode 100644 index 0000000..23bdc29 --- /dev/null +++ b/examples/allow_js/in.js @@ -0,0 +1 @@ +export const a = "simple"; diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl index 945778d..ce180a1 100644 --- a/swc/private/swc.bzl +++ b/swc/private/swc.bzl @@ -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