Skip to content

Commit

Permalink
refactor: align js file extension logic with rules_ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Dec 4, 2024
1 parent ef6719b commit 0fc9df4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 8 deletions.
26 changes: 24 additions & 2 deletions examples/allow_js/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand All @@ -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",
},
)
1 change: 1 addition & 0 deletions examples/allow_js/expected.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.a = "simple";
1 change: 1 addition & 0 deletions examples/allow_js/expected.jsx
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/expected.mjs
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.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.a = "simple";
1 change: 1 addition & 0 deletions examples/allow_js/in.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string;
1 change: 1 addition & 0 deletions examples/allow_js/in.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = "simple";
1 change: 1 addition & 0 deletions examples/allow_js/in.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = "simple";
23 changes: 17 additions & 6 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,
)

0 comments on commit 0fc9df4

Please sign in to comment.