This library provides rules_js-compatible Bazel rules for generating protocol buffers. The code is experimental and the API has not yet stabilized.
From the release you wish to use:
https://github.com/gonzojive/rules_ts_proto/releases
copy the WORKSPACE snippet into your WORKSPACE file.
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@com_github_gonzojive_rules_ts_proto//ts_proto:defs.bzl", "ts_proto_library")
proto_library(
name = "greeting_proto",
srcs = ["greeting.proto"],
import_prefix = "github.com/gonzojive/rules_ts_proto/example/prefix",
visibility = ["//visibility:public"],
deps = [
"//location:location_proto",
],
)
ts_proto_library(
name = "greeting_ts_proto",
proto = ":greeting_proto",
visibility = ["//visibility:public"],
deps = [
"//location:location_ts_proto",
],
)The //:greeting_ts_proto target is effectively a js_library target that can
be used as a dependency. For example:
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
esbuild(
name = "program",
entry_point = "program.mjs",
deps = [
#":greeting_ts_proto",
"//location:location_ts_proto",
"//:node_modules/google-protobuf",
],
)// file: program.mjs
import { Position } from "./location/location_pb.mjs";
const pos = new Position();
pos.setLatitude(42.42);
console.log("request.latitude = %s", pos.getLatitude());For more examples that can be copy pasted into your project, look at examples
workspaces in the e2e folder.
Some commentary on how rules_ts_proto works:
- The basic code generators and runtime libraries used by
rules_ts_protocome from a fork of Google's protobuf-javascript and grpc-web. - The files produced by
rules_ts_protoare ES6 modules, not CommonJS modules. rules_ts_protoproduces output files within the bazel package where the target appears (not child directories).- A
ts_proto_libraryhas a single correspondingproto_library. Thisproto_librarymust have a single.protofile in itssrcslist, a restriction that should be removed at some point. - For each
proto_librarydep, there should be a correspondingts_proto_librarydep in thets_proto_librarytarget. There are no restructions on where thets_proto_librarydeps are within the workspace.ts_rules_protowill ensure the generated code's imports are correct. - For code generated for a given
.protofile,rules_ts_protouses relative imports to import proto dependencies. TheTsProtoInfoprovider attached to eachts_proto_librarykeeps track of the association between an input.protofile and the generated JavaScript and TypeScript files. The implementation of thets_proto_libraryrule combines theTsProtoInfovalues of all dependencies and feeds this info into the custom protoc plugin as a JSON "configuration." The custom protoc plugin in turn calls theprotobuf-javascriptandgrpc-webprotoc plugins. The imports generated by these subordinate plugins must be amended using the configuration.
The majority of the complexity in this library comes from how imports work in the world of TypeScript and JavaScript.
NodeJS documentation of ECMASCript modules has an import specifiers section. It defines three types of import specifiers. One of them is
Relative specifiers like
'./startup.js'or'../config.mjs'. They refer to a path relative to the location of the importing file. The file extension is always necessary for these.
rules_ts_proto generates relative import statements that use absolute
extensions for this reason.