Skip to content

Commit f6fa264

Browse files
longlhoalexeagle
authored andcommitted
fix(typescript): add json to ts_project DefaultInfo, fix #1988
1 parent 245f0f8 commit f6fa264

10 files changed

Lines changed: 92 additions & 2 deletions

File tree

packages/typescript/internal/ts_project.bzl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ _ATTRS = {
2929
_OUTPUTS = {
3030
"buildinfo_out": attr.output(),
3131
"js_outs": attr.output_list(),
32+
"json_outs": attr.output_list(),
3233
"map_outs": attr.output_list(),
3334
"typing_maps_outs": attr.output_list(),
3435
"typings_outs": attr.output_list(),
@@ -96,10 +97,17 @@ def _ts_project_impl(ctx):
9697
inputs = ctx.files.srcs + depset(transitive = deps_depsets).to_list() + [ctx.file.tsconfig]
9798
if ctx.attr.extends:
9899
inputs.extend(ctx.files.extends)
99-
outputs = ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs
100+
101+
json_outs = ctx.outputs.json_outs
102+
103+
# If there are no predeclared json_outs (probably bc of no .json or no outdir),
104+
# let's try to search for them in srcs so we can declare them as output
105+
if not json_outs:
106+
json_outs = [ctx.actions.declare_file(src.basename, sibling = src) for src in ctx.files.srcs if src.basename.endswith(".json")]
107+
outputs = json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs
100108
if ctx.outputs.buildinfo_out:
101109
outputs.append(ctx.outputs.buildinfo_out)
102-
runtime_outputs = depset(ctx.outputs.js_outs + ctx.outputs.map_outs)
110+
runtime_outputs = depset(json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs)
103111
typings_outputs = ctx.outputs.typings_outs + [s for s in ctx.files.srcs if s.path.endswith(".d.ts")]
104112

105113
if len(outputs) > 0:
@@ -390,6 +398,9 @@ def ts_project_macro(
390398
tsconfig = tsconfig,
391399
extends = extends,
392400
outdir = outdir,
401+
# JSON files are special. They are output if outdir is set since tsc will
402+
# copy them to outdir. Otherwise they are kind of both.
403+
json_outs = [_join(outdir, f) for f in srcs if f.endswith(".json")] if outdir and not emit_declaration_only else [],
393404
js_outs = _out_paths(srcs, outdir, ".js") if not emit_declaration_only else [],
394405
map_outs = _out_paths(srcs, outdir, ".js.map") if source_map and not emit_declaration_only else [],
395406
typings_outs = _out_paths(srcs, outdir, ".d.ts") if declaration or composite else [],
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
2+
load("//packages/typescript:index.bzl", "ts_project")
3+
4+
SRCS = glob(["**/*.ts*"]) + [
5+
"subdir/foo.json",
6+
"bar.json",
7+
]
8+
9+
ts_project(
10+
name = "tsconfig",
11+
srcs = SRCS,
12+
outdir = "foobar",
13+
)
14+
15+
ts_project(
16+
name = "tsconfig-no-outdir",
17+
srcs = SRCS,
18+
tsconfig = "tsconfig.json",
19+
)
20+
21+
generated_file_test(
22+
name = "test-subdir",
23+
src = "foo.golden.json",
24+
# Refers to the output from tsconfig ts_project above
25+
generated = ":foobar/subdir/foo.json",
26+
)
27+
28+
generated_file_test(
29+
name = "test",
30+
src = "bar.golden.json",
31+
# Refers to the output from tsconfig ts_project above
32+
generated = ":foobar/bar.json",
33+
)
34+
35+
# We cannot reuse foo.golden.json bc tsc inserts a newline when
36+
# it copies to outdir
37+
generated_file_test(
38+
name = "test-subdir-no-outdir",
39+
src = "foo-no-outdir.golden.json",
40+
# Refers to the output from tsconfig-no-outdir ts_project above
41+
generated = ":subdir/foo.json",
42+
)
43+
44+
# We cannot reuse bar.golden.json bc tsc inserts a newline when
45+
# it copies to outdir
46+
generated_file_test(
47+
name = "test-no-outdir",
48+
src = "bar-no-outdir.golden.json",
49+
# Refers to the output from tsconfig-no-outdir ts_project above
50+
generated = ":bar.json",
51+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "bar"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "bar"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "bar"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "json"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "json"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const a: string = 'hello';
2+
import {name} from './foo.json'
3+
export {name} from '../bar.json'
4+
export const jsonName = name
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "json"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"types": [],
4+
"resolveJsonModule": true
5+
}
6+
}

0 commit comments

Comments
 (0)