Skip to content

Commit

Permalink
test: consuming linked js_library vs npm_package (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Jul 19, 2024
1 parent b7a5711 commit 6f54733
Show file tree
Hide file tree
Showing 18 changed files with 5,436 additions and 4,897 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-2065072158
examples/js_binary/package.json=-41174383
examples/linked_consumer/package.json=-1109186228
examples/linked_empty_node_modules/package.json=-1039372825
examples/linked_lib/package.json=1590632845
examples/linked_pkg/package.json=-726181961
examples/macro/package.json=857146175
examples/npm_deps/package.json=-929156430
examples/npm_deps/patches/[email protected]=-442666336
Expand All @@ -24,5 +27,5 @@ npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/vendored/semver-max/package.json=578664053
package.json=-275319675
pnpm-lock.yaml=2129089772
pnpm-workspace.yaml=-1282513813
pnpm-lock.yaml=76490674
pnpm-workspace.yaml=-884103165
3 changes: 3 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
e2e/
examples/js_binary/node_modules/
examples/linked_consumer/node_modules
examples/linked_empty_node_modules/node_modules
examples/linked_lib/node_modules
examples/linked_pkg/node_modules
examples/macro/node_modules/
examples/npm_deps/node_modules/
examples/npm_package/libs/lib_a/node_modules/
Expand Down
3 changes: 3 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ npm_translate_lock(
"//:package.json",
"//:pnpm-workspace.yaml",
"//examples/js_binary:package.json",
"//examples/linked_consumer:package.json",
"//examples/linked_empty_node_modules:package.json",
"//examples/linked_lib:package.json",
"//examples/linked_pkg:package.json",
"//examples/macro:package.json",
"//examples/npm_deps:package.json",
"//examples/npm_package/libs/lib_a:package.json",
Expand Down
24 changes: 24 additions & 0 deletions examples/linked_consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

# Exposed by js_library()
js_test(
name = "direct",
data = [
"//examples/linked_lib:lib",
"//examples/linked_pkg:lib",
],
entry_point = "test_file_all_linked.js",
)

# Exposed by linked npm deps
js_test(
name = "pkg",
data = [
":node_modules/@lib/test",
":node_modules/@lib/test2",
],
entry_point = "test_pkg_deps_linked.js",
)
8 changes: 8 additions & 0 deletions examples/linked_consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "tests",
"private": true,
"dependencies": {
"@lib/test": "workspace:*",
"@lib/test2": "workspace:*"
}
}
20 changes: 20 additions & 0 deletions examples/linked_consumer/test_file_all_linked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Asserts all dependencies are available directly from the file

const testLib = require('../linked_pkg/index')
const testLib2 = require('../linked_lib/index')

console.log(
'loaded dependency',
testLib.importDep().id(),
testLib2.importDep().id()
)
console.log(
'loaded devDependency',
testLib.importDevDep().id(),
testLib2.importDevDep().id()
)
console.log(
'loaded aliased dependency',
testLib.importAliasedDep().id(),
testLib2.importAliasedDep().id()
)
39 changes: 39 additions & 0 deletions examples/linked_consumer/test_pkg_deps_linked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Asserts *only non-dev* dependencies are available from the npm package

const testLib = require('@lib/test')
const testLib2 = require('@lib/test2')

console.log(
'loaded dependency',
testLib.importDep().id(),
testLib2.importDep().id()
)
console.log(
'loaded aliased dependency',
testLib.importAliasedDep().id(),
testLib2.importAliasedDep().id()
)

let e = null
try {
testLib.importDevDep()
} catch (ex) {
e = ex
}
if (e == null) {
throw new Error('devDependency should NOT be available')
}

let e2 = null
try {
testLib2.importDevDep()
} catch (ex) {
e2 = ex
}
if (e2 == null) {
console.error(
'BUG: devDependency should NOT be available when linking js_library()'
)
}

console.log('devDependency not available')
29 changes: 29 additions & 0 deletions examples/linked_lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

# A basic package with 2 simple dependencies (packages with no dependencies)
# Exposed via js_library()
js_library(
name = "pkg",
srcs = [
"index.js",
"package.json",
],
visibility = ["//visibility:public"],
deps = [
":node_modules/@aspect-test/e",
":node_modules/@aspect-test/f",
":node_modules/@types/node",
":node_modules/alias-e",
],
)

# When using js_library() as a package it can also serve as a "lib".
# Use an alias so both linked_lib and linked_pkg have the same target names.
alias(
name = "lib",
actual = ":pkg",
visibility = ["//visibility:public"],
)
3 changes: 3 additions & 0 deletions examples/linked_lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.importDep = () => require('@aspect-test/e')
exports.importDevDep = () => require('@aspect-test/f')
exports.importAliasedDep = () => require('alias-e')
12 changes: 12 additions & 0 deletions examples/linked_lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@lib/test2",
"private": true,
"dependencies": {
"@aspect-test/e": "1.0.0",
"alias-e": "npm:@aspect-test/e"
},
"devDependencies": {
"@types/node": "18.13.0",
"@aspect-test/f": "1.0.0"
}
}
27 changes: 27 additions & 0 deletions examples/linked_pkg/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

# A basic library with 2 simple dependencies (packages with no dependencies)
js_library(
name = "lib",
srcs = ["index.js"],
visibility = ["//visibility:public"],
deps = [
":node_modules/@aspect-test/e",
":node_modules/@aspect-test/f",
":node_modules/@types/node",
":node_modules/alias-e",
],
)

# Exposed via npm_package()
npm_package(
name = "pkg",
srcs = [
":lib",
],
visibility = ["//visibility:public"],
)
3 changes: 3 additions & 0 deletions examples/linked_pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.importDep = () => require('@aspect-test/e')
exports.importDevDep = () => require('@aspect-test/f')
exports.importAliasedDep = () => require('alias-e')
12 changes: 12 additions & 0 deletions examples/linked_pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@lib/test",
"private": true,
"dependencies": {
"@aspect-test/e": "1.0.0",
"alias-e": "npm:@aspect-test/e"
},
"devDependencies": {
"@types/node": "18.13.0",
"@aspect-test/f": "1.0.0"
}
}
Loading

0 comments on commit 6f54733

Please sign in to comment.