From 91a95b89e097c289c0f489a71e39c0bb222efd4c Mon Sep 17 00:00:00 2001 From: Matt Insler Date: Wed, 28 Oct 2020 19:24:08 -0700 Subject: [PATCH] feat(typescript): add allow_js support to ts_project --- packages/typescript/internal/ts_project.bzl | 30 ++++++++++++---- .../internal/ts_project_options_validator.ts | 2 ++ .../test/ts_project/allow_js/BUILD.bazel | 36 +++++++++++++++++++ .../typescript/test/ts_project/allow_js/a.js | 3 ++ .../test/ts_project/allow_js/tsconfig.json | 9 +++++ .../test/ts_project/allow_js/verify.js | 6 ++++ 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 packages/typescript/test/ts_project/allow_js/BUILD.bazel create mode 100644 packages/typescript/test/ts_project/allow_js/a.js create mode 100644 packages/typescript/test/ts_project/allow_js/tsconfig.json create mode 100644 packages/typescript/test/ts_project/allow_js/verify.js diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index 1ca2e566ef..bd5aa6a6ff 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -215,6 +215,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, @@ -244,6 +245,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(), @@ -258,12 +260,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( @@ -273,6 +280,7 @@ def ts_project_macro( args = [], deps = [], extends = None, + allow_js = False, declaration = False, source_map = False, declaration_map = False, @@ -456,6 +464,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. @@ -482,7 +493,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(extends) == type([]): @@ -497,6 +511,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 @@ -535,6 +550,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, ) @@ -553,10 +569,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, diff --git a/packages/typescript/internal/ts_project_options_validator.ts b/packages/typescript/internal/ts_project_options_validator.ts index a8468446d9..a98b4df74d 100644 --- a/packages/typescript/internal/ts_project_options_validator.ts +++ b/packages/typescript/internal/ts_project_options_validator.ts @@ -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'); @@ -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} diff --git a/packages/typescript/test/ts_project/allow_js/BUILD.bazel b/packages/typescript/test/ts_project/allow_js/BUILD.bazel new file mode 100644 index 0000000000..f70437b271 --- /dev/null +++ b/packages/typescript/test/ts_project/allow_js/BUILD.bazel @@ -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)", + ], +) diff --git a/packages/typescript/test/ts_project/allow_js/a.js b/packages/typescript/test/ts_project/allow_js/a.js new file mode 100644 index 0000000000..afc18f4822 --- /dev/null +++ b/packages/typescript/test/ts_project/allow_js/a.js @@ -0,0 +1,3 @@ +'use strict'; +exports.__esModule = true; +exports.a = 'a'; diff --git a/packages/typescript/test/ts_project/allow_js/tsconfig.json b/packages/typescript/test/ts_project/allow_js/tsconfig.json new file mode 100644 index 0000000000..f0c07acb4b --- /dev/null +++ b/packages/typescript/test/ts_project/allow_js/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "allowJs": true, + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "types": [] + } +} diff --git a/packages/typescript/test/ts_project/allow_js/verify.js b/packages/typescript/test/ts_project/allow_js/verify.js new file mode 100644 index 0000000000..6e1a196bca --- /dev/null +++ b/packages/typescript/test/ts_project/allow_js/verify.js @@ -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');