Skip to content

Commit

Permalink
fix(npm): npm_link_package() in subdirectory to js_library() package
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Jul 26, 2024
1 parent b0171d2 commit 79a38ff
Show file tree
Hide file tree
Showing 24 changed files with 674 additions and 1 deletion.
1 change: 1 addition & 0 deletions e2e/npm_link_package-rerooted/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root/node_modules/
1 change: 1 addition & 0 deletions e2e/npm_link_package-rerooted/.bazeliskrc
15 changes: 15 additions & 0 deletions e2e/npm_link_package-rerooted/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Import Aspect bazelrc presets
try-import %workspace%/../../.aspect/bazelrc/bazel7.bazelrc
import %workspace%/../../.aspect/bazelrc/convenience.bazelrc
import %workspace%/../../.aspect/bazelrc/correctness.bazelrc
import %workspace%/../../.aspect/bazelrc/debug.bazelrc
import %workspace%/../../.aspect/bazelrc/javascript.bazelrc
import %workspace%/../../.aspect/bazelrc/performance.bazelrc

### YOUR PROJECT SPECIFIC OPTIONS GO HERE ###

# Load any settings & overrides specific to the current user from `.aspect/bazelrc/user.bazelrc`.
# This file should appear in `.gitignore` so that settings are not shared with team members. This
# should be last statement in this config so the user configuration is able to overwrite flags from
# this file. See https://bazel.build/configure/best-practices#bazelrc-file.
try-import %workspace%/../../.aspect/bazelrc/user.bazelrc
1 change: 1 addition & 0 deletions e2e/npm_link_package-rerooted/.bazelversion
4 changes: 4 additions & 0 deletions e2e/npm_link_package-rerooted/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Disabling pnpm [hoisting](https://pnpm.io/npmrc#hoist) by setting `hoist=false` is recommended on
# projects using rules_js so that pnpm outside of Bazel lays out a node_modules tree similar to what
# rules_js lays out under Bazel (without a hidden node_modules/.pnpm/node_modules)
hoist=false
Empty file.
51 changes: 51 additions & 0 deletions e2e/npm_link_package-rerooted/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local_repository(
name = "aspect_rules_js",
path = "../..",
)

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

rules_js_dependencies()

load("@aspect_rules_js//js:toolchains.bzl", "rules_js_register_toolchains")

rules_js_register_toolchains(node_version = "16.14.2")

load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
lifecycle_hooks_envs = {
"*": [
"aspect_test_id=testc",
"BUILD_VERSION=1",
],
# Without APPDATA environment variable, sharp install will fail because of the binary can not
# be copied to ~/.npm folder which is readonly for bazel.
"sharp": ["APPDATA=."],
},
lifecycle_hooks_execution_requirements = {
"sharp": ["requires-network"],
},
npm_package_target_name = "{dirname}_pkg",
npmrc = "//:.npmrc",
pnpm_lock = "//root:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

local_repository(
name = "rules_foo",
path = "../rules_foo",
)

load("@rules_foo//foo:repositories.bzl", "foo_repositories")

foo_repositories()

load("@foo//:npm_repositories.bzl", foo_npm_repositories = "npm_repositories")

foo_npm_repositories()
28 changes: 28 additions & 0 deletions e2e/npm_link_package-rerooted/root/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
load("@foo//:@aspect-test/a/package_json.bzl", aspect_test_a_bin = "bin")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

npm_link_package(
name = "node_modules/@e2e/lib",
src = "//root/lib:lib_pkg",
package = "@e2e/lib",
root_package = "root",
)

npm_link_package(
name = "node_modules/@e2e/pkg",
src = "//root/pkg:lib_pkg",
root_package = "root",
)

npm_link_package(
name = "node_modules/@e2e/wrapper-lib",
src = "//root/wrapper-lib:wrapper-lib_pkg",
root_package = "root",
)

aspect_test_a_bin.bin_a_test(
name = "aspect_test_a_bin_test",
)
15 changes: 15 additions & 0 deletions e2e/npm_link_package-rerooted/root/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")

js_library(
name = "lib_pkg",
srcs = [
"index.js",
"package.json",
],
visibility = ["//visibility:public"],
deps = [
# uvu is a runtime dependency of this package and should be linked into its direct dependencies
# by the downstream npm_link_package
"//root:node_modules/uvu",
],
)
9 changes: 9 additions & 0 deletions e2e/npm_link_package-rerooted/root/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const packageJson = require('./package.json')
const assert = require('uvu/assert')
assert.is(2 + 2, 4)
module.exports = {
id: () =>
`${packageJson.name}@${
packageJson.version ? packageJson.version : '0.0.0'
}`,
}
4 changes: 4 additions & 0 deletions e2e/npm_link_package-rerooted/root/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@e2e/lib",
"private": true
}
14 changes: 14 additions & 0 deletions e2e/npm_link_package-rerooted/root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "simple",
"dependencies": {
"@aspect-test/a": "5.0.2",
"sharp": "0.32.1",
"uvu": "0.5.6"
},
"devDependencies": {
"@aspect-test/b": "5.0.2"
},
"optionalDependencies": {
"@aspect-test/c": "2.0.2"
}
}
18 changes: 18 additions & 0 deletions e2e/npm_link_package-rerooted/root/pkg/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")

npm_package(
name = "lib_pkg",
srcs = [
"index.js",
"package.json",
],
data = [
# A locally linked package as a runtime dependency
"//root:node_modules/@e2e/lib",
# uvu is a runtime dependency of this package and should be linked into its direct dependencies
# by the downstream npm_link_package
"//root:node_modules/uvu",
],
package = "@e2e/pkg",
visibility = ["//visibility:public"],
)
10 changes: 10 additions & 0 deletions e2e/npm_link_package-rerooted/root/pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const packageJson = require('./package.json')
const assert = require('uvu/assert')
const lib = require('@e2e/lib')
assert.is(2 + 2, 4)
module.exports = {
id: () =>
`${packageJson.name}@${
packageJson.version ? packageJson.version : '0.0.0'
}` + lib.id(),
}
4 changes: 4 additions & 0 deletions e2e/npm_link_package-rerooted/root/pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@e2e/pkg",
"private": true
}
Loading

0 comments on commit 79a38ff

Please sign in to comment.