Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default_ext #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 14 additions & 8 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 @@ -108,11 +112,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 = {
"*": default_ext,
".mts": ".mjs",
".mjs": ".mjs",
".cjs": ".cjs",
Expand All @@ -130,20 +135,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 = {
"*": default_ext + ".map",
".mts": ".mjs.map",
".cts": ".cjs.map",
".mjs": ".mjs.map",
Expand All @@ -154,13 +160,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 @@ -284,13 +290,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
Loading