Skip to content

Commit 8c7dfcd

Browse files
authored
feat: add example of using proto_library with Node.js (#2767)
Also fix a bug where `js_library` deps ran the proto aspect, but js_binary/js_test did not. /cc @acozzette
1 parent 9a76eb9 commit 8c7dfcd

18 files changed

Lines changed: 304 additions & 0 deletions

.aspect/workflows/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ workspaces:
147147
# e2e/update_pnpm_lock_with_import:
148148
# e2e/webpack_devserver:
149149
# e2e/webpack_devserver_esm:
150+
examples/proto:
151+
icon: protobuf
152+
tasks: *e2e_tasks
150153
bazel:
151154
flags:
152155
- --config=ci

.bazelignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Nested modules
22
e2e/
3+
examples/proto
34

45
# TODO(bazel 8): move to REPO.bazel
56
examples/js_binary/node_modules/
@@ -20,6 +21,8 @@ examples/npm_package/packages/pkg_d/node_modules/
2021
examples/npm_package/packages/pkg_e/node_modules/
2122
examples/runfiles/node_modules
2223
examples/stack_traces/node_modules
24+
examples/proto/node_modules
25+
examples/proto/tools/node_modules
2326
examples/vite3/node_modules
2427
examples/vite6/node_modules
2528
examples/webpack_cli/node_modules/

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ js/private/watch/aspect_watch_protocol.mjs linguist-generated=true
66
js/private/watch/aspect_watch_protocol.d.mts linguist-generated=true
77
js/private/node-patches/fs.cjs linguist-generated=true
88
js/private/js_image_layer.mjs linguist-generated=true
9+
**/*_pb.d.ts linguist-generated=true

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
**/pnpm-lock.yaml
22
e2e/**/*-docs.md
33
examples/**/*-docs.md
4+
examples/**/*_pb.d.ts
45
js/private/coverage/coverage.js
56
js/private/devserver/js_run_devserver.mjs
67
js/private/node-patches/fs.cjs

examples/proto/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build --incompatible_enable_proto_toolchain_resolution
2+
build --@protobuf//bazel/toolchains:prefer_prebuilt_protoc

examples/proto/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hoist=false

examples/proto/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
24.11.0

examples/proto/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_test")
2+
load("@npm//:defs.bzl", "npm_link_all_packages")
3+
load("@protobuf//bazel:proto_library.bzl", "proto_library")
4+
5+
npm_link_all_packages()
6+
7+
proto_library(
8+
name = "foo_proto",
9+
srcs = [
10+
"status.proto",
11+
"user.proto",
12+
],
13+
deps = ["@protobuf//:timestamp_proto"],
14+
)
15+
16+
js_test(
17+
name = "test",
18+
data = [":foo_proto"],
19+
entry_point = "test_proto.js",
20+
)

examples/proto/MODULE.bazel

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
bazel_dep(name = "aspect_rules_js")
2+
bazel_dep(name = "diff.bzl", version = "0.4.3")
3+
bazel_dep(name = "protobuf", version = "33.4") # Works with Bazel 7-9
4+
bazel_dep(name = "rules_nodejs", version = "6.7.3")
5+
6+
local_path_override(
7+
module_name = "aspect_rules_js",
8+
path = "../..",
9+
)
10+
11+
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
12+
node.toolchain(node_version_from_nvmrc = "//:.nvmrc")
13+
14+
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
15+
pnpm.pnpm(pnpm_version_from = "//:package.json")
16+
use_repo(pnpm, "pnpm")
17+
18+
npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm")
19+
npm.npm_translate_lock(
20+
name = "npm",
21+
npmrc = "//:.npmrc",
22+
pnpm_lock = "//:pnpm-lock.yaml",
23+
)
24+
use_repo(npm, "npm")
25+
26+
register_toolchains("//tools/toolchains:all")

examples/proto/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"private": true,
3+
"packageManager": "[email protected]",
4+
"dependencies": {
5+
"@bufbuild/protobuf": "catalog:"
6+
},
7+
"type": "module"
8+
}

0 commit comments

Comments
 (0)