Skip to content

Commit 12571ee

Browse files
Matt Insleralexeagle
Matt Insler
authored andcommitted
feat(typescript): add allow_js support to ts_project
1 parent 9a50cb6 commit 12571ee

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

packages/typescript/internal/ts_project.bzl

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

214214
arguments = ctx.actions.args()
215215
arguments.add_all([ctx.file.tsconfig.path, marker.path, ctx.attr.target, struct(
216+
allow_js = ctx.attr.allow_js,
216217
declaration = ctx.attr.declaration,
217218
declaration_map = ctx.attr.declaration_map,
218219
composite = ctx.attr.composite,
@@ -242,6 +243,7 @@ def _validate_options_impl(ctx):
242243
validate_options = rule(
243244
implementation = _validate_options_impl,
244245
attrs = {
246+
"allow_js": attr.bool(),
245247
"composite": attr.bool(),
246248
"declaration": attr.bool(),
247249
"declaration_map": attr.bool(),
@@ -256,12 +258,17 @@ validate_options = rule(
256258
},
257259
)
258260

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

267274
def ts_project_macro(
@@ -271,6 +278,7 @@ def ts_project_macro(
271278
args = [],
272279
deps = [],
273280
extends = None,
281+
allow_js = False,
274282
declaration = False,
275283
source_map = False,
276284
declaration_map = False,
@@ -459,6 +467,9 @@ def ts_project_macro(
459467
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
460468
By default the out_dir is '.', meaning the packages folder in bazel-out.
461469
470+
allow_js: boolean; Specifies whether TypeScript will read .js and .jsx files. When used with declaration,
471+
TypeScript will generate .d.ts files from .js files.
472+
462473
declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
463474
outputs are written. Equivalent to the TypeScript --declarationDir option.
464475
By default declarations are written to the out_dir.
@@ -485,7 +496,10 @@ def ts_project_macro(
485496
"""
486497

487498
if srcs == None:
488-
srcs = native.glob(["**/*.ts", "**/*.tsx"])
499+
if allow_js == True:
500+
srcs = native.glob(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"])
501+
else:
502+
srcs = native.glob(["**/*.ts", "**/*.tsx"])
489503
extra_deps = []
490504

491505
if type(tsconfig) == type(dict()):
@@ -500,6 +514,7 @@ def ts_project_macro(
500514
declaration = compiler_options.setdefault("declaration", declaration)
501515
declaration_map = compiler_options.setdefault("declarationMap", declaration_map)
502516
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
517+
allow_js = compiler_options.setdefault("allowJs", allow_js)
503518

504519
# These options are always passed on the tsc command line so don't include them
505520
# in the tsconfig. At best they're redundant, but at worst we'll have a conflict
@@ -538,6 +553,7 @@ def ts_project_macro(
538553
incremental = incremental,
539554
ts_build_info_file = ts_build_info_file,
540555
emit_declaration_only = emit_declaration_only,
556+
allow_js = allow_js,
541557
tsconfig = tsconfig,
542558
extends = extends,
543559
)
@@ -560,10 +576,10 @@ def ts_project_macro(
560576
declaration_dir = declaration_dir,
561577
out_dir = out_dir,
562578
root_dir = root_dir,
563-
js_outs = _out_paths(srcs, out_dir, root_dir, ".js") if not emit_declaration_only else [],
564-
map_outs = _out_paths(srcs, out_dir, root_dir, ".js.map") if source_map and not emit_declaration_only else [],
565-
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts") if declaration or composite else [],
566-
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts.map") if declaration_map else [],
579+
js_outs = _out_paths(srcs, out_dir, root_dir, False, ".js") if not emit_declaration_only else [],
580+
map_outs = _out_paths(srcs, out_dir, root_dir, False, ".js.map") if source_map and not emit_declaration_only else [],
581+
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts") if declaration or composite else [],
582+
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts.map") if declaration_map else [],
567583
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
568584
tsc = tsc,
569585
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)