Skip to content

Commit 72d22eb

Browse files
davidaghassijbedard
authored andcommitted
feat: add default_ext
Close #252
1 parent 093f69b commit 72d22eb

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

docs/swc.md

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swc/defs.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ for example to set your own output labels for `js_outs`.
3232
toolchains = _swc_lib.toolchains,
3333
)
3434

35-
def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcrc = None, source_maps = False, out_dir = None, root_dir = None, **kwargs):
35+
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):
3636
"""Execute the SWC compiler
3737
3838
Args:
@@ -70,6 +70,8 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
7070
7171
root_dir: A subdirectory under the input package which should be considered the root directory of all the input files
7272
73+
default_ext: The default extension to use for output files. If not set, the default is ".js".
74+
7375
**kwargs: additional keyword arguments passed through to underlying [`swc_compile`](#swc_compile), eg. `visibility`, `tags`
7476
"""
7577
if not types.is_list(srcs):
@@ -103,8 +105,8 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
103105
dts_outs = []
104106

105107
if not output_dir:
106-
js_outs = _swc_lib.calculate_js_outs(srcs, out_dir, root_dir)
107-
map_outs = _swc_lib.calculate_map_outs(srcs, source_maps, out_dir, root_dir)
108+
js_outs = _swc_lib.calculate_js_outs(default_ext, srcs, out_dir, root_dir)
109+
map_outs = _swc_lib.calculate_map_outs(default_ext, srcs, source_maps, out_dir, root_dir)
108110
dts_outs = _swc_lib.calculate_dts_outs(srcs, kwargs.get("emit_isolated_dts", False), out_dir, root_dir)
109111

110112
swc_compile(
@@ -121,6 +123,7 @@ def swc(name, srcs, args = [], data = [], plugins = [], output_dir = False, swcr
121123
swcrc = swcrc,
122124
out_dir = out_dir,
123125
root_dir = root_dir,
126+
default_ext = default_ext,
124127
**kwargs
125128
)
126129

swc/private/swc.bzl

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ EXPERIMENTAL: this API is undocumented, experimental and may change without noti
7373
""",
7474
default = False,
7575
),
76+
"default_ext": attr.string(
77+
doc = """Default extension for output files.
78+
79+
If a source file does not indicate a specific module type, this extension is used.
80+
81+
If unset, extensions will be determined based on the `js_outs` outputs attribute
82+
or source file extensions.""",
83+
),
7684
}
7785

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

128-
def _to_js_out(src, out_dir, root_dir, js_outs = []):
136+
def _to_js_out(default_ext, src, out_dir, root_dir, js_outs = []):
129137
if not _is_supported_src(src) or _is_typings_src(src):
130138
return None
131139

@@ -136,9 +144,17 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
136144
".cts": ".cjs",
137145
}
138146
ext_index = src.rindex(".")
139-
js_out = src[:ext_index] + exts.get(src[ext_index:], ".js")
140-
js_out = _to_out_path(js_out, out_dir, root_dir)
147+
js_out_base = src[:ext_index]
148+
js_out_ext = exts.get(src[ext_index:], default_ext if default_ext else ".js")
149+
js_out = _to_out_path(js_out_base + js_out_ext, out_dir, root_dir)
150+
151+
# If a default extension was specified then use js_out with the defaults
152+
if default_ext:
153+
return js_out
141154

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

144160
# Check if a custom out was requested with a potentially different extension
@@ -156,15 +172,15 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
156172
# Return the matched custom out if it exists otherwise fallback to the default
157173
return alt_js_out or js_out
158174

159-
def _calculate_js_outs(srcs, out_dir, root_dir):
175+
def _calculate_js_outs(default_ext, srcs, out_dir = None, root_dir = None):
160176
out = []
161177
for f in srcs:
162-
js_out = _to_js_out(f, out_dir, root_dir)
178+
js_out = _to_js_out(default_ext, f, out_dir, root_dir)
163179
if js_out and js_out != f:
164180
out.append(js_out)
165181
return out
166182

167-
def _to_map_out(src, source_maps, out_dir, root_dir):
183+
def _to_map_out(default_ext, src, source_maps, out_dir, root_dir):
168184
if source_maps == "false" or source_maps == "inline":
169185
return None
170186
if not _is_supported_src(src) or _is_typings_src(src):
@@ -176,17 +192,17 @@ def _to_map_out(src, source_maps, out_dir, root_dir):
176192
".cjs": ".cjs.map",
177193
}
178194
ext_index = src.rindex(".")
179-
map_out = src[:ext_index] + exts.get(src[ext_index:], ".js.map")
195+
map_out = src[:ext_index] + exts.get(src[ext_index:], default_ext + ".map")
180196
map_out = _to_out_path(map_out, out_dir, root_dir)
181197
return map_out
182198

183-
def _calculate_map_outs(srcs, source_maps, out_dir, root_dir):
199+
def _calculate_map_outs(default_ext, srcs, source_maps, out_dir, root_dir):
184200
if source_maps == "false" or source_maps == "inline":
185201
return []
186202

187203
out = []
188204
for f in srcs:
189-
map_out = _to_map_out(f, source_maps, out_dir, root_dir)
205+
map_out = _to_map_out(default_ext, f, source_maps, out_dir, root_dir)
190206
if map_out:
191207
out.append(map_out)
192208
return out
@@ -343,14 +359,14 @@ def _swc_impl(ctx):
343359
output_sources.append(src)
344360
continue
345361

346-
js_out_path = _to_js_out(src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
362+
js_out_path = _to_js_out(ctx.attr.default_ext, src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
347363
if not js_out_path:
348364
# This source file is not a supported src
349365
continue
350366
js_out = ctx.actions.declare_file(js_out_path)
351367
outputs = [js_out]
352368

353-
map_out_path = _to_map_out(src_path, ctx.attr.source_maps, ctx.attr.out_dir, ctx.attr.root_dir)
369+
map_out_path = _to_map_out(ctx.attr.default_ext, src_path, ctx.attr.source_maps, ctx.attr.out_dir, ctx.attr.root_dir)
354370
if map_out_path:
355371
js_map_out = ctx.actions.declare_file(map_out_path)
356372
outputs.append(js_map_out)

0 commit comments

Comments
 (0)