Skip to content

Commit

Permalink
feat: add default_ext
Browse files Browse the repository at this point in the history
  • Loading branch information
davidaghassi committed Nov 16, 2024
1 parent 9498a51 commit fa4b2fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
27 changes: 27 additions & 0 deletions 01_srcs_expand_variables.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl
index 386cb03..81f4c91 100644
--- a/swc/private/swc.bzl
+++ b/swc/private/swc.bzl
@@ -3,6 +3,7 @@
load("@aspect_bazel_lib//lib:platform_utils.bzl", "platform_utils")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "js_info")
+load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//swc:providers.bzl", "SwcPluginConfigInfo")

@@ -240,7 +241,13 @@ def _impl(ctx):
fail("Under output_dir, the srcs must be directories, not files")
output_dir = ctx.actions.declare_directory(ctx.attr.out_dir if ctx.attr.out_dir else ctx.label.name)

- inputs.extend(ctx.files.srcs)
+ inputs.extend([
+ " ".join([
+ expand_variables(ctx, exp, attribute_name = "srcs")
+ for exp in expand_locations(ctx, arg, ctx.attr.srcs).split(" ")
+ ])
+ for arg in ctx.attr.srcs
+ ])
inputs.extend(ctx.files.plugins)
inputs.extend(plugin_cache)

12 changes: 9 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 = None, **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 @@ -97,13 +99,16 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
elif source_maps == False:
source_maps = "false"

if not default_ext:
default_ext = ".js"

# Determine js & map outputs
js_outs = []
map_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)

swc_compile(
name = name,
Expand All @@ -118,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
24 changes: 14 additions & 10 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more
"root_dir": attr.string(
doc = "a subdirectory under the input package which should be consider the root directory of all the input files",
),
"default_ext": attr.string(
doc = "default extension for output files",
default = ".js",
),
}

_outputs = {
Expand Down Expand Up @@ -127,12 +131,12 @@ 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):
return None

exts = {
"*": ".js",
"*": default_ext,
".mts": ".mjs",
".mjs": ".mjs",
".cjs": ".cjs",
Expand All @@ -149,21 +153,21 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
return maybe_out
return 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):
return None
exts = {
"*": ".js.map",
"*": default_ext + ".map",
".mts": ".mjs.map",
".cts": ".cjs.map",
".mjs": ".mjs.map",
Expand All @@ -173,13 +177,13 @@ def _to_map_out(src, source_maps, out_dir, root_dir):
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 @@ -300,13 +304,13 @@ def _swc_impl(ctx):

src_path = _relative_to_package(src.path, ctx)

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)
Expand Down

0 comments on commit fa4b2fc

Please sign in to comment.