Skip to content

Commit

Permalink
fix: support setting resolveJsonModule with inline ts_project(tsconfi…
Browse files Browse the repository at this point in the history
…g) (#728)
  • Loading branch information
jbedard authored Oct 31, 2024
1 parent 6df0fe6 commit 3849ce6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
1 change: 0 additions & 1 deletion examples/json_data/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ts_project(
"src/tsdata.txt",
":fg",
],
resolve_json_module = True,
tsconfig = {
"compilerOptions": {
"resolveJsonModule": True,
Expand Down
44 changes: 44 additions & 0 deletions examples/resolve_json_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ ts_project(
resolve_json_module = True,
)

ts_project(
name = "ts-dict-override",
srcs = [
"data.json",
"index.ts",
],
extends = "tsconfig.json",
tsconfig = {
"compilerOptions": {
"outDir": "ts-dict-override",
"resolveJsonModule": True,
},
},
)

ts_project(
name = "ts-dict-unspecified",
srcs = [
"data.json",
"index.ts",
],
extends = "tsconfig.json",
resolve_json_module = True,
tsconfig = {
"compilerOptions": {
"outDir": "ts-dict-unspecified",
},
},
)

assert_contains(
name = "test",
actual = "index.js",
Expand All @@ -25,3 +55,17 @@ js_test(
data = [":ts"],
entry_point = "index.js",
)

# Test that the json is available at runtime with various ways of
# specifying resolveJsonModule in tsconfig.json.
[
js_test(
name = "ts-with-json-%s" % t,
data = [":ts-%s" % t],
entry_point = "ts-%s/index.js" % t,
)
for t in [
"dict-override",
"dict-unspecified",
]
]
48 changes: 31 additions & 17 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,6 @@ def ts_project(
**kwargs: passed through to underlying [`ts_project_rule`](#ts_project_rule), eg. `visibility`, `tags`
"""

if srcs == None:
include = ["**/*.ts", "**/*.tsx"]
exclude = []
if allow_js == True:
include.extend(["**/*.js", "**/*.jsx"])
if resolve_json_module == True:
include.append("**/*.json")
exclude.extend(["**/package.json", "**/package-lock.json", "**/tsconfig*.json"])
srcs = native.glob(include, exclude)
tsc_deps = deps

common_kwargs = {
Expand Down Expand Up @@ -299,17 +290,20 @@ def ts_project(
no_emit = compiler_options.setdefault("noEmit", no_emit)
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
allow_js = compiler_options.setdefault("allowJs", allow_js)
if resolve_json_module != None:
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)

# These options are always passed on the tsc command line so don't include them
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
if "outDir" in compiler_options.keys():
out_dir = compiler_options.pop("outDir")
if "declarationDir" in compiler_options.keys():
declaration_dir = compiler_options.pop("declarationDir")
if "rootDir" in compiler_options.keys():
root_dir = compiler_options.pop("rootDir")
out_dir = compiler_options.pop("outDir", out_dir)
declaration_dir = compiler_options.pop("declarationDir", declaration_dir)
root_dir = compiler_options.pop("rootDir", root_dir)

if srcs == None:
# Default sources based on macro attributes after applying tsconfig properties
srcs = _default_srcs(
allow_js = allow_js,
resolve_json_module = resolve_json_module,
)

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

typings_out_dir = declaration_dir if declaration_dir else out_dir
tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"
Expand Down Expand Up @@ -463,6 +463,20 @@ def ts_project(
**kwargs
)

def _default_srcs(allow_js, resolve_json_module):
"""Returns a list of srcs for ts_project when srcs is not provided."""
include = ["**/*.ts", "**/*.tsx"]
exclude = []

if allow_js == True:
include.extend(["**/*.js", "**/*.jsx"])

if resolve_json_module == True:
include.append("**/*.json")
exclude.extend(["**/package.json", "**/package-lock.json", "**/tsconfig*.json"])

return native.glob(include, exclude)

def _invoke_custom_transpiler(type_str, transpiler, transpile_target_name, srcs, common_kwargs):
if type(transpiler) == "function" or type(transpiler) == "rule":
transpiler(
Expand Down

0 comments on commit 3849ce6

Please sign in to comment.