From fa4b2fc3715eb0a2125847e5373e0334f451562c Mon Sep 17 00:00:00 2001 From: davidaghassi Date: Sat, 16 Nov 2024 08:26:34 -0700 Subject: [PATCH] feat: add default_ext Addresses #252 --- 01_srcs_expand_variables.patch | 27 +++++++++++++++++++++++++++ swc/defs.bzl | 12 +++++++++--- swc/private/swc.bzl | 24 ++++++++++++++---------- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 01_srcs_expand_variables.patch diff --git a/01_srcs_expand_variables.patch b/01_srcs_expand_variables.patch new file mode 100644 index 0000000..8d4924d --- /dev/null +++ b/01_srcs_expand_variables.patch @@ -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) + diff --git a/swc/defs.bzl b/swc/defs.bzl index a15e7ad..aa73a34 100644 --- a/swc/defs.bzl +++ b/swc/defs.bzl @@ -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: @@ -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): @@ -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, @@ -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 ) diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl index 3331bdd..dc82ce6 100644 --- a/swc/private/swc.bzl +++ b/swc/private/swc.bzl @@ -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 = { @@ -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", @@ -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", @@ -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 @@ -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)