diff --git a/examples/allow_js/BUILD.bazel b/examples/allow_js/BUILD.bazel index b335d09..8d4006c 100644 --- a/examples/allow_js/BUILD.bazel +++ b/examples/allow_js/BUILD.bazel @@ -6,7 +6,7 @@ load("@aspect_rules_swc//swc:defs.bzl", "swc") # Runs `swc in.js > ../../bazel-bin/examples/js_outs/in.js` swc( - name = "compile", + name = "compile-js", srcs = ["in.js"], ) @@ -15,5 +15,27 @@ 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"}, + files = {"expected.js": ":compile-js"}, +) + +# Runs swc on all js files +swc( + name = "compile-js-exts", + srcs = [ + "in.cjs", + "in.d.ts", + "in.jsx", + "in.mjs", + ], + out_dir = "js_outs", +) + +# Assert the extensions and output of each js file type via pre-declared outputs. +write_source_files( + name = "test-x", + files = { + "expected.jsx": "js_outs/in.js", + "expected.mjs": "js_outs/in.mjs", + "expected.cjs": "js_outs/in.cjs", + }, ) diff --git a/examples/allow_js/expected.cjs b/examples/allow_js/expected.cjs new file mode 100644 index 0000000..e46a8c2 --- /dev/null +++ b/examples/allow_js/expected.cjs @@ -0,0 +1 @@ +exports.a = "simple"; diff --git a/examples/allow_js/expected.jsx b/examples/allow_js/expected.jsx new file mode 100644 index 0000000..54732d1 --- /dev/null +++ b/examples/allow_js/expected.jsx @@ -0,0 +1 @@ +export var a = "simple"; diff --git a/examples/allow_js/expected.mjs b/examples/allow_js/expected.mjs new file mode 100644 index 0000000..54732d1 --- /dev/null +++ b/examples/allow_js/expected.mjs @@ -0,0 +1 @@ +export var a = "simple"; diff --git a/examples/allow_js/in.cjs b/examples/allow_js/in.cjs new file mode 100644 index 0000000..e46a8c2 --- /dev/null +++ b/examples/allow_js/in.cjs @@ -0,0 +1 @@ +exports.a = "simple"; diff --git a/examples/allow_js/in.d.ts b/examples/allow_js/in.d.ts new file mode 100644 index 0000000..425b80f --- /dev/null +++ b/examples/allow_js/in.d.ts @@ -0,0 +1 @@ +export const a: string; diff --git a/examples/allow_js/in.jsx b/examples/allow_js/in.jsx new file mode 100644 index 0000000..23bdc29 --- /dev/null +++ b/examples/allow_js/in.jsx @@ -0,0 +1 @@ +export const a = "simple"; diff --git a/examples/allow_js/in.mjs b/examples/allow_js/in.mjs new file mode 100644 index 0000000..23bdc29 --- /dev/null +++ b/examples/allow_js/in.mjs @@ -0,0 +1 @@ +export const a = "simple"; diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl index 6082164..4479d81 100644 --- a/swc/private/swc.bzl +++ b/swc/private/swc.bzl @@ -78,11 +78,14 @@ Can be empty, meaning no source maps should be produced. If non-empty, there should be one for each entry in srcs."""), } -_SUPPORTED_EXTENSIONS = [".ts", ".mts", ".cts", ".tsx", ".jsx", ".mjs", ".cjs", ".js"] +def _is_ts_src(src): + return src.endswith(".ts") or src.endswith(".mts") or src.endswith(".cts") or src.endswith(".tsx") or src.endswith(".jsx") + +def _is_js_src(src): + return src.endswith(".mjs") or src.endswith(".cjs") or src.endswith(".js") def _is_supported_src(src): - i = src.rfind(".") - return i > 0 and src[i:] in _SUPPORTED_EXTENSIONS + return _is_ts_src(src) or _is_js_src(src) # TODO: vendored from rules_ts - aspect_bazel_lib should provide this? # https://github.com/aspect-build/rules_ts/blob/v3.2.1/ts/private/ts_lib.bzl#L194-L200 @@ -122,13 +125,22 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []): js_out = src[:ext_index] + exts.get(src[ext_index:], ".js") js_out = _to_out_path(js_out, out_dir, root_dir) + alt_js_out = None + # Check if a custom out was requested with a potentially different extension no_ext = _remove_extension(js_out) for maybe_out in js_outs: + # Always use an exact match if it exists + if maybe_out == js_out: + return js_out + + # Try to match on a potential output with a different extension # Initial startswith() check to avoid the expensive _remove_extension() if maybe_out.startswith(no_ext) and no_ext == _remove_extension(maybe_out): - return maybe_out - return js_out + alt_js_out = maybe_out + + # Return the matched custom out if it exists otherwise fallback to the default + return alt_js_out or js_out def _calculate_js_outs(srcs, out_dir, root_dir): out = [] @@ -363,7 +375,6 @@ swc = struct( implementation = _swc_impl, attrs = dict(_attrs, **_outputs), toolchains = ["@aspect_rules_swc//swc:toolchain_type"], - SUPPORTED_EXTENSIONS = _SUPPORTED_EXTENSIONS, calculate_js_outs = _calculate_js_outs, calculate_map_outs = _calculate_map_outs, )