Skip to content

Commit ff7bcb4

Browse files
authored
fix: add support for swc_version_from to bzlmod extension (#298)
1 parent f8f5a88 commit ff7bcb4

File tree

8 files changed

+184
-11
lines changed

8 files changed

+184
-11
lines changed

docs/repositories.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/smoke/.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

e2e/smoke/.npmrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Disabling pnpm [hoisting](https://pnpm.io/npmrc#hoist) by setting `hoist=false` is recommended on
2+
# projects using rules_js so that pnpm outside of Bazel lays out a node_modules tree similar to what
3+
# rules_js lays out under Bazel (without a hidden node_modules/.pnpm/node_modules)
4+
hoist=false

e2e/smoke/MODULE.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ local_path_override(
66

77
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)
88

9+
bazel_dep(name = "aspect_rules_js", version = "2.0.0")
10+
11+
npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)
12+
npm.npm_translate_lock(
13+
name = "npm",
14+
npmrc = "//:.npmrc",
15+
pnpm_lock = "//:pnpm-lock.yaml",
16+
verify_node_modules_ignored = "//:.bazelignore",
17+
)
18+
use_repo(npm, "npm")
19+
920
# Optional: specify a custom swc toolchain instead of the default
1021
swc = use_extension("@aspect_rules_swc//swc:extensions.bzl", "swc", dev_dependency = True)
1122
swc.toolchain(
1223
name = "swc",
13-
swc_version = "v1.3.78",
24+
swc_version_from = "@npm//:@swc/core/resolved.json",
1425
)

e2e/smoke/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@swc/core": "~1.11.0"
4+
}
5+
}

e2e/smoke/pnpm-lock.yaml

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swc/extensions.bzl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ load(":repositories.bzl", "swc_register_toolchains")
55
swc_toolchain = tag_class(attrs = {
66
"name": attr.string(doc = "Base name for generated repositories"),
77
"swc_version": attr.string(doc = "Explicit version of @swc/core. If provided, the package.json is not read."),
8-
# TODO: support this variant
9-
# "swc_version_from": attr.string(doc = "Location of package.json which may have a version for @swc/core."),
8+
"swc_version_from": attr.label(doc = "Location of package.json which may have a version for @swc/core."),
109
})
1110

1211
default_repository = "swc"
@@ -23,7 +22,7 @@ def _toolchain_extension(module_ctx):
2322
# Prioritize the root-most registration of the default toolchain version and
2423
# ignore any further registrations (modules are processed breadth-first)
2524
continue
26-
if toolchain.swc_version == registrations[toolchain.name]:
25+
if toolchain.swc_version == registrations[toolchain.name].swc_version and toolchain.swc_version_from == registrations[toolchain.name].swc_version_from:
2726
# No problem to register a matching toolchain twice
2827
continue
2928
fail("Multiple conflicting toolchains declared for name {} ({} and {}".format(
@@ -32,11 +31,16 @@ def _toolchain_extension(module_ctx):
3231
registrations[toolchain.name],
3332
))
3433
else:
35-
registrations[toolchain.name] = toolchain.swc_version
36-
for name, swc_version in registrations.items():
34+
registrations[toolchain.name] = struct(
35+
swc_version = toolchain.swc_version,
36+
swc_version_from = toolchain.swc_version_from,
37+
)
38+
39+
for name, registration in registrations.items():
3740
swc_register_toolchains(
3841
name = name,
39-
swc_version = swc_version,
42+
swc_version = registration.swc_version,
43+
swc_version_from = registration.swc_version_from,
4044
register = False,
4145
)
4246

swc/repositories.bzl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ def _determine_version(rctx):
7171

7272
json_path = rctx.path(rctx.attr.swc_version_from)
7373
p = json.decode(rctx.read(json_path))
74-
if "devDependencies" in p.keys() and _NPM_PKG in p["devDependencies"]:
74+
75+
# Allow use of "resolved.json", see https://github.com/aspect-build/rules_js/pull/1221
76+
if "$schema" in p.keys() and p["$schema"] == "https://docs.aspect.build/rules/aspect_rules_js/docs/npm_translate_lock":
77+
v = p["version"]
78+
elif "devDependencies" in p.keys() and _NPM_PKG in p["devDependencies"]:
7579
v = p["devDependencies"][_NPM_PKG]
7680
elif "dependencies" in p.keys() and _NPM_PKG in p["dependencies"]:
7781
v = p["dependencies"][_NPM_PKG]
@@ -147,8 +151,15 @@ def swc_register_toolchains(name, swc_version = None, swc_version_from = None, r
147151
148152
Args:
149153
name: base name for all created repos; we recommend `swc`
150-
swc_version_from: label of a json file (typically `package.json`) which declares an exact `@swc/core` version
151-
in a dependencies or devDependencies property.
154+
swc_version_from: label of a json file which declares an `@swc/core` version.
155+
156+
This may be a `package.json` file, with "@swc/core" in the dependencies or
157+
devDependencies property, and the version exactly specified.
158+
159+
With rules_js v1.32.0 or greater, it may also be a `resolved.json` file
160+
produced by `npm_translate_lock`, such as
161+
`@npm//path/to/linked:@swc/core/resolved.json`
162+
152163
Exactly one of `swc_version` or `swc_version_from` must be set.
153164
swc_version: version of the swc project, from https://github.com/swc-project/swc/releases
154165
Exactly one of `swc_version` or `swc_version_from` must be set.

0 commit comments

Comments
 (0)