Skip to content

Commit 3849ce6

Browse files
authored
fix: support setting resolveJsonModule with inline ts_project(tsconfig) (#728)
1 parent 6df0fe6 commit 3849ce6

File tree

3 files changed

+75
-18
lines changed

3 files changed

+75
-18
lines changed

examples/json_data/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ ts_project(
1616
"src/tsdata.txt",
1717
":fg",
1818
],
19-
resolve_json_module = True,
2019
tsconfig = {
2120
"compilerOptions": {
2221
"resolveJsonModule": True,

examples/resolve_json_module/BUILD.bazel

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ ts_project(
1313
resolve_json_module = True,
1414
)
1515

16+
ts_project(
17+
name = "ts-dict-override",
18+
srcs = [
19+
"data.json",
20+
"index.ts",
21+
],
22+
extends = "tsconfig.json",
23+
tsconfig = {
24+
"compilerOptions": {
25+
"outDir": "ts-dict-override",
26+
"resolveJsonModule": True,
27+
},
28+
},
29+
)
30+
31+
ts_project(
32+
name = "ts-dict-unspecified",
33+
srcs = [
34+
"data.json",
35+
"index.ts",
36+
],
37+
extends = "tsconfig.json",
38+
resolve_json_module = True,
39+
tsconfig = {
40+
"compilerOptions": {
41+
"outDir": "ts-dict-unspecified",
42+
},
43+
},
44+
)
45+
1646
assert_contains(
1747
name = "test",
1848
actual = "index.js",
@@ -25,3 +55,17 @@ js_test(
2555
data = [":ts"],
2656
entry_point = "index.js",
2757
)
58+
59+
# Test that the json is available at runtime with various ways of
60+
# specifying resolveJsonModule in tsconfig.json.
61+
[
62+
js_test(
63+
name = "ts-with-json-%s" % t,
64+
data = [":ts-%s" % t],
65+
entry_point = "ts-%s/index.js" % t,
66+
)
67+
for t in [
68+
"dict-override",
69+
"dict-unspecified",
70+
]
71+
]

ts/defs.bzl

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,6 @@ def ts_project(
260260
**kwargs: passed through to underlying [`ts_project_rule`](#ts_project_rule), eg. `visibility`, `tags`
261261
"""
262262

263-
if srcs == None:
264-
include = ["**/*.ts", "**/*.tsx"]
265-
exclude = []
266-
if allow_js == True:
267-
include.extend(["**/*.js", "**/*.jsx"])
268-
if resolve_json_module == True:
269-
include.append("**/*.json")
270-
exclude.extend(["**/package.json", "**/package-lock.json", "**/tsconfig*.json"])
271-
srcs = native.glob(include, exclude)
272263
tsc_deps = deps
273264

274265
common_kwargs = {
@@ -299,17 +290,20 @@ def ts_project(
299290
no_emit = compiler_options.setdefault("noEmit", no_emit)
300291
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
301292
allow_js = compiler_options.setdefault("allowJs", allow_js)
302-
if resolve_json_module != None:
303-
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)
293+
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)
304294

305295
# These options are always passed on the tsc command line so don't include them
306296
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
307-
if "outDir" in compiler_options.keys():
308-
out_dir = compiler_options.pop("outDir")
309-
if "declarationDir" in compiler_options.keys():
310-
declaration_dir = compiler_options.pop("declarationDir")
311-
if "rootDir" in compiler_options.keys():
312-
root_dir = compiler_options.pop("rootDir")
297+
out_dir = compiler_options.pop("outDir", out_dir)
298+
declaration_dir = compiler_options.pop("declarationDir", declaration_dir)
299+
root_dir = compiler_options.pop("rootDir", root_dir)
300+
301+
if srcs == None:
302+
# Default sources based on macro attributes after applying tsconfig properties
303+
srcs = _default_srcs(
304+
allow_js = allow_js,
305+
resolve_json_module = resolve_json_module,
306+
)
313307

314308
# FIXME: need to remove keys that have a None value?
315309
write_tsconfig(
@@ -326,6 +320,12 @@ def ts_project(
326320
# From here, tsconfig becomes a file, the same as if the
327321
# user supplied a tsconfig.json InputArtifact
328322
tsconfig = "tsconfig_%s.json" % name
323+
elif srcs == None:
324+
# Default sources based on macro attributes
325+
srcs = _default_srcs(
326+
allow_js = allow_js,
327+
resolve_json_module = resolve_json_module,
328+
)
329329

330330
typings_out_dir = declaration_dir if declaration_dir else out_dir
331331
tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"
@@ -463,6 +463,20 @@ def ts_project(
463463
**kwargs
464464
)
465465

466+
def _default_srcs(allow_js, resolve_json_module):
467+
"""Returns a list of srcs for ts_project when srcs is not provided."""
468+
include = ["**/*.ts", "**/*.tsx"]
469+
exclude = []
470+
471+
if allow_js == True:
472+
include.extend(["**/*.js", "**/*.jsx"])
473+
474+
if resolve_json_module == True:
475+
include.append("**/*.json")
476+
exclude.extend(["**/package.json", "**/package-lock.json", "**/tsconfig*.json"])
477+
478+
return native.glob(include, exclude)
479+
466480
def _invoke_custom_transpiler(type_str, transpiler, transpile_target_name, srcs, common_kwargs):
467481
if type(transpiler) == "function" or type(transpiler) == "rule":
468482
transpiler(

0 commit comments

Comments
 (0)