diff --git a/.prettierignore b/.prettierignore index 2fde4b5..5627bbe 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ docs/*.md **/expected.js +**/expected.cjs diff --git a/docs/swc.md b/docs/swc.md index 2b49512..c3d0bd5 100644 --- a/docs/swc.md +++ b/docs/swc.md @@ -34,8 +34,8 @@ for example to set your own output labels for `js_outs`. | name | A unique name for this target. | Name | required | | | args | additional arguments to pass to swc cli, see https://swc.rs/docs/usage/cli | List of strings | optional | [] | | data | runtime dependencies propagated to binaries that depend on this | List of labels | optional | [] | -| js_outs | list of expected JavaScript output files | List of labels | optional | | -| map_outs | list of expected source map output files | List of labels | optional | | +| js_outs | list of expected JavaScript output files.

There must be one for each entry in srcs, and in the same order. | List of labels | optional | | +| map_outs | 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. | List of labels | optional | | | output_dir | whether to produce a directory output rather than individual files | Boolean | optional | False | | srcs | source files, typically .ts files in the source tree | List of labels | required | | | swc_cli | binary that executes the swc CLI | Label | optional | @aspect_rules_swc//swc:cli | diff --git a/examples/custom_outs/BUILD.bazel b/examples/custom_outs/BUILD.bazel new file mode 100644 index 0000000..c3e4438 --- /dev/null +++ b/examples/custom_outs/BUILD.bazel @@ -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", +) diff --git a/examples/custom_outs/expected.cjs b/examples/custom_outs/expected.cjs new file mode 100755 index 0000000..87da5ca --- /dev/null +++ b/examples/custom_outs/expected.cjs @@ -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 \ No newline at end of file diff --git a/examples/custom_outs/expected.js b/examples/custom_outs/expected.js new file mode 100644 index 0000000..d536c6b --- /dev/null +++ b/examples/custom_outs/expected.js @@ -0,0 +1,4 @@ +export var a = "a"; + + +//# sourceMappingURL=out.js.map \ No newline at end of file diff --git a/examples/custom_outs/in.ts b/examples/custom_outs/in.ts new file mode 100644 index 0000000..f6c1fdd --- /dev/null +++ b/examples/custom_outs/in.ts @@ -0,0 +1 @@ +export const a: string = "a"; diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl index a8aa847..67f8845 100644 --- a/swc/private/swc.bzl +++ b/swc/private/swc.bzl @@ -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): @@ -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: