Skip to content

Commit

Permalink
Add rules_js_external_packages example
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Oct 10, 2023
1 parent a677fa4 commit 2eeffc2
Show file tree
Hide file tree
Showing 35 changed files with 407 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rules_js_external_packages/main/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
packages/vega/node_modules
packages/rigel/node_modules
1 change: 1 addition & 0 deletions rules_js_external_packages/main/.bazeliskrc
5 changes: 5 additions & 0 deletions rules_js_external_packages/main/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 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 rules_js_external_packages/main/.bazelversion
2 changes: 2 additions & 0 deletions rules_js_external_packages/main/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/bazel-*
6 changes: 6 additions & 0 deletions rules_js_external_packages/main/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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). See
# https://github.com/aspect-build/rules_js/blob/7377f2d0387cc2a9251137929b1c53ccdb3fbcf0/docs/npm_import.md#npm_translate_lock
# documentation for more information.
hoist=false
3 changes: 3 additions & 0 deletions rules_js_external_packages/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")
48 changes: 48 additions & 0 deletions rules_js_external_packages/main/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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

http_archive(
name = "aspect_bazel_lib",
sha256 = "cbf473d630ab67b36461d83b38fdc44e56f45b78d03c405e4958280211124d79",
strip_prefix = "bazel-lib-1.36.0",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.36.0/bazel-lib-v1.36.0.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

http_archive(
name = "aspect_rules_js",
sha256 = "7ab9776bcca823af361577a1a2ebb9a30d2eb5b94ecc964b8be360f443f714b2",
strip_prefix = "rules_js-1.32.6",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.32.6/rules_js-v1.32.6.tar.gz",
)

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

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

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

npm_translate_lock(
name = "npm",
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

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

npm_repositories()
6 changes: 6 additions & 0 deletions rules_js_external_packages/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "rules_js_external_packages",
"dependencies": {
"vega": "workspace:*"
}
}
4 changes: 4 additions & 0 deletions rules_js_external_packages/main/packages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":sync_npm_packages.bzl", "sync_npm_packages")
load("@other//packages:packages.bzl", OTHER_PACKAGES = "PACKAGES")

sync_npm_packages(packages = OTHER_PACKAGES)
41 changes: 41 additions & 0 deletions rules_js_external_packages/main/packages/rigel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

npm_package(
name = "rigel",
srcs = [
"index.mjs",
"package.json",
],
# This is a perf improvement; the default will be flipped to False in rules_js 2.0
include_runfiles = False,
visibility = ["//visibility:public"],
)

js_binary(
name = "rigel_bin",
data = [":node_modules"],
entry_point = "index.mjs",
)

js_run_binary(
name = "run_rigel_bin",
stdout = "run_rigel_bin_stdout",
tool = ":rigel_bin",
)

assert_contains(
name = "contains_rigel",
actual = ":run_rigel_bin_stdout",
expected = "rigel",
)

filegroup(
name = "srcs",
srcs = glob(["**/*"]),
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions rules_js_external_packages/main/packages/rigel/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import chalk from 'chalk';

export function whoami() {
return 'rigel';
}

console.log(chalk.blue(whoami()));
7 changes: 7 additions & 0 deletions rules_js_external_packages/main/packages/rigel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "rigel",
"dependencies": {
"chalk": "5.2.0"
},
"main": "index.mjs"
}
26 changes: 26 additions & 0 deletions rules_js_external_packages/main/packages/sync_npm_packages.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")

def sync_npm_packages(packages):
"""
Sync npm packages from @other to this bazel package
Args:
packages: A list of package names to sync
"""
files = {}

for p in packages:
copy_to_directory(
name = "copy_{}".format(p),
srcs = ["@other//packages/{}:srcs".format(p)],
include_external_repositories = ["other"],
root_paths = [],
replace_prefixes = {"packages/{}/".format(p): ""},
)
files[":{}".format(p)] = "copy_{}".format(p)

