Skip to content

Commit

Permalink
fix: allow user to choose any output path for outputs, such as custom…
Browse files Browse the repository at this point in the history
… extension or subdirectory
  • Loading branch information
alexeagle committed Dec 22, 2021
1 parent 3b050db commit 219d173
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs/*.md
**/expected.js
**/expected.cjs
4 changes: 2 additions & 2 deletions docs/swc.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ for example to set your own output labels for `js_outs`.
| <a id="swc_rule-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="swc_rule-args"></a>args | additional arguments to pass to swc cli, see https://swc.rs/docs/usage/cli | List of strings | optional | [] |
| <a id="swc_rule-data"></a>data | runtime dependencies propagated to binaries that depend on this | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="swc_rule-js_outs"></a>js_outs | list of expected JavaScript output files | List of labels | optional | |
| <a id="swc_rule-map_outs"></a>map_outs | list of expected source map output files | List of labels | optional | |
| <a id="swc_rule-js_outs"></a>js_outs | list of expected JavaScript output files.<br><br>There must be one for each entry in srcs, and in the same order. | List of labels | optional | |
| <a id="swc_rule-map_outs"></a>map_outs | list of expected source map output files.<br><br>Can be empty, meaning no source maps should be produced. If non-empty, there must be one for each entry in srcs, and in the same order. | List of labels | optional | |
| <a id="swc_rule-output_dir"></a>output_dir | whether to produce a directory output rather than individual files | Boolean | optional | False |
| <a id="swc_rule-srcs"></a>srcs | source files, typically .ts files in the source tree | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
| <a id="swc_rule-swc_cli"></a>swc_cli | binary that executes the swc CLI | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | @aspect_rules_swc//swc:cli |
Expand Down
30 changes: 30 additions & 0 deletions examples/custom_outs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@aspect_rules_swc//swc:swc.bzl", swc = "swc_rule")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

[
swc(
name = "transpile_" + format,
srcs = ["in.ts"],
args = [
"-C",
"module.type=" + format,
],
js_outs = [format + "/out." + ("cjs" if format == "commonjs" else "js")],
)
for format in [
"commonjs",
"es6",
]
]

diff_test(
name = "test_commonjs",
file1 = "commonjs/out.cjs",
file2 = "expected.cjs",
)

diff_test(
name = "test_es6",
file1 = "es6/out.js",
file2 = "expected.js",
)
10 changes: 10 additions & 0 deletions examples/custom_outs/expected.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = void 0;
var a = "a";
exports.a = a;


//# sourceMappingURL=out.cjs.map
4 changes: 4 additions & 0 deletions examples/custom_outs/expected.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/custom_outs/in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = "a";
15 changes: 10 additions & 5 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ _attrs = {
}

_outputs = {
"js_outs": attr.output_list(doc = "list of expected JavaScript output files"),
"map_outs": attr.output_list(doc = "list of expected source map output files"),
"js_outs": attr.output_list(doc = """list of expected JavaScript output files.
There must be one for each entry in srcs, and in the same order."""),
"map_outs": attr.output_list(doc = """list of expected source map output files.
Can be empty, meaning no source maps should be produced.
If non-empty, there must be one for each entry in srcs, and in the same order."""),
}

def _impl(ctx):
Expand Down Expand Up @@ -60,12 +65,12 @@ def _impl(ctx):
else:
outputs.extend(ctx.outputs.js_outs)
outputs.extend(ctx.outputs.map_outs)
for src in ctx.files.srcs:
js_out = ctx.actions.declare_file(paths.replace_extension(src.basename, ".js"), sibling = src)
for i, src in enumerate(ctx.files.srcs):
js_out = ctx.outputs.js_outs[i]
inputs = [src] + ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo.tool_files
outs = [js_out]
if source_maps:
outs.append(ctx.actions.declare_file(paths.replace_extension(src.basename, ".js.map"), sibling = src))
outs.append(ctx.outputs.map_outs[i])

# Pass in the swcrc config if it is set
if ctx.file.swcrc:
Expand Down

0 comments on commit 219d173

Please sign in to comment.