Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a new source_root attribute #177

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/swc.md

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

39 changes: 39 additions & 0 deletions examples/source_root/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
},
)
3 changes: 3 additions & 0 deletions examples/source_root/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/source_root/expected.js.map

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

3 changes: 3 additions & 0 deletions examples/source_root/expected_subdir.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/source_root/expected_subdir.js.map

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

1 change: 1 addition & 0 deletions examples/source_root/in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = "foo";
1 change: 1 addition & 0 deletions examples/source_root/src/subdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: string = "a";
12 changes: 10 additions & 2 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
),
"output_dir": attr.bool(
doc = """Whether to produce a directory output rather than individual files.

Expand Down Expand Up @@ -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)

Expand Down