@@ -73,6 +73,14 @@ EXPERIMENTAL: this API is undocumented, experimental and may change without noti
73
73
""" ,
74
74
default = False ,
75
75
),
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
+ ),
76
84
}
77
85
78
86
_outputs = {
@@ -125,7 +133,7 @@ def _remove_extension(f):
125
133
i = f .rfind ("." )
126
134
return f if i <= 0 else f [:- (len (f ) - i )]
127
135
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 = []):
129
137
if not _is_supported_src (src ) or _is_typings_src (src ):
130
138
return None
131
139
@@ -136,9 +144,17 @@ def _to_js_out(src, out_dir, root_dir, js_outs = []):
136
144
".cts" : ".cjs" ,
137
145
}
138
146
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
141
154
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.
142
158
alt_js_out = None
143
159
144
160
# 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 = []):
156
172
# Return the matched custom out if it exists otherwise fallback to the default
157
173
return alt_js_out or js_out
158
174
159
- def _calculate_js_outs (srcs , out_dir , root_dir ):
175
+ def _calculate_js_outs (default_ext , srcs , out_dir = None , root_dir = None ):
160
176
out = []
161
177
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 )
163
179
if js_out and js_out != f :
164
180
out .append (js_out )
165
181
return out
166
182
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 ):
168
184
if source_maps == "false" or source_maps == "inline" :
169
185
return None
170
186
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):
176
192
".cjs" : ".cjs.map" ,
177
193
}
178
194
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" )
180
196
map_out = _to_out_path (map_out , out_dir , root_dir )
181
197
return map_out
182
198
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 ):
184
200
if source_maps == "false" or source_maps == "inline" :
185
201
return []
186
202
187
203
out = []
188
204
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 )
190
206
if map_out :
191
207
out .append (map_out )
192
208
return out
@@ -343,14 +359,14 @@ def _swc_impl(ctx):
343
359
output_sources .append (src )
344
360
continue
345
361
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 )
347
363
if not js_out_path :
348
364
# This source file is not a supported src
349
365
continue
350
366
js_out = ctx .actions .declare_file (js_out_path )
351
367
outputs = [js_out ]
352
368
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 )
354
370
if map_out_path :
355
371
js_map_out = ctx .actions .declare_file (map_out_path )
356
372
outputs .append (js_map_out )
0 commit comments