Protocol Buffers and gRPC (UNSTABLE)
UNSTABLE API: contents of this page are not subject to our usual semver guarantees. We may make breaking changes in any release. Please try this API and provide feedback. We intend to promote it to a stable API in a minor release, possibly as soon as v2.1.0.
ts_proto_library
uses the Connect library from bufbuild, and supports both Web and Node.js:
This Bazel integration follows the "Local Generation" mechanism described at
https://connectrpc.com/docs/web/generating-code#local-generation,
using the @bufbuild/protoc-gen-connect-es
and @bufbuild/protoc-gen-es
packages as plugins to protoc.
Note: this API surface is not included in defs.bzl
to avoid eager loads of rules_proto for all rules_ts users.
Installation
If you install rules_ts in WORKSPACE
, you'll need to install the deps of rules_proto, like this:
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")
rules_proto_dependencies()
If you use bzlmod/MODULE.bazel
then no extra install is required.
Future work
- Add support for generating the
ts_proto_library
targets in aspect configure - Allow users to choose other plugins. We intend to wait until http://github.com/bazelbuild/rules_proto supports protoc plugins.
- Allow users to control the output format. Currently it is hard-coded to
js+dts
, and the JS output uses ES Modules.
Macros and Functions
ts_proto_library
A macro to generate JavaScript code and TypeScript typings from .proto files.
Example usage (generated):
load("@aspect_rules_ts//ts:proto.bzl", "ts_proto_library")
ts_proto_library(
# name of resulting ts_proto_library target
name = "",
# Label pointing to the linked node_modules target where @bufbuild/protoc-gen-es is linked, e.g
node_modules = None,
)
name
Required.
name of resulting ts_proto_library target
node_modules
Required.
Label pointing to the linked node_modules target where @bufbuild/protoc-gen-es is linked, e.g. //:node_modules.
Since the generated code depends on @bufbuild/protobuf, this package must also be linked.
If has_services = True
then @bufbuild/proto-gen-connect-es should be linked as well.
has_services
Optional. Default: True
whether the proto file contains a service, and therefore *_connect.{js,d.ts} should be written.
copy_files
Optional. Default: True
whether to copy the resulting .d.ts files back to the source tree, for the editor to locate them.
files_to_copy
Optional. Default: None
which files from the protoc output to copy. By default, scans for *.proto in the current package and replaces with the typical output filenames.
kwargs
Optional.
additional named arguments to the ts_proto_library rule