Skip to content

Commit

Permalink
feat: allow definition of alternative typescript repositories (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Aug 23, 2024
1 parent d1f0efb commit 3cfbd4a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/repositories.md

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

10 changes: 10 additions & 0 deletions e2e/external_dep/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ ts_project(
visibility = ["//visibility:public"],
)

# Demonstrate how to use a different typescript where required
# Picks up TypeScript 3.9.9 defined in MODULE.bazel
ts_project(
name = "lib.ts3",
srcs = ["lib.ts"],
composite = True,
out_dir = "ts3_out",
tsc = "@npm_typescript3//:tsc",
)

build_test(
name = "test",
targets = [":lib"],
Expand Down
8 changes: 7 additions & 1 deletion e2e/external_dep/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)

rules_ts_ext = use_extension("@aspect_rules_ts//ts:extensions.bzl", "ext", dev_dependency = True)
rules_ts_ext.deps()
use_repo(rules_ts_ext, "npm_typescript")

# Example demonstrating that you can also have a second typescript version
rules_ts_ext.deps(
name = "npm_typescript3",
ts_version = "3.9.9",
)
use_repo(rules_ts_ext, "npm_typescript", "npm_typescript3")
5 changes: 5 additions & 0 deletions e2e/external_dep/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ load("@aspect_rules_ts//ts:repositories.bzl", "LATEST_TYPESCRIPT_VERSION", "rule

rules_ts_dependencies(ts_version = LATEST_TYPESCRIPT_VERSION)

rules_ts_dependencies(
name = "npm_typescript3",
ts_version = "3.9.9",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()
Expand Down
2 changes: 2 additions & 0 deletions ts/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def _extension_impl(module_ctx):
if not ts_version and not attr.ts_version_from:
ts_version = LATEST_TYPESCRIPT_VERSION
npm_dependencies(
name = attr.name,
ts_version = ts_version,
ts_version_from = attr.ts_version_from,
ts_integrity = attr.ts_integrity,
Expand All @@ -22,6 +23,7 @@ ext = module_extension(
implementation = _extension_impl,
tag_classes = {
"deps": tag_class(attrs = {
"name": attr.string(default = "npm_typescript"),
"ts_version": attr.string(),
"ts_version_from": attr.label(),
"ts_integrity": attr.string(),
Expand Down
4 changes: 2 additions & 2 deletions ts/private/npm_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ http_archive_version = repository_rule(
)

# buildifier: disable=function-docstring
def npm_dependencies(ts_version_from = None, ts_version = None, ts_integrity = None, bzlmod = False):
def npm_dependencies(name = "npm_typescript", ts_version_from = None, ts_version = None, ts_integrity = None, bzlmod = False):
if (ts_version and ts_version_from) or (not ts_version_from and not ts_version):
fail("""Exactly one of 'ts_version' or 'ts_version_from' must be set.""")

maybe(
http_archive_version,
name = "npm_typescript",
name = name,
bzlmod = bzlmod,
version = ts_version,
version_from = ts_version_from,
Expand Down
4 changes: 3 additions & 1 deletion ts/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def rules_ts_bazel_dependencies():
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.2.0/rules_nodejs-v6.2.0.tar.gz",
)

def rules_ts_dependencies(ts_version_from = None, ts_version = None, ts_integrity = None):
def rules_ts_dependencies(name = "npm_typescript", ts_version_from = None, ts_version = None, ts_integrity = None):
"""Dependencies needed by users of rules_ts.
To skip fetching the typescript package, call `rules_ts_bazel_dependencies` instead.
Args:
name: name of the resulting external repository containing the TypeScript compiler.
ts_version_from: label of a json file which declares a typescript version.
This may be a `package.json` file, with "typescript" in the dependencies or
Expand All @@ -76,6 +77,7 @@ def rules_ts_dependencies(ts_version_from = None, ts_version = None, ts_integrit
rules_ts_bazel_dependencies()

npm_dependencies(
name = name,
ts_version_from = ts_version_from,
ts_version = ts_version,
ts_integrity = ts_integrity,
Expand Down

0 comments on commit 3cfbd4a

Please sign in to comment.