Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): ts_project is linkable #1924

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript/test/ts_project/linkable/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
4 changes: 4 additions & 0 deletions packages/typescript/test/ts_project/linkable/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {a} from '@bazel/ts_project_lib';
import {b} from '@bazel/ts_project_lib/other';

console.error(`got ${a} ${b}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"types": []
}
}
8 changes: 8 additions & 0 deletions packages/typescript/test/ts_project/linkable/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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__"],
)
1 change: 1 addition & 0 deletions packages/typescript/test/ts_project/linkable/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: number = 1;
1 change: 1 addition & 0 deletions packages/typescript/test/ts_project/linkable/lib/other.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b: string = 'b';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"declaration": true,
"types": []
}
}