From 4888a4a1ce06eebee2b0f096cfe2ad5392102d45 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 28 May 2020 15:15:18 -0700 Subject: [PATCH] feat(typescript): ts_project is linkable This doesn't copy a package.json file, so it has to be resolvable with just index.ts --- packages/typescript/internal/ts_project.bzl | 18 ++++++++++++++++-- .../test/ts_project/linkable/app/BUILD.bazel | 8 ++++++++ .../test/ts_project/linkable/app/index.ts | 4 ++++ .../test/ts_project/linkable/app/tsconfig.json | 1 + .../test/ts_project/linkable/lib/BUILD.bazel | 8 ++++++++ .../test/ts_project/linkable/lib/index.ts | 1 + .../test/ts_project/linkable/lib/other.ts | 1 + .../test/ts_project/linkable/lib/tsconfig.json | 5 +++++ 8 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/typescript/test/ts_project/linkable/app/BUILD.bazel create mode 100644 packages/typescript/test/ts_project/linkable/app/index.ts create mode 100644 packages/typescript/test/ts_project/linkable/app/tsconfig.json create mode 100644 packages/typescript/test/ts_project/linkable/lib/BUILD.bazel create mode 100644 packages/typescript/test/ts_project/linkable/lib/index.ts create mode 100644 packages/typescript/test/ts_project/linkable/lib/other.ts create mode 100644 packages/typescript/test/ts_project/linkable/lib/tsconfig.json diff --git a/packages/typescript/internal/ts_project.bzl b/packages/typescript/internal/ts_project.bzl index f7f0f1d8bf..f66886ec69 100644 --- a/packages/typescript/internal/ts_project.bzl +++ b/packages/typescript/internal/ts_project.bzl @@ -1,6 +1,7 @@ "ts_project rule" -load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "NpmPackageInfo", "run_node") +load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "LinkablePackageInfo", "NpmPackageInfo", "run_node") +load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect") _DEFAULT_TSC = ( # BEGIN-INTERNAL @@ -10,6 +11,7 @@ _DEFAULT_TSC = ( ) _ATTRS = { + "package_name": attr.string(), # NB: no restriction on extensions here, because tsc sometimes adds type-check support # for more file kinds (like require('some.json')) and also # if you swap out the `compiler` attribute (like with ngtsc) @@ -19,7 +21,10 @@ _ATTRS = { "extends": attr.label_list(allow_files = [".json"]), "tsc": attr.label(default = Label(_DEFAULT_TSC), executable = True, cfg = "host"), "tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]), - "deps": attr.label_list(providers = [DeclarationInfo]), + "deps": attr.label_list( + aspects = [module_mappings_aspect], + providers = [DeclarationInfo], + ), } # tsc knows how to produce the following kinds of output files. @@ -144,6 +149,14 @@ def _ts_project_impl(ctx): ), ) + if ctx.attr.package_name: + path = "/".join([p for p in [ctx.bin_dir.path, ctx.label.workspace_root, ctx.label.package] if p]) + providers.append(LinkablePackageInfo( + package_name = ctx.attr.package_name, + path = path, + files = depset(typings_outputs, transitive = [runtime_outputs]), + )) + return providers ts_project = rule( @@ -350,6 +363,7 @@ def ts_project_macro( Instructs Bazel to expect a `.tsbuildinfo` output. emit_declaration_only: if the `emitDeclarationOnly` bit is set in the tsconfig. Instructs Bazel *not* to expect `.js` or `.js.map` outputs for `.ts` sources. + **kwargs: undocumented additional attributes to ts_project rule wrapped by this macro """ if srcs == None: diff --git a/packages/typescript/test/ts_project/linkable/app/BUILD.bazel b/packages/typescript/test/ts_project/linkable/app/BUILD.bazel new file mode 100644 index 0000000000..2b13ecd7e1 --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/app/BUILD.bazel @@ -0,0 +1,8 @@ +load("//packages/typescript:index.bzl", "ts_project") + +ts_project( + # This causes the linker to symlink the output directory into our execroot + # execroot/node_modules/@bazel/ts_project_lib -> + # bazel-out/[arch]/bin/packages/typescript/test/ts_project/linkable_lib + deps = ["//packages/typescript/test/ts_project/linkable/lib:tsconfig"], +) diff --git a/packages/typescript/test/ts_project/linkable/app/index.ts b/packages/typescript/test/ts_project/linkable/app/index.ts new file mode 100644 index 0000000000..9e97e9a3f7 --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/app/index.ts @@ -0,0 +1,4 @@ +import {a} from '@bazel/ts_project_lib'; +import {b} from '@bazel/ts_project_lib/other'; + +console.error(`got ${a} ${b}`); diff --git a/packages/typescript/test/ts_project/linkable/app/tsconfig.json b/packages/typescript/test/ts_project/linkable/app/tsconfig.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/app/tsconfig.json @@ -0,0 +1 @@ +{} diff --git a/packages/typescript/test/ts_project/linkable/lib/BUILD.bazel b/packages/typescript/test/ts_project/linkable/lib/BUILD.bazel new file mode 100644 index 0000000000..0ee34ff20b --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/lib/BUILD.bazel @@ -0,0 +1,8 @@ +load("//packages/typescript:index.bzl", "ts_project") + +# This directory will be linked into downstream rules in node_modules/@bazel/ts_project_lib +ts_project( + package_name = "@bazel/ts_project_lib", + declaration = True, + visibility = ["//packages/typescript/test:__subpackages__"], +) diff --git a/packages/typescript/test/ts_project/linkable/lib/index.ts b/packages/typescript/test/ts_project/linkable/lib/index.ts new file mode 100644 index 0000000000..d8c9a7d7e2 --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/lib/index.ts @@ -0,0 +1 @@ +export const a: number = 1; diff --git a/packages/typescript/test/ts_project/linkable/lib/other.ts b/packages/typescript/test/ts_project/linkable/lib/other.ts new file mode 100644 index 0000000000..28898f4406 --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/lib/other.ts @@ -0,0 +1 @@ +export const b: string = 'b'; diff --git a/packages/typescript/test/ts_project/linkable/lib/tsconfig.json b/packages/typescript/test/ts_project/linkable/lib/tsconfig.json new file mode 100644 index 0000000000..d5f8d1fb3f --- /dev/null +++ b/packages/typescript/test/ts_project/linkable/lib/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "declaration": true + } +}