diff --git a/docs/swc.md b/docs/swc.md index 863b1cf..a123e7b 100644 --- a/docs/swc.md +++ b/docs/swc.md @@ -20,7 +20,7 @@ swc(
 swc_compile(name, args, data, js_outs, map_outs, out_dir, output_dir, plugins, root_dir,
-            source_maps, srcs, swcrc)
+            source_maps, source_root, srcs, swcrc)
 
Underlying rule for the `swc` macro. @@ -46,7 +46,8 @@ for example to set your own output labels for `js_outs`. | output_dir | Whether to produce a directory output rather than individual files.

If out_dir is also specified, it is used as the name of the output directory. Otherwise, the directory is named the same as the target. | Boolean | optional | False | | plugins | swc compilation plugins, created with swc_plugin rule | List of labels | optional | [] | | root_dir | a subdirectory under the input package which should be consider the root directory of all the input files | String | optional | "" | -| source_maps | see https://swc.rs/docs/usage/cli#--source-maps--s | String | optional | "false" | +| source_maps | Create source map files for emitted JavaScript files.

see https://swc.rs/docs/usage/cli#--source-maps--s | String | optional | "false" | +| source_root | Specify the root path for debuggers to find the reference source code.

see https://swc.rs/docs/usage/cli#--source-root | String | optional | "" | | srcs | source files, typically .ts files in the source tree | List of labels | required | | | swcrc | label of a configuration file for swc, see https://swc.rs/docs/configuration/swcrc | Label | optional | None | diff --git a/examples/source_root/BUILD.bazel b/examples/source_root/BUILD.bazel new file mode 100644 index 0000000..135adbd --- /dev/null +++ b/examples/source_root/BUILD.bazel @@ -0,0 +1,39 @@ +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") +load("@aspect_rules_swc//swc:defs.bzl", "swc") + +# Specifies a custom location where a debugger should locate source files instead of relative source +# locations. This string is treated verbatim inside the source-map where you can use a path or a URL. +swc( + name = "compile", + srcs = ["in.ts"], + source_maps = True, + # The custom location can an URL + source_root = "https://my-website.com/debug/source/", +) + +swc( + name = "compile_subdir", + srcs = ["src/subdir.ts"], + root_dir = "src", + source_maps = True, + # The custom location can be a path + source_root = "../../../debug/source", +) + +# Assert that the output of "compile" rule matches the expected file. +write_source_files( + name = "test", + files = { + "expected.js": ":in.js", + "expected.js.map": ":in.js.map", + }, +) + +# Assert that the output of "compile_subdir" rule matches the expected file. +write_source_files( + name = "test_subdir", + files = { + "expected_subdir.js": ":subdir.js", + "expected_subdir.js.map": ":subdir.js.map", + }, +) diff --git a/examples/source_root/expected.js b/examples/source_root/expected.js new file mode 100644 index 0000000..4fef43f --- /dev/null +++ b/examples/source_root/expected.js @@ -0,0 +1,3 @@ +export var a = "foo"; + +//# sourceMappingURL=in.js.map \ No newline at end of file diff --git a/examples/source_root/expected.js.map b/examples/source_root/expected.js.map new file mode 100644 index 0000000..4b4d636 --- /dev/null +++ b/examples/source_root/expected.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["in.ts"],"sourceRoot":"https://my-website.com/debug/source/","sourcesContent":["export const a: string = \"foo\";\n"],"names":["a"],"mappings":"AAAA,OAAO,IAAMA,IAAY,MAAM"} \ No newline at end of file diff --git a/examples/source_root/expected_subdir.js b/examples/source_root/expected_subdir.js new file mode 100644 index 0000000..4124c34 --- /dev/null +++ b/examples/source_root/expected_subdir.js @@ -0,0 +1,3 @@ +export var a = "a"; + +//# sourceMappingURL=subdir.js.map \ No newline at end of file diff --git a/examples/source_root/expected_subdir.js.map b/examples/source_root/expected_subdir.js.map new file mode 100644 index 0000000..2577191 --- /dev/null +++ b/examples/source_root/expected_subdir.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["subdir.ts"],"sourceRoot":"../../../debug/source","sourcesContent":["export const a: string = \"a\";\n"],"names":["a"],"mappings":"AAAA,OAAO,IAAMA,IAAY,IAAI"} \ No newline at end of file diff --git a/examples/source_root/in.ts b/examples/source_root/in.ts new file mode 100644 index 0000000..7064c3e --- /dev/null +++ b/examples/source_root/in.ts @@ -0,0 +1 @@ +export const a: string = "foo"; diff --git a/examples/source_root/src/subdir.ts b/examples/source_root/src/subdir.ts new file mode 100644 index 0000000..f6c1fdd --- /dev/null +++ b/examples/source_root/src/subdir.ts @@ -0,0 +1 @@ +export const a: string = "a"; diff --git a/swc/private/swc.bzl b/swc/private/swc.bzl index 02ff6b1..4ee86a3 100644 --- a/swc/private/swc.bzl +++ b/swc/private/swc.bzl @@ -23,10 +23,18 @@ _attrs = { """, ), "source_maps": attr.string( - doc = "see https://swc.rs/docs/usage/cli#--source-maps--s", + doc = """Create source map files for emitted JavaScript files. + + see https://swc.rs/docs/usage/cli#--source-maps--s""", values = ["true", "false", "inline", "both"], default = "false", ), + "source_root": attr.string( + doc = """Specify the root path for debuggers to find the reference source code. + + see https://swc.rs/docs/usage/cli#--source-root""", + default = "", + ), "output_dir": attr.bool( doc = """Whether to produce a directory output rather than individual files. @@ -247,7 +255,7 @@ def _impl(ctx): for src in ctx.files.srcs: src_args = ctx.actions.args() src_args.add("--source-file-name", src.basename) - src_args.add("--source-root", src.dirname) + src_args.add("--source-root", src.dirname if ctx.attr.source_root == "" else ctx.attr.source_root) src_path = _relative_to_package(src.path, ctx)