Skip to content

Commit

Permalink
feat: support json sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Dec 9, 2024
1 parent 72d22eb commit beef65d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
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"}}""",
],
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):
# 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 or True:
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

0 comments on commit beef65d

Please sign in to comment.