write_source_files(
name = "other_packages",
files = files,
)
47 changes: 47 additions & 0 deletions rules_js_external_packages/main/packages/vega/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

npm_package(
name = "vega",
srcs = [
"index.mjs",
"package.json",
],
# This is a perf improvement; the default will be flipped to False in rules_js 2.0
include_runfiles = False,
visibility = ["//visibility:public"],
)

js_binary(
name = "vega_bin",
data = [":node_modules"],
entry_point = "index.mjs",
)

js_run_binary(
name = "run_vega_bin",
stdout = "run_vega_bin_stdout",
tool = ":vega_bin",
)

assert_contains(
name = "contains_vega",
actual = ":run_vega_bin_stdout",
expected = "vega",
)

assert_contains(
name = "contains_rigel",
actual = ":run_vega_bin_stdout",
expected = "rigel",
)

assert_contains(
name = "contains_upper_rigel",
actual = ":run_vega_bin_stdout",
expected = "RIGEL",
)
9 changes: 9 additions & 0 deletions rules_js_external_packages/main/packages/vega/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import chalk from 'chalk';
import { whoami as whoisrigel } from 'rigel';

export function whoami() {
return chalk.green('vega');
}

console.log(whoami());
console.log(whoisrigel().toUpperCase());
7 changes: 7 additions & 0 deletions rules_js_external_packages/main/packages/vega/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vega",
"dependencies": {
"chalk": "5.3.0",
"rigel": "workspace:*"
}
}
40 changes: 40 additions & 0 deletions rules_js_external_packages/main/pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions rules_js_external_packages/main/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
2 changes: 2 additions & 0 deletions rules_js_external_packages/other/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
packages/rigel/node_modules
1 change: 1 addition & 0 deletions rules_js_external_packages/other/.bazeliskrc
5 changes: 5 additions & 0 deletions rules_js_external_packages/other/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 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 rules_js_external_packages/other/.bazelversion
2 changes: 2 additions & 0 deletions rules_js_external_packages/other/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/bazel-*
6 changes: 6 additions & 0 deletions rules_js_external_packages/other/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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). See
# https://github.com/aspect-build/rules_js/blob/7377f2d0387cc2a9251137929b1c53ccdb3fbcf0/docs/npm_import.md#npm_translate_lock
# documentation for more information.
hoist=false
3 changes: 3 additions & 0 deletions rules_js_external_packages/other/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")
32 changes: 32 additions & 0 deletions rules_js_external_packages/other/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "aspect_rules_js",
sha256 = "7ab9776bcca823af361577a1a2ebb9a30d2eb5b94ecc964b8be360f443f714b2",
strip_prefix = "rules_js-1.32.6",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.32.6/rules_js-v1.32.6.tar.gz",
)

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

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

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

npm_translate_lock(
name = "npm",
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

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

npm_repositories()
6 changes: 6 additions & 0 deletions rules_js_external_packages/other/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "rules_js_external_packages",
"dependencies": {
"rigel": "workspace:*"
}
}
Empty file.
1 change: 1 addition & 0 deletions rules_js_external_packages/other/packages/packages.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PACKAGES = ["rigel"]
41 changes: 41 additions & 0 deletions rules_js_external_packages/other/packages/rigel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

npm_package(
name = "rigel",
srcs = [
"index.mjs",
"package.json",
],
# This is a perf improvement; the default will be flipped to False in rules_js 2.0
include_runfiles = False,
visibility = ["//visibility:public"],
)

js_binary(
name = "rigel_bin",
data = [":node_modules"],
entry_point = "index.mjs",
)

js_run_binary(
name = "run_rigel_bin",
stdout = "run_rigel_bin_stdout",
tool = ":rigel_bin",
)

assert_contains(
name = "contains_rigel",
actual = ":run_rigel_bin_stdout",
expected = "rigel",
)

filegroup(
name = "srcs",
srcs = glob(["**/*"]),
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 2eeffc2

Please sign in to comment.