Skip to content

Commit 9578f9f

Browse files
committed
fix: overridding tsconfig outDir via ts_project(tsconfig) dict
1 parent 43ae5c9 commit 9578f9f

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

examples/out_dir/BUILD.bazel

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,41 @@ ts_project(
3131
tsconfig = {},
3232
)
3333

34+
ts_project(
35+
name = "out_dir-tsconfig-foo",
36+
srcs = ["main.ts"],
37+
declaration = True,
38+
declaration_map = True,
39+
out_dir = "tsconfig-foo",
40+
tsconfig = "tsconfig-outdir-foo.json",
41+
)
42+
43+
ts_project(
44+
name = "out_dir-tsconfig-foo-overriden-to-dot",
45+
srcs = ["main2.ts"],
46+
declaration = True,
47+
declaration_map = True,
48+
extends = "tsconfig-outdir-foo.json",
49+
tsconfig = {
50+
"compilerOptions": {
51+
"outDir": ".",
52+
},
53+
},
54+
)
55+
56+
ts_project(
57+
name = "out_dir-tsconfig-foo-overriden-to-dotslash-bar",
58+
srcs = ["main2.ts"],
59+
declaration = True,
60+
declaration_map = True,
61+
extends = "tsconfig-outdir-foo.json",
62+
tsconfig = {
63+
"compilerOptions": {
64+
"outDir": "./bar",
65+
},
66+
},
67+
)
68+
3469
ts_project(
3570
name = "out_dir-declaration_dir",
3671
srcs = ["main.ts"],
@@ -44,13 +79,31 @@ ts_project(
4479
build_test(
4580
name = "test",
4681
targets = [
47-
# outputs specified by the tsconfig attr
82+
# outputs specified by the tsconfig attr dict
4883
":out_dir-params-dot",
4984
"main.js",
5085
"main.d.ts",
5186
"main.d.ts.map",
5287

53-
# outputs specified by the tsconfig attr
88+
# outputs specified by the tsconfig attr dict
89+
":out_dir-tsconfig-foo",
90+
"tsconfig-foo/main.js",
91+
"tsconfig-foo/main.d.ts",
92+
"tsconfig-foo/main.d.ts.map",
93+
94+
# outputs specified by the tsconfig attr file but overriden to '.' by tsconfig attr dict
95+
":out_dir-tsconfig-foo-overriden-to-dot",
96+
"main2.js",
97+
"main2.d.ts",
98+
"main2.d.ts.map",
99+
100+
# outputs specified by the tsconfig attr file but overriden to 'bar' by tsconfig attr dict
101+
":out_dir-tsconfig-foo-overriden-to-dotslash-bar",
102+
"bar/main2.js",
103+
"bar/main2.d.ts",
104+
"bar/main2.d.ts.map",
105+
106+
# outputs specified by the tsconfig attr file
54107
":out_dir-tsconfig",
55108
"tsconfig/main.js",
56109
"tsconfig/main.d.ts",

examples/out_dir/main2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello world')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"declarationMap": true,
5+
"outDir": "tsconfig-foo"
6+
}
7+
}

ts/defs.bzl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ def ts_project(
302302
resolve_json_module = compiler_options.setdefault("resolveJsonModule", resolve_json_module)
303303

304304
# These options are always passed on the tsc command line so don't include them
305-
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
306-
out_dir = compiler_options.pop("outDir", out_dir)
307-
declaration_dir = compiler_options.pop("declarationDir", declaration_dir)
308-
root_dir = compiler_options.pop("rootDir", root_dir)
305+
# in the tsconfig if not already present. At best they're redundant, but at worst
306+
# they will (minorly) differ from an extended config and we'll have a conflict.
307+
#
308+
# For example: an extended tsconfig contains `outDir:"foo"`, but within bazel we
309+
# want to override to `outDir: "."`.
310+
out_dir = compiler_options.get("outDir", out_dir)
311+
declaration_dir = compiler_options.get("declarationDir", declaration_dir)
312+
root_dir = compiler_options.get("rootDir", root_dir)
309313

310314
# When generating a tsconfig.json and we set rootDir we need to add the exclude field,
311315
# because of these tickets (validation for not generated tsconfig is also present elsewhere):
@@ -540,6 +544,8 @@ def _invoke_custom_transpiler(type_str, transpiler, transpile_target_name, srcs,
540544
fail("%s attribute should be a rule/macro or a skylib partial. Got %s" % (type_str, type(transpiler)))
541545

542546
def _clean_dir_arg(p):
543-
if not p or p == "." or p == "./":
547+
if p == None or p == "":
544548
return None
549+
if p == "." or p == "./":
550+
return "."
545551
return p.removeprefix("./")

ts/private/ts_lib.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def _to_out_path(f, out_dir, root_dir):
242242

243243
if root_dir:
244244
f = f.removeprefix(root_dir + "/")
245-
if out_dir:
245+
if out_dir and out_dir != ".":
246246
f = out_dir + "/" + f
247247
return f
248248

0 commit comments

Comments
 (0)