Skip to content

Commit

Permalink
feat(typescript): add allow_js support to ts_project
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Insler authored and alexeagle committed Oct 29, 2020
1 parent 9a50cb6 commit 12571ee
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def _validate_options_impl(ctx):

arguments = ctx.actions.args()
arguments.add_all([ctx.file.tsconfig.path, marker.path, ctx.attr.target, struct(
allow_js = ctx.attr.allow_js,
declaration = ctx.attr.declaration,
declaration_map = ctx.attr.declaration_map,
composite = ctx.attr.composite,
Expand Down Expand Up @@ -242,6 +243,7 @@ def _validate_options_impl(ctx):
validate_options = rule(
implementation = _validate_options_impl,
attrs = {
"allow_js": attr.bool(),
"composite": attr.bool(),
"declaration": attr.bool(),
"declaration_map": attr.bool(),
Expand All @@ -256,12 +258,17 @@ validate_options = rule(
},
)

def _out_paths(srcs, outdir, rootdir, ext):
def _is_ts_src(src, allow_js):
if not src.endswith(".d.ts") and (src.endswith(".ts") or src.endswith(".tsx")):
return True
return allow_js and (src.endswith(".js") or src.endswith(".jsx"))

def _out_paths(srcs, outdir, rootdir, allow_js, ext):
rootdir_replace_pattern = rootdir + "/" if rootdir else ""
return [
_join(outdir, f[:f.rindex(".")].replace(rootdir_replace_pattern, "") + ext)
for f in srcs
if not f.endswith(".d.ts") and (f.endswith(".ts") or f.endswith(".tsx"))
if _is_ts_src(f, allow_js)
]

def ts_project_macro(
Expand All @@ -271,6 +278,7 @@ def ts_project_macro(
args = [],
deps = [],
extends = None,
allow_js = False,
declaration = False,
source_map = False,
declaration_map = False,
Expand Down Expand Up @@ -459,6 +467,9 @@ def ts_project_macro(
will appear in bazel-out/[arch]/bin/path/to/my/package/foo/*.js.
By default the out_dir is '.', meaning the packages folder in bazel-out.
allow_js: boolean; Specifies whether TypeScript will read .js and .jsx files. When used with declaration,
TypeScript will generate .d.ts files from .js files.
declaration_dir: a string specifying a subdirectory under the bazel-out folder where generated declaration
outputs are written. Equivalent to the TypeScript --declarationDir option.
By default declarations are written to the out_dir.
Expand All @@ -485,7 +496,10 @@ def ts_project_macro(
"""

if srcs == None:
srcs = native.glob(["**/*.ts", "**/*.tsx"])
if allow_js == True:
srcs = native.glob(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"])
else:
srcs = native.glob(["**/*.ts", "**/*.tsx"])
extra_deps = []

if type(tsconfig) == type(dict()):
Expand All @@ -500,6 +514,7 @@ def ts_project_macro(
declaration = compiler_options.setdefault("declaration", declaration)
declaration_map = compiler_options.setdefault("declarationMap", declaration_map)
emit_declaration_only = compiler_options.setdefault("emitDeclarationOnly", emit_declaration_only)
allow_js = compiler_options.setdefault("allowJs", allow_js)

# 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
Expand Down Expand Up @@ -538,6 +553,7 @@ def ts_project_macro(
incremental = incremental,
ts_build_info_file = ts_build_info_file,
emit_declaration_only = emit_declaration_only,
allow_js = allow_js,
tsconfig = tsconfig,
extends = extends,
)
Expand All @@ -560,10 +576,10 @@ def ts_project_macro(
declaration_dir = declaration_dir,
out_dir = out_dir,
root_dir = root_dir,
js_outs = _out_paths(srcs, out_dir, root_dir, ".js") if not emit_declaration_only else [],
map_outs = _out_paths(srcs, out_dir, root_dir, ".js.map") if source_map and not emit_declaration_only else [],
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts") if declaration or composite else [],
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, ".d.ts.map") if declaration_map else [],
js_outs = _out_paths(srcs, out_dir, root_dir, False, ".js") if not emit_declaration_only else [],
map_outs = _out_paths(srcs, out_dir, root_dir, False, ".js.map") if source_map and not emit_declaration_only else [],
typings_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts") if declaration or composite else [],
typing_maps_outs = _out_paths(srcs, typings_out_dir, root_dir, allow_js, ".d.ts.map") if declaration_map else [],
buildinfo_out = tsbuildinfo_path if composite or incremental else None,
tsc = tsc,
link_workspace_root = link_workspace_root,
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/internal/ts_project_options_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function main([tsconfigPath, output, target, attrsStr]: string[]): 0|1 {
}
}

check('allowJs', 'allow_js');
check('declarationMap', 'declaration_map');
check('emitDeclarationOnly', 'emit_declaration_only');
check('sourceMap', 'source_map');
Expand All @@ -89,6 +90,7 @@ function main([tsconfigPath, output, target, attrsStr]: string[]): 0|1 {
require('fs').writeFileSync(
output, `
// ${process.argv[1]} checked attributes for ${target}
// allow_js: ${attrs.allow_js}
// composite: ${attrs.composite}
// declaration: ${attrs.declaration}
// declaration_map: ${attrs.declaration_map}
Expand Down
36 changes: 36 additions & 0 deletions packages/typescript/test/ts_project/allow_js/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
load("//packages/typescript:index.bzl", "ts_project")

# Ensure that a.js produces outDir/a.js and outDir/a.d.ts
SRCS = [
"a.js",
]

ts_project(
name = "tsconfig",
srcs = SRCS,
allow_js = True,
declaration = True,
declaration_map = True,
out_dir = "out",
source_map = True,
)

filegroup(
name = "types",
srcs = [":tsconfig"],
output_group = "types",
)

nodejs_test(
name = "test",
data = [
":tsconfig",
":types",
],
entry_point = "verify.js",
templated_args = [
"$(locations :types)",
"$(locations :tsconfig)",
],
)
3 changes: 3 additions & 0 deletions packages/typescript/test/ts_project/allow_js/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
exports.__esModule = true;
exports.a = 'a';
9 changes: 9 additions & 0 deletions packages/typescript/test/ts_project/allow_js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"allowJs": true,
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"types": []
}
}
6 changes: 6 additions & 0 deletions packages/typescript/test/ts_project/allow_js/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const assert = require('assert');

const types_files = process.argv.slice(2, 4);
const code_files = process.argv.slice(4, 6);
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts')), 'Missing a.d.ts');
assert.ok(types_files.some(f => f.endsWith('out/a.d.ts.map')), 'Missing a.d.ts.map');

0 comments on commit 12571ee

Please sign in to comment.