Skip to content

Commit

Permalink
refactor: stop using @npm repository for alias; instead use target na…
Browse files Browse the repository at this point in the history
…mes where node_modules are linked such as :node_modules/@foo/bar
  • Loading branch information
gregmagolan committed Jun 3, 2022
1 parent 885e68c commit 8a31364
Show file tree
Hide file tree
Showing 24 changed files with 449 additions and 516 deletions.
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ link_all_npm_packages(name = "node_modules")
# Link the acorn package, which was fetched separately with npm_import from /WORKSPACE, to the
# virtual store in bazel-bin/node_modules/.aspect_rules_js
link_acorn(
name = "link_acorn",
name = "node_modules/acorn",
# `direct` set to False as an example of *not* also linking this 3rd dependency as a
# direct dependency in the package at bazel-bin/node_modules/@mycorp/mylib. Alternately,
# you may specify link_packages in the npm_import of this package and direct is then
Expand All @@ -21,7 +21,7 @@ link_acorn(

# Linking a first-party dependency to the virtual store in bazel-bin/node_modules/.aspect_rules_js
link_npm_package(
name = "link_mycorp_mylib",
name = "node_modules/@mycorp/mylib",
src = "//examples/lib",
# `direct` set to False as an example of *not* also linking this first-party dependency as a
# direct dependency in the package at bazel-bin/node_modules/@mycorp/mylib.
Expand Down
8 changes: 4 additions & 4 deletions docs/link_npm_package.md

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

22 changes: 15 additions & 7 deletions docs/npm_import.md

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

4 changes: 1 addition & 3 deletions e2e/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ link_all_npm_packages(name = "node_modules")
js_test(
name = "test",
data = [
"//:direct_link__chalk",
# these don't work yet:
# "@npm//chalk",
":node_modules/chalk",
],
entry_point = "main.mjs",
)
2 changes: 1 addition & 1 deletion e2e/link_npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@rules_foo_npm//foo/@aspect-test/a:package_json.bzl", aspect_test_a_bin =
link_all_npm_packages(name = "node_modules")

link_npm_package(
name = "linked_e2e_lib",
name = "node_modules/@e2e/lib",
src = "//lib",
visibility = ["//visibility:public"],
)
Expand Down
20 changes: 10 additions & 10 deletions e2e/link_npm_package/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ js_binary(
name = "main",
args = ["foo"],
data = [
"//:linked_e2e_lib",
"@npm//@aspect-test/a",
"@npm//@aspect-test/b",
"@npm//@aspect-test/c",
"@rules_foo_npm//foo/@aspect-test/a",
"//:node_modules/@aspect-test/a",
"//:node_modules/@aspect-test/b",
"//:node_modules/@aspect-test/c",
"//:node_modules/@e2e/lib",
"@rules_foo//foo:node_modules/@aspect-test/a",
],
entry_point = "main.js",
)
Expand All @@ -17,11 +17,11 @@ js_test(
name = "test",
args = ["foo"],
data = [
"//:linked_e2e_lib",
"@npm//@aspect-test/a",
"@npm//@aspect-test/b",
"@npm//@aspect-test/c",
"@rules_foo_npm//foo/@aspect-test/a",
"//:node_modules/@aspect-test/a",
"//:node_modules/@aspect-test/b",
"//:node_modules/@aspect-test/c",
"//:node_modules/@e2e/lib",
"@rules_foo//foo:node_modules/@aspect-test/a",
],
entry_point = "main.js",
log_level = "info",
Expand Down
8 changes: 4 additions & 4 deletions e2e/pnpm_workspace/app/a/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ js_binary(
name = "main",
args = ["foo"],
data = [
"@npm//@aspect-test",
"@npm//app/a/@aspect-test",
"@npm//app/a/@lib",
":node_modules/@aspect-test",
":node_modules/@lib",
"//:node_modules/@aspect-test",
],
entry_point = "main.js",
)
Expand All @@ -20,7 +20,7 @@ js_test(
args = ["foo"],
data = [
":node_modules",
"@npm//@aspect-test",
"//:node_modules/@aspect-test",
],
entry_point = "main.js",
log_level = "info",
Expand Down
12 changes: 6 additions & 6 deletions e2e/pnpm_workspace/app/b/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ js_binary(
name = "main",
args = ["foo"],
data = [
"@npm//@aspect-test/a",
"@npm//@aspect-test/b",
"@npm//@aspect-test/c",
"@npm//app/b/@aspect-test/h",
"@npm//app/b/@lib/b",
"@npm//app/b/@lib/b_alias",
":node_modules/@aspect-test/h",
":node_modules/@lib/b",
":node_modules/@lib/b_alias",
"//:node_modules/@aspect-test/a",
"//:node_modules/@aspect-test/b",
"//:node_modules/@aspect-test/c",
],
entry_point = "main.js",
)
Expand Down
8 changes: 4 additions & 4 deletions e2e/pnpm_workspace_dot_dot/app/a/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ js_binary(
name = "main",
args = ["foo"],
data = [
"@npm//app/a/@aspect-test",
"@npm//app/a/@lib",
"@npm//root/@aspect-test",
":node_modules/@aspect-test",
":node_modules/@lib",
"//root:node_modules/@aspect-test",
],
entry_point = "main.js",
)
Expand All @@ -20,7 +20,7 @@ js_test(
args = ["foo"],
data = [
":node_modules",
"@npm//root/@aspect-test",
"//root:node_modules/@aspect-test",
],
entry_point = "main.js",
log_level = "info",
Expand Down
18 changes: 9 additions & 9 deletions e2e/pnpm_workspace_dot_dot/app/b/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ js_binary(
name = "main",
args = ["foo"],
data = [
"@npm//app/b/@aspect-test/a",
"@npm//app/b/@aspect-test/b",
"@npm//app/b/@aspect-test/c",
"@npm//app/b/@aspect-test/h",
"@npm//app/b/@lib/b",
"@npm//app/b/@lib/b_alias",
"@npm//root/@aspect-test/a",
"@npm//root/@aspect-test/b",
"@npm//root/@aspect-test/c",
":node_modules/@aspect-test/a",
":node_modules/@aspect-test/b",
":node_modules/@aspect-test/c",
":node_modules/@aspect-test/h",
":node_modules/@lib/b",
":node_modules/@lib/b_alias",
"//root:node_modules/@aspect-test/a",
"//root:node_modules/@aspect-test/b",
"//root:node_modules/@aspect-test/c",
],
entry_point = "main.js",
)
Expand Down
4 changes: 2 additions & 2 deletions e2e/rules_foo/foo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ link_all_npm_packages(name = "node_modules")
js_binary(
name = "main",
data = [
"@rules_foo_npm//foo/@aspect-test/a",
"@rules_foo//foo:node_modules/@aspect-test/a",
],
entry_point = "main.js",
)

js_test(
name = "test",
data = [
"@rules_foo_npm//foo/@aspect-test/a",
"@rules_foo//foo:node_modules/@aspect-test/a",
],
entry_point = "main.js",
log_level = "info",
Expand Down
11 changes: 5 additions & 6 deletions examples/genrule/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_js//npm:defs.bzl", "constants")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@acorn__8.4.0__links//:defs.bzl", link_acorn = "link_npm_package")

# Link the acorn package, which was fetched separately with npm_import from /WORKSPACE, as
# a direct dependency in bazel-bin/examples/genrule/node_modules
link_acorn(name = "link_acorn")
link_acorn(name = "node_modules/acorn")

#############################
# Test case 1
Expand Down Expand Up @@ -62,13 +61,13 @@ genrule(
name = "call_acorn",
srcs = [
":one.js",
":link_acorn",
":link_acorn{}".format(constants.dir_suffix),
":node_modules/acorn",
":node_modules/acorn/dir",
],
outs = ["actual2"],
cmd = " ".join([
"$(NODE_PATH)",
"./$(execpath :link_acorn{})/bin/acorn".format(constants.dir_suffix),
"./$(execpath :node_modules/acorn/dir)/bin/acorn",
"--compact",
"$(execpath :one.js)",
# $@ is bazel shorthand for the path of the output file
Expand Down Expand Up @@ -98,7 +97,7 @@ genrule(
name = "require_acorn",
srcs = [
":require_acorn_js",
":link_acorn",
":node_modules/acorn",
],
outs = ["actual3"],
cmd = """
Expand Down
14 changes: 7 additions & 7 deletions examples/js_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@npm//examples/npm_deps/@aspect-test/a:package_json.bzl", aspect_test_a_bin = "bin")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_test", "run_js_binary")
load("@aspect_rules_js//npm:defs.bzl", "constants", "link_npm_package")
load("@aspect_rules_js//npm:defs.bzl", "link_npm_package")

# Link the acorn package, which was fetched separately with npm_import from /WORKSPACE, as
# a direct dependency in bazel-bin/examples/js_binary/node_modules
link_acorn(name = "link_acorn")
link_acorn(name = "node_modules/acorn")

# Link @mycorp/mylib as a direct dependency in bazel-bin/examples/js_binary/node_modules
link_npm_package(name = "link_mycorp_mylib")
link_npm_package(name = "node_modules/@mycorp/mylib")

####################################################
# Test case 4
Expand All @@ -20,7 +20,7 @@ link_npm_package(name = "link_mycorp_mylib")

js_binary(
name = "bin",
data = [":link_acorn"],
data = [":node_modules/acorn"],
entry_point = "require_acorn.js",
)

Expand Down Expand Up @@ -49,7 +49,7 @@ diff_test(

js_test(
name = "test_test",
data = ["@npm//@types/node"],
data = ["//:node_modules/@types/node"],
entry_point = "test.js",
)

Expand All @@ -67,7 +67,7 @@ write_file(

js_binary(
name = "bin6",
data = [":link_mycorp_mylib"],
data = [":node_modules/@mycorp/mylib"],
entry_point = "case6.js",
)

Expand Down Expand Up @@ -278,7 +278,7 @@ run_js_binary(

directory_path(
name = "acorn_entry_point",
directory = ":link_acorn{}".format(constants.dir_suffix),
directory = ":node_modules/acorn/dir",
path = "bin/acorn",
)

Expand Down
4 changes: 2 additions & 2 deletions examples/macro/mocha.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def mocha_test(name, srcs, args = [], data = [], env = {}, **kwargs):
data = data + [
"_%s_srcs" % name,
"//examples/macro:mocha_reporters.json",
"@npm//examples/macro/mocha-multi-reporters",
"@npm//examples/macro/mocha-junit-reporter",
"//examples/macro:node_modules/mocha-multi-reporters",
"//examples/macro:node_modules/mocha-junit-reporter",
],
env = dict(env, **{
# Add environment variable so that mocha writes its test xml
Expand Down
Loading

0 comments on commit 8a31364

Please sign in to comment.