Skip to content

Commit

Permalink
fix(typescript): add json to ts_project DefaultInfo, fix #1988
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho authored and alexeagle committed Jul 6, 2020
1 parent 245f0f8 commit f6fa264
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _ATTRS = {
_OUTPUTS = {
"buildinfo_out": attr.output(),
"js_outs": attr.output_list(),
"json_outs": attr.output_list(),
"map_outs": attr.output_list(),
"typing_maps_outs": attr.output_list(),
"typings_outs": attr.output_list(),
Expand Down Expand Up @@ -96,10 +97,17 @@ def _ts_project_impl(ctx):
inputs = ctx.files.srcs + depset(transitive = deps_depsets).to_list() + [ctx.file.tsconfig]
if ctx.attr.extends:
inputs.extend(ctx.files.extends)
outputs = ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs

json_outs = ctx.outputs.json_outs

# If there are no predeclared json_outs (probably bc of no .json or no outdir),
# let's try to search for them in srcs so we can declare them as output
if not json_outs:
json_outs = [ctx.actions.declare_file(src.basename, sibling = src) for src in ctx.files.srcs if src.basename.endswith(".json")]
outputs = json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs
if ctx.outputs.buildinfo_out:
outputs.append(ctx.outputs.buildinfo_out)
runtime_outputs = depset(ctx.outputs.js_outs + ctx.outputs.map_outs)
runtime_outputs = depset(json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs)
typings_outputs = ctx.outputs.typings_outs + [s for s in ctx.files.srcs if s.path.endswith(".d.ts")]

if len(outputs) > 0:
Expand Down Expand Up @@ -390,6 +398,9 @@ def ts_project_macro(
tsconfig = tsconfig,
extends = extends,
outdir = outdir,
# JSON files are special. They are output if outdir is set since tsc will
# copy them to outdir. Otherwise they are kind of both.
json_outs = [_join(outdir, f) for f in srcs if f.endswith(".json")] if outdir and not emit_declaration_only else [],
js_outs = _out_paths(srcs, outdir, ".js") if not emit_declaration_only else [],
map_outs = _out_paths(srcs, outdir, ".js.map") if source_map and not emit_declaration_only else [],
typings_outs = _out_paths(srcs, outdir, ".d.ts") if declaration or composite else [],
Expand Down
51 changes: 51 additions & 0 deletions packages/typescript/test/ts_project/json/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load("//packages/typescript:index.bzl", "ts_project")

SRCS = glob(["**/*.ts*"]) + [
"subdir/foo.json",
"bar.json",
]

ts_project(
name = "tsconfig",
srcs = SRCS,
outdir = "foobar",
)

ts_project(
name = "tsconfig-no-outdir",
srcs = SRCS,
tsconfig = "tsconfig.json",
)

generated_file_test(
name = "test-subdir",
src = "foo.golden.json",
# Refers to the output from tsconfig ts_project above
generated = ":foobar/subdir/foo.json",
)

generated_file_test(
name = "test",
src = "bar.golden.json",
# Refers to the output from tsconfig ts_project above
generated = ":foobar/bar.json",
)

# We cannot reuse foo.golden.json bc tsc inserts a newline when
# it copies to outdir
generated_file_test(
name = "test-subdir-no-outdir",
src = "foo-no-outdir.golden.json",
# Refers to the output from tsconfig-no-outdir ts_project above
generated = ":subdir/foo.json",
)

# We cannot reuse bar.golden.json bc tsc inserts a newline when
# it copies to outdir
generated_file_test(
name = "test-no-outdir",
src = "bar-no-outdir.golden.json",
# Refers to the output from tsconfig-no-outdir ts_project above
generated = ":bar.json",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "bar"
}
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/json/bar.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "bar"
}
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/json/bar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "bar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "json"
}
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/json/foo.golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "json"
}
4 changes: 4 additions & 0 deletions packages/typescript/test/ts_project/json/subdir/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const a: string = 'hello';
import {name} from './foo.json'
export {name} from '../bar.json'
export const jsonName = name
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/json/subdir/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "json"
}
6 changes: 6 additions & 0 deletions packages/typescript/test/ts_project/json/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"types": [],
"resolveJsonModule": true
}
}

0 comments on commit f6fa264

Please sign in to comment.