Skip to content

Commit

Permalink
feat: add default_ext
Browse files Browse the repository at this point in the history
Close #252
  • Loading branch information
davidaghassi authored and jbedard committed Dec 5, 2024
1 parent 093f69b commit 72d22eb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
9 changes: 6 additions & 3 deletions docs/swc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions swc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ for example to set your own output labels for `js_outs`.
toolchains = _swc_lib.toolchains,
)

def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcrc = None, source_maps = False, out_dir = None, root_dir = None, **kwargs):
def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcrc = None, source_maps = False, out_dir = None, root_dir = None, default_ext = ".js", **kwargs):
"""Execute the SWC compiler
Args:
Expand Down Expand Up @@ -70,6 +70,8 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
root_dir: A subdirectory under the input package which should be considered the root directory of all the input files
default_ext: The default extension to use for output files. If not set, the default is ".js".
**kwargs: additional keyword arguments passed through to underlying [`swc_compile`](#swc_compile), eg. `visibility`, `tags`
"""
if not types.is_list(srcs):
Expand Down Expand Up @@ -103,8 +105,8 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
dts_outs = []

if not output_dir:
js_outs = _swc_lib.calculate_js_outs(srcs, out_dir, root_dir)
map_outs = _swc_lib.calculate_map_outs(srcs, source_maps, out_dir, root_dir)
js_outs = _swc_lib.calculate_js_outs(default_ext, srcs, out_dir, root_dir)
map_outs = _swc_lib.calculate_map_outs(default_ext, srcs, source_maps, out_dir, root_dir)
dts_outs = _swc_lib.calculate_dts_outs(srcs, kwargs.get("emit_isolated_dts", False), out_dir, root_dir)

swc_compile(
Expand All @@ -121,6 +123,7 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
swcrc = swcrc,
out_dir = out_dir,
root_dir = root_dir,
default_ext = default_ext,
**kwargs
)

Expand Down
38 changes: 27 additions & 11 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ EXPERIMENTAL: this API is undocumented, experimental and may change without noti
""",
default = False,
),
"default_ext": attr.string(
doc = """Default extension for output files.
If a source file does not indicate a specific module type, this extension is used.
If unset, extensions will be determined based on the `js_outs` outputs attribute
or source file extensions.""",
),
}

_outputs = {
Expand Down Expand Up @@ -125,7 +133,7 @@ def _remove_extension(f):
i = f.rfind(".")
return f if i <= 0 else f[:-(len(f) - i)]

def _to_js_out(src, out_dir, root_dir, js_outs = []):
def _to_js_out(default_ext, src, out_dir, root_dir, js_outs = []):
if not _is_supported_src(src) or _is_typings_src(src):
return None

Expand All @@ -136,9 +144,17 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
".cts": ".cjs",
}
ext_index = src.rindex(".")
js_out = src[:ext_index] + exts.get(src[ext_index:], ".js")
js_out = _to_out_path(js_out, out_dir, root_dir)
js_out_base = src[:ext_index]
js_out_ext = exts.get(src[ext_index:], default_ext if default_ext else ".js")
js_out = _to_out_path(js_out_base + js_out_ext, out_dir, root_dir)

# If a default extension was specified then use js_out with the defaults
if default_ext:
return js_out

# If no default_ext was provided allow customizing the output extension via js_outs.
# See https://github.com/aspect-build/rules_swc/commit/edc6421cf42a7174bcc38e91b0812abd0bfb0f09
# TODO(3.0): remove this feature in favour of standard logic above.
alt_js_out = None

# Check if a custom out was requested with a potentially different extension
Expand All @@ -156,15 +172,15 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
# 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):
def _calculate_js_outs(default_ext, srcs, out_dir = None, root_dir = None):
out = []
for f in srcs:
js_out = _to_js_out(f, out_dir, root_dir)
js_out = _to_js_out(default_ext, f, out_dir, root_dir)
if js_out and js_out != f:
out.append(js_out)
return out

def _to_map_out(src, source_maps, out_dir, root_dir):
def _to_map_out(default_ext, src, source_maps, out_dir, root_dir):
if source_maps == "false" or source_maps == "inline":
return None
if not _is_supported_src(src) or _is_typings_src(src):
Expand All @@ -176,17 +192,17 @@ def _to_map_out(src, source_maps, out_dir, root_dir):
".cjs": ".cjs.map",
}
ext_index = src.rindex(".")
map_out = src[:ext_index] + exts.get(src[ext_index:], ".js.map")
map_out = src[:ext_index] + exts.get(src[ext_index:], default_ext + ".map")
map_out = _to_out_path(map_out, out_dir, root_dir)
return map_out

def _calculate_map_outs(srcs, source_maps, out_dir, root_dir):
def _calculate_map_outs(default_ext, srcs, source_maps, out_dir, root_dir):
if source_maps == "false" or source_maps == "inline":
return []

out = []
for f in srcs:
map_out = _to_map_out(f, source_maps, out_dir, root_dir)
map_out = _to_map_out(default_ext, f, source_maps, out_dir, root_dir)
if map_out:
out.append(map_out)
return out
Expand Down Expand Up @@ -343,14 +359,14 @@ def _swc_impl(ctx):
output_sources.append(src)
continue

js_out_path = _to_js_out(src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
js_out_path = _to_js_out(ctx.attr.default_ext, src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
if not js_out_path:
# This source file is not a supported src
continue
js_out = ctx.actions.declare_file(js_out_path)
outputs = [js_out]

map_out_path = _to_map_out(src_path, ctx.attr.source_maps, ctx.attr.out_dir, ctx.attr.root_dir)
map_out_path = _to_map_out(ctx.attr.default_ext, src_path, ctx.attr.source_maps, ctx.attr.out_dir, ctx.attr.root_dir)
if map_out_path:
js_map_out = ctx.actions.declare_file(map_out_path)
outputs.append(js_map_out)
Expand Down

0 comments on commit 72d22eb

Please sign in to comment.