Skip to content

Commit d670621

Browse files
committed
fix: consumption of @npm//:node_modules/{pkg}/package_json.bzl targets across workspaces
1 parent 74e5ded commit d670621

17 files changed

Lines changed: 129 additions & 241 deletions

e2e/bzlmod/BUILD.bazel

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ load("@bazel_lib//lib:testing.bzl", "assert_contains")
33
load("@bazel_skylib//rules:build_test.bzl", "build_test")
44
load("@npm//:defs.bzl", "npm_link_all_packages")
55
load("@npm//:jasmine/package_json.bzl", jasmine_bin = "bin")
6-
load("@npm//:less/package_json.bzl", less_bin = "bin")
76
load("@npm_meaning-of-life__links//:defs.bzl", npm_link_meaning_of_life = "npm_link_imported_package")
7+
load("@other_module//:less.bzl", "lessc")
88

99
not_windows = select({
1010
"@platforms//os:windows": ["@platforms//:incompatible"],
@@ -26,14 +26,9 @@ js_test(
2626
entry_point = "main.mjs",
2727
)
2828

29-
less_bin.lessc(
29+
lessc(
3030
name = "styles",
31-
srcs = ["my.less"],
32-
outs = ["my.css"],
33-
args = [
34-
"my.less",
35-
"my.css",
36-
],
31+
css = "my.less",
3732
)
3833

3934
assert_contains(
@@ -83,9 +78,13 @@ build_test(
8378
build_test(
8479
name = "other_module_run_binary_test",
8580
targets = [
86-
# TODO: https://github.com/aspect-build/rules_js/issues/2725
8781
# js_run_binary() in @other_repo
88-
# "@other_module//:my.css",
82+
"@other_module//:my.css",
83+
"@other_module//:styles",
84+
85+
# js_run_binary() with chdir in a subdir of @other_repo
86+
"@other_module//subdir:my.css",
87+
"@other_module//subdir:styles",
8988

9089
# js_run_binary() running a binary in @other_repo
9190
":run_other_module_binary_test",

e2e/bzlmod/MODULE.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ npm.npm_translate_lock(
2727
# See https://github.com/aspect-build/rules_js/pull/1142 for more info.
2828
custom_postinstalls = {
2929
"chalk": "node --version",
30-
"less": "npm --version",
3130
"jasmine": "tsc --version",
3231
},
3332
npmrc = "//:.npmrc",
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@npm_other_module//:defs.bzl", "npm_link_all_packages")
2-
load("@npm_other_module//:less/package_json.bzl", less_bin = "bin")
32
load("@npm_other_module//:pyright/package_json.bzl", pyright = "bin")
3+
load(":less.bzl", "lessc", "lessc_binary")
44

55
npm_link_all_packages()
66

@@ -16,18 +16,13 @@ genrule(
1616
cmd = """ echo ".foo { color: red }" > $@""",
1717
)
1818

19-
less_bin.lessc(
19+
lessc(
2020
name = "styles",
21-
srcs = ["my.less"],
22-
outs = ["my.css"],
23-
args = [
24-
"my.less",
25-
"my.css",
26-
],
21+
css = "my.less",
2722
visibility = ["//visibility:public"],
2823
)
2924

30-
less_bin.lessc_binary(
25+
lessc_binary(
3126
name = "lessc",
3227
visibility = ["//visibility:public"],
3328
)

e2e/bzlmod/other_module/less.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Macros wrapping the less compiler from the other_module npm packages."""
2+
3+
load("@npm_other_module//:less/package_json.bzl", less_bin = "bin")
4+
5+
def lessc(name, css, **kwargs):
6+
"""Compile a .less file to .css using the less compiler from other_module."""
7+
less_bin.lessc(
8+
name = name,
9+
srcs = [css],
10+
outs = [css.replace(".less", ".css")],
11+
args = [
12+
css,
13+
css.replace(".less", ".css"),
14+
],
15+
chdir = kwargs.pop("chdir", "."),
16+
**kwargs
17+
)
18+
19+
def lessc_binary(name, **kwargs):
20+
"""Create a standalone lessc binary from the less compiler in other_module."""
21+
less_bin.lessc_binary(
22+
name = name,
23+
**kwargs
24+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@npm_other_module//:less/package_json.bzl", less_bin = "bin")
2+
3+
# Verifies that js_run_binary() works in a subdirectory of an external module with chdir.
4+
less_bin.lessc(
5+
name = "styles",
6+
srcs = ["my.less"],
7+
outs = ["my.css"],
8+
args = [
9+
"my.less",
10+
"my.css",
11+
],
12+
chdir = package_name(),
13+
visibility = ["//visibility:public"],
14+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.box {
2+
width: 100px;
3+
}

e2e/bzlmod/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"packageManager": "[email protected]",
55
"dependencies": {
66
"chalk": "5.3.0",
7-
"less": "4.1.3",
87
"jasmine": "5.0.2"
98
},
109
"pnpm": {

0 commit comments

Comments
 (0)