From 34a50083877b9e4eda829456ef344e305269970c Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Sun, 16 Aug 2020 10:08:27 -0700 Subject: [PATCH] fix(typescript): produce .d.ts as default output rather than empty This lets you type-check a ts_project(emit_declaration_only=True) just by building it (its default outputs) Fixes #2116 --- packages/typescript/internal/ts_project.bzl | 9 +++++---- .../test/ts_project/declaration_only/BUILD.bazel | 13 +++++++++++++ .../test/ts_project/declaration_only/a.d.ts_ | 1 + .../test/ts_project/declaration_only/a.ts | 1 + .../test/ts_project/declaration_only/tsconfig.json | 7 +++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 packages/typescript/test/ts_project/declaration_only/BUILD.bazel create mode 100644 packages/typescript/test/ts_project/declaration_only/a.d.ts_ create mode 100644 packages/typescript/test/ts_project/declaration_only/a.ts create mode 100644 packages/typescript/test/ts_project/declaration_only/tsconfig.json diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index f7029dd180..0fefc9b743 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -116,8 +116,9 @@ def _ts_project_impl(ctx): outputs = json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs + ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs if ctx.outputs.buildinfo_out: outputs.append(ctx.outputs.buildinfo_out) - runtime_outputs = depset(json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs) + runtime_outputs = json_outs + ctx.outputs.js_outs + ctx.outputs.map_outs typings_outputs = ctx.outputs.typings_outs + ctx.outputs.typing_maps_outs + [s for s in ctx.files.srcs if s.path.endswith(".d.ts")] + default_outputs_depset = depset(runtime_outputs) if len(runtime_outputs) else depset(typings_outputs) if len(outputs) > 0: run_node( @@ -139,14 +140,14 @@ def _ts_project_impl(ctx): # Only the JavaScript outputs are intended for use in non-TS-aware # dependents. DefaultInfo( - files = runtime_outputs, + files = default_outputs_depset, runfiles = ctx.runfiles( - transitive_files = runtime_outputs, + transitive_files = default_outputs_depset, collect_default = True, ), ), js_module_info( - sources = runtime_outputs, + sources = depset(runtime_outputs), deps = ctx.attr.deps, ), _TsConfigInfo(tsconfigs = depset([ctx.file.tsconfig] + ctx.files.extends, transitive = [ diff --git a/packages/typescript/test/ts_project/declaration_only/BUILD.bazel b/packages/typescript/test/ts_project/declaration_only/BUILD.bazel new file mode 100644 index 0000000000..a167806756 --- /dev/null +++ b/packages/typescript/test/ts_project/declaration_only/BUILD.bazel @@ -0,0 +1,13 @@ +load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test") +load("//packages/typescript:index.bzl", "ts_project") + +ts_project( + declaration = True, + emit_declaration_only = True, +) + +generated_file_test( + name = "test", + src = "a.d.ts_", + generated = ":tsconfig", +) diff --git a/packages/typescript/test/ts_project/declaration_only/a.d.ts_ b/packages/typescript/test/ts_project/declaration_only/a.d.ts_ new file mode 100644 index 0000000000..106d610ab9 --- /dev/null +++ b/packages/typescript/test/ts_project/declaration_only/a.d.ts_ @@ -0,0 +1 @@ +export declare const a: string; diff --git a/packages/typescript/test/ts_project/declaration_only/a.ts b/packages/typescript/test/ts_project/declaration_only/a.ts new file mode 100644 index 0000000000..db233fb40c --- /dev/null +++ b/packages/typescript/test/ts_project/declaration_only/a.ts @@ -0,0 +1 @@ +export const a: string = ''; diff --git a/packages/typescript/test/ts_project/declaration_only/tsconfig.json b/packages/typescript/test/ts_project/declaration_only/tsconfig.json new file mode 100644 index 0000000000..e929240d2d --- /dev/null +++ b/packages/typescript/test/ts_project/declaration_only/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "declaration": true, + "emitDeclarationOnly": true, + "types": [] + } +}