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: support json sources #287

Merged
merged 1 commit into from
Jan 6, 2025
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: 5 additions & 0 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ swc_register_toolchains(
name = "swc",
swc_version = LATEST_SWC_VERSION,
)

# To use rules_swc with bazel-lib 2.x, you must additionally register the coreutils toolchain.
load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")

register_coreutils_toolchains()
41 changes: 41 additions & 0 deletions examples/resolve_json_module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@aspect_rules_swc//swc:defs.bzl", "swc")

swc(
name = "ts",
srcs = [
"data.json",
"index.ts",
],
args = [
"--config-json",
"""{"module": {"type": "commonjs"}}""",
],
)

swc(
name = "ts-out_dir",
srcs = [
"data.json",
"index.ts",
],
args = [
"--config-json",
"""{"module": {"type": "commonjs"}}""",
jbedard marked this conversation as resolved.
Show resolved Hide resolved
],
out_dir = "ts-out_dir",
)

js_test(
# Test that the json is available at runtime.
name = "ts-with-json",
data = [":ts"],
entry_point = "index.js",
)

js_test(
# Test that the json is available at runtime with an out_dir.
name = "ts-with-json-out_dir",
data = [":ts-out_dir"],
entry_point = "ts-out_dir/index.js",
)
5 changes: 5 additions & 0 deletions examples/resolve_json_module/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"a": "b"
}
]
2 changes: 2 additions & 0 deletions examples/resolve_json_module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import data from "./data.json";
export const a: string = "hello" + JSON.stringify(data);
1 change: 1 addition & 0 deletions swc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ bzl_library(
deps = [
"//swc/private:swc",
"//swc/private:swc_plugin",
"@aspect_bazel_lib//lib:copy_file",
"@bazel_skylib//lib:types",
"@bazel_skylib//rules:write_file",
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if bazel_lib_utils.is_bazel_7_or_greater() else []),
Expand Down
23 changes: 22 additions & 1 deletion swc/private/swc.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"Internal implementation details"

load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file_action")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS")
load("@aspect_bazel_lib//lib:platform_utils.bzl", "platform_utils")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "js_info")
Expand Down Expand Up @@ -109,6 +111,9 @@ def _is_js_src(src):
def _is_supported_src(src):
return _is_ts_src(src) or _is_js_src(src)

def _is_data_src(src):
return src.endswith(".json")

# TODO: vendored from rules_ts - aspect_bazel_lib should provide this?
# https://github.com/aspect-build/rules_ts/blob/v3.2.1/ts/private/ts_lib.bzl#L194-L200
def _relative_to_package(path, ctx):
Expand Down Expand Up @@ -359,6 +364,22 @@ def _swc_impl(ctx):
output_sources.append(src)
continue

if _is_data_src(src_path):
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
# Copy data to the output directory.
# NOTE: assumes json must be resolved at runtime, see ts_project(resolve_json_module)
if ctx.attr.out_dir:
out_path = "%s/%s" % (ctx.attr.out_dir if ctx.attr.out_dir else ".", src_path)
out_file = ctx.actions.declare_file(out_path)
copy_file_action(
ctx = ctx,
src = src,
dst = out_file,
)
output_sources.append(out_file)
else:
output_sources.append(src)
continue

js_out_path = _to_js_out(ctx.attr.default_ext, src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
if not js_out_path:
# This source file is not a supported src
Expand Down Expand Up @@ -440,7 +461,7 @@ def _swc_impl(ctx):
swc = struct(
implementation = _swc_impl,
attrs = dict(_attrs, **_outputs),
toolchains = ["@aspect_rules_swc//swc:toolchain_type"],
toolchains = ["@aspect_rules_swc//swc:toolchain_type"] + COPY_FILE_TO_BIN_TOOLCHAINS,
calculate_js_outs = _calculate_js_outs,
calculate_map_outs = _calculate_map_outs,
calculate_dts_outs = _calculate_dts_outs,
Expand Down
Loading