Skip to content

Commit 91a95b8

Browse files
Matt InslerAlex Eagle
authored andcommitted
feat(typescript): add allow_js support to ts_project
1 parent 0fcd6e6 commit 91a95b8

6 files changed

Lines changed: 79 additions & 7 deletions

File tree

packages/typescript/internal/ts_project.bzl

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def _validate_options_impl(ctx):
215215

216216
arguments = ctx.actions.args()
217217
arguments.add_all([ctx.file.tsconfig.path, marker.path, ctx.attr.target, struct(
218+
allow_js = ctx.attr.allow_js,
218219
declaration = ctx.attr.declaration,
219220
declaration_map = ctx.attr.declaration_map,
220221
composite = ctx.attr.composite,
@@ -244,6 +245,7 @@ def _validate_options_impl(ctx):
244245
validate_options = rule(
245246
implementation = _validate_options_impl,
246247
attrs = {
248+
"allow_js": attr.bool(),
247249
"composite": attr.bool(),
248250
"declaration": attr.bool(),
249251
"declaration_map": attr.bool(),
@@ -258,12 +260,17 @@ validate_options = rule(
258260
},
259261
)
260262

261-
def _out_paths(srcs, outdir, rootdir, ext):
263+
def _is_ts_src(src, allow_js):
264+
if not src.endswith(".d.ts") and (src.endswith(".ts") or src.endswith(".tsx")):
265+
return True
266+
return allow_js and (src.endswith(".js") or src.endswith(".jsx"))
267+
268+
def _out_paths(srcs, outdir, rootdir, allow_js, ext):
262269
rootdir_replace_pattern = rootdir + "/" if rootdir else ""
263270
return [
264271
_join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + ext)
265272
for f in srcs
266-
if not f.endswith(".d.ts") and (f.endswith(".ts") or f.endswith(".tsx"))
273+
if _is_ts_src(f, allow_js)
267274
]
268275

269276
def ts_project_macro(
@@ -273,6 +280,7 @@ def ts_project_macro(
273280
args = [],
274281
deps = [],
275282
extends = None,
283+
allow_js = False,
276284
declaration = False,
277285
source_map = False,
278286
declaration_map = False,
@@ -456,6 +464,9 @@ def ts_project_macro(
456464
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
457465
By default the out_dir is '.', meaning the packages folder in bazel-out.
458466
467+
allow_js: boolean; Specifies whether TypeScript will read .js and .jsx files. When used with declaration,
468+
TypeScript will generate .d.ts files from .js files.
469+
459470
declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
460471
outputs are written. Equivalent to the TypeScript --declarationDir option.
461472
By default declarations are written to the out_dir.
@@ -482,7 +493,10 @@ def ts_project_macro(
482493
"""
483494

484495
if srcs == None:
485-
srcs = native.glob(["**/*.ts", "**/*.tsx"])
496+
if allow_js == True:
497+
srcs = native.glob(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"])
498+
else:
499+
srcs = native.glob(["**/*.ts", "**/*.tsx"])
486500
extra_deps = []
487501

488502
if type(extends) == type([]):
@@ -497,6 +511,7 @@ def ts_project_macro(
497511
declaration = compiler_options.setdefault("declaration", declaration)
498512
declaration_map = compiler_options.setdefault("declarationMap", declaration_map)
499513
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
514+
allow_js = compiler_options.setdefault("allowJs", allow_js)
500515

501516
# These options are always passed on the tsc command line so don't include them
502517
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
@@ -535,6 +550,7 @@ def ts_project_macro(
535550
incremental = incremental,
536551
ts_build_info_file = ts_build_info_file,
537552
emit_declaration_only = emit_declaration_only,
553+
allow_js = allow_js,
538554
tsconfig = tsconfig,
539555
extends = extends,
540556
)
@@ -553,10 +569,10 @@ def ts_project_macro(
553569
declaration_dir = declaration_dir,
554570
out_dir = out_dir,
555571
root_dir = root_dir,
556-
js_outs = _out_paths(srcs, out_dir, root_dir, ".js") if not emit_declaration_only else [],
557-
map_outs = _out_paths(srcs, out_dir, root_dir, ".js.map") if source_map and not emit_declaration_only else [],
558-
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts") if declaration or composite else [],
559-
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts.map") if declaration_map else [],
572+
js_outs = _out_paths(srcs, out_dir, root_dir, False, ".js") if not emit_declaration_only else [],
573+
map_outs = _out_paths(srcs, out_dir, root_dir, False, ".js.map") if source_map and not emit_declaration_only else [],
574+
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts") if declaration or composite else [],
575+
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts.map") if declaration_map else [],
560576
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
561577
tsc = tsc,
562578
link_workspace_root = link_workspace_root,

packages/typescript/internal/ts_project_options_validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function main([tsconfigPath, output, target, attrsStr]: string[]): 0|1 {
6464
}
6565
}
6666

67+
check('allowJs', 'allow_js');
6768
check('declarationMap', 'declaration_map');
6869
check('emitDeclarationOnly', 'emit_declaration_only');
6970
check('sourceMap', 'source_map');
@@ -89,6 +90,7 @@ function main([tsconfigPath, output, target, attrsStr]: string[]): 0|1 {
8990
require('fs').writeFileSync(
9091
output, `
9192
// ${process.argv[1]} checked attributes for ${target}
93+
// allow_js: ${attrs.allow_js}
9294
// composite: ${attrs.composite}
9395
// declaration: ${attrs.declaration}
9496
// declaration_map: ${attrs.declaration_map}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
2+
load("//packages/typescript:index.bzl", "ts_project")
3+
4+
# Ensure that a.js produces outDir/a.js and outDir/a.d.ts
5+
SRCS = [
6+
"a.js",
7+
]
8+
9+
ts_project(
10+
name = "tsconfig",
11+
srcs = SRCS,
12+
allow_js = True,
13+
declaration = True,
14+
declaration_map = True,
15+
out_dir = "out",
16+
source_map = True,
17+
)
18+
19+
filegroup(
20+
name = "types",
21+
srcs = [":tsconfig"],
22+
output_group = "types",
23+
)
24+
25+
nodejs_test(
26+
name = "test",
27+
data = [
28+
":tsconfig",
29+
":types",
30+
],
31+
entry_point = "verify.js",
32+
templated_args = [
33+
"$(locations :types)",
34+
"$(locations :tsconfig)",
35+
],
36+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
exports.__esModule = true;
3+
exports.a = 'a';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"sourceMap": true,
5+
"declaration": true,
6+
"declarationMap": true,
7+
"types": []
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const assert = require('assert');
2+
3+
const types_files = process.argv.slice(2, 4);
4+
const code_files = process.argv.slice(4, 6);
5+
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts')), 'Missing a.d.ts');
6+
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts.map')), 'Missing a.d.ts.map');

0 commit comments

Comments
 (0)