Skip to content

Commit 1921be7

Browse files
committed
feat: support json sources
1 parent 72d22eb commit 1921be7

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

e2e/smoke/WORKSPACE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ swc_register_toolchains(
2929
name = "swc",
3030
swc_version = LATEST_SWC_VERSION,
3131
)
32+
33+
# To use rules_swc with bazel-lib 2.x, you must additionally register the coreutils toolchain.
34+
load("@aspect_bazel_lib//lib:repositories.bzl", "register_coreutils_toolchains")
35+
36+
register_coreutils_toolchains()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_test")
2+
load("@aspect_rules_swc//swc:defs.bzl", "swc")
3+
4+
swc(
5+
name = "ts",
6+
srcs = [
7+
"data.json",
8+
"index.ts",
9+
],
10+
args = [
11+
"--config-json",
12+
"""{"module": {"type": "commonjs"}}""",
13+
],
14+
)
15+
16+
swc(
17+
name = "ts-out_dir",
18+
srcs = [
19+
"data.json",
20+
"index.ts",
21+
],
22+
args = [
23+
"--config-json",
24+
"""{"module": {"type": "commonjs"}}""",
25+
],
26+
out_dir = "ts-out_dir",
27+
)
28+
29+
js_test(
30+
# Test that the json is available at runtime.
31+
name = "ts-with-json",
32+
data = [":ts"],
33+
entry_point = "index.js",
34+
)
35+
36+
js_test(
37+
# Test that the json is available at runtime with an out_dir.
38+
name = "ts-with-json-out_dir",
39+
data = [":ts-out_dir"],
40+
entry_point = "ts-out_dir/index.js",
41+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{
3+
"a": "b"
4+
}
5+
]

examples/resolve_json_module/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import data from "./data.json";
2+
export const a: string = "hello" + JSON.stringify(data);

swc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bzl_library(
5252
deps = [
5353
"//swc/private:swc",
5454
"//swc/private:swc_plugin",
55+
"@aspect_bazel_lib//lib:copy_file",
5556
"@bazel_skylib//lib:types",
5657
"@bazel_skylib//rules:write_file",
5758
] + (["@bazel_tools//tools/build_defs/repo:cache.bzl"] if bazel_lib_utils.is_bazel_7_or_greater() else []),

swc/private/swc.bzl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"Internal implementation details"
22

3+
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file_action")
4+
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS")
35
load("@aspect_bazel_lib//lib:platform_utils.bzl", "platform_utils")
46
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
57
load("@aspect_rules_js//js:providers.bzl", "js_info")
@@ -109,6 +111,9 @@ def _is_js_src(src):
109111
def _is_supported_src(src):
110112
return _is_ts_src(src) or _is_js_src(src)
111113

114+
def _is_data_src(src):
115+
return src.endswith(".json")
116+
112117
# TODO: vendored from rules_ts - aspect_bazel_lib should provide this?
113118
# https://github.com/aspect-build/rules_ts/blob/v3.2.1/ts/private/ts_lib.bzl#L194-L200
114119
def _relative_to_package(path, ctx):
@@ -359,6 +364,22 @@ def _swc_impl(ctx):
359364
output_sources.append(src)
360365
continue
361366

367+
if _is_data_src(src_path):
368+
# Copy data to the output directory.
369+
# NOTE: assumes json must be resolved at runtime, see ts_project(resolve_json_module)
370+
if ctx.attr.out_dir:
371+
out_path = "%s/%s" % (ctx.attr.out_dir if ctx.attr.out_dir else ".", src_path)
372+
out_file = ctx.actions.declare_file(out_path)
373+
copy_file_action(
374+
ctx = ctx,
375+
src = src,
376+
dst = out_file,
377+
)
378+
output_sources.append(out_file)
379+
else:
380+
output_sources.append(src)
381+
continue
382+
362383
js_out_path = _to_js_out(ctx.attr.default_ext, src_path, ctx.attr.out_dir, ctx.attr.root_dir, js_outs_relative)
363384
if not js_out_path:
364385
# This source file is not a supported src
@@ -440,7 +461,7 @@ def _swc_impl(ctx):
440461
swc = struct(
441462
implementation = _swc_impl,
442463
attrs = dict(_attrs, **_outputs),
443-
toolchains = ["@aspect_rules_swc//swc:toolchain_type"],
464+
toolchains = ["@aspect_rules_swc//swc:toolchain_type"] + COPY_FILE_TO_BIN_TOOLCHAINS,
444465
calculate_js_outs = _calculate_js_outs,
445466
calculate_map_outs = _calculate_map_outs,
446467
calculate_dts_outs = _calculate_dts_outs,

0 commit comments

Comments
 (0)