Skip to content

Commit

Permalink
feat: add JsInfo provider to ts_config rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Feb 11, 2023
1 parent 914bb40 commit 9a25c4d
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ bazel-output-base
examples/node_modules
examples/linked/node_modules
examples/linked_consumer/node_modules
examples/linked_tsconfig/node_modules
examples/linked_tsconfig_consumer/node_modules
19 changes: 19 additions & 0 deletions examples/linked_tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")

npm_link_all_packages()

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
":node_modules/@tsconfig/node18",
],
)

npm_package(
name = "linked_tsconfig",
srcs = [":tsconfig"],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions examples/linked_tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@lib/tsconfig",
"private": true,
"dependencies": {
"@tsconfig/node18": "1.0.1"
}
}
3 changes: 3 additions & 0 deletions examples/linked_tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node18/tsconfig.json"
}
23 changes: 23 additions & 0 deletions examples/linked_tsconfig_consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")

npm_link_all_packages()

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
":node_modules/@lib/tsconfig",
],
)

ts_project(
name = "lib",
srcs = [
"index.ts",
],
tsconfig = ":tsconfig",
deps = [
":node_modules/@types/node",
],
)
1 change: 1 addition & 0 deletions examples/linked_tsconfig_consumer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world')
7 changes: 7 additions & 0 deletions examples/linked_tsconfig_consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"@lib/tsconfig": "workspace:*",
"@types/node": "18.13.0"
}
}
4 changes: 4 additions & 0 deletions examples/linked_tsconfig_consumer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"//": "the hacky path below is required because while @lib/tsconfig/tsconfig.json _will_ resolve, tsc won't follow the symlink into the virtual store so the transitive npm dep of @lib/tsconfig on @tsconfig/node18/tsconfig.json won't be resolvable",
"extends": "../node_modules/.aspect_rules_js/@[email protected]/node_modules/@lib/tsconfig/tsconfig.json"
}
18 changes: 18 additions & 0 deletions examples/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
packages:
- linked
- linked_consumer
- linked_tsconfig
- linked_tsconfig_consumer
47 changes: 45 additions & 2 deletions ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_file_to_bin_action", "copy_files_to_bin_actions")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "js_info")
load(":ts_lib.bzl", _lib = "lib")

TsConfigInfo = provider(
Expand All @@ -30,21 +31,63 @@ TsConfigInfo = provider(

def _ts_config_impl(ctx):
files = [copy_file_to_bin_action(ctx, ctx.file.src)]

transitive_deps = [
depset(copy_files_to_bin_actions(ctx, ctx.files.deps)),
js_lib_helpers.gather_files_from_js_providers(
targets = ctx.attr.deps,
include_transitive_sources = True,
include_declarations = True,
include_npm_linked_packages = True,
)
),
]

# TODO: now that ts_config.bzl provides a JsInfo, we should be able to remove TsConfigInfo in the future
# since transitive files will now be passed through transitive_declarations in JsInfo
for dep in ctx.attr.deps:
if TsConfigInfo in dep:
transitive_deps.append(dep[TsConfigInfo].deps)

transitive_sources = js_lib_helpers.gather_transitive_sources([], ctx.attr.deps)

transitive_declarations = js_lib_helpers.gather_transitive_declarations(files, ctx.attr.deps)

npm_linked_packages = js_lib_helpers.gather_npm_linked_packages(
srcs = [],
deps = ctx.attr.deps,
)

npm_package_store_deps = js_lib_helpers.gather_npm_package_store_deps(
targets = ctx.attr.deps,
)

files_depset = depset(files)

runfiles = js_lib_helpers.gather_runfiles(
ctx = ctx,
sources = depset(), # tsconfig.json file won't be needed at runtime
data = [],
deps = ctx.attr.deps,
)

return [
DefaultInfo(files = depset(files)),
DefaultInfo(
files = files_depset,
runfiles = runfiles,
),
js_info(
# provide tsconfig.json file via `declarations` and not `sources` since they are only needed
# for downstream ts_project rules and not in downstream runtime binary rules
declarations = files_depset,
npm_linked_package_files = npm_linked_packages.direct_files,
npm_linked_packages = npm_linked_packages.direct,
npm_package_store_deps = npm_package_store_deps,
sources = depset(),
transitive_declarations = transitive_declarations,
transitive_npm_linked_package_files = npm_linked_packages.transitive_files,
transitive_npm_linked_packages = npm_linked_packages.transitive,
transitive_sources = transitive_sources,
),
TsConfigInfo(deps = depset(files, transitive = transitive_deps)),
]

Expand Down

0 comments on commit 9a25c4d

Please sign in to comment.