Skip to content

Instantly share code, notes, and snippets.

@lewish
Created February 15, 2023 17:23
Show Gist options
  • Save lewish/97b8f09cfd35b370588d806238bed6ad to your computer and use it in GitHub Desktop.
Save lewish/97b8f09cfd35b370588d806238bed6ad to your computer and use it in GitHub Desktop.
protobuf-ts bazel rules
load("@rules_proto_grpc//:defs.bzl", "proto_plugin")
load("@npm//:@protobuf-ts/plugin/package_json.bzl", protobuf_ts_plugin = "bin")
package(default_visibility = ["//visibility:public"])
protobuf_ts_plugin.protoc_gen_ts_binary(
name = "protoc_gen_ts",
)
proto_plugin(
name = "protobuf-ts-plugin",
outputs = [
"{protopath}.ts",
],
tool = ":protoc_gen_ts",
use_built_in_shell_environment = False,
)
proto_plugin(
name = "protobuf-ts-plugin-grpc",
outputs = [
"{protopath}.client.ts",
],
tool = ":protoc_gen_ts",
use_built_in_shell_environment = False,
)
typescript_proto_library(
name = "protos_typescript_protos_ts",
output_mode = "NO_PREFIX_FLAT",
protos = [
":protos_proto",
],
)
ts_library(
name = "protos_typescript_protos",
srcs = [
":protos_typescript_protos_ts",
],
deps = [
"//:node_modules/@protobuf-ts/runtime",
"//:node_modules/@protobuf-ts/runtime-rpc",
"//google/protobuf:protobuf_typescript_protos",
],
)
load(
"@rules_proto_grpc//:defs.bzl",
"ProtoPluginInfo",
"proto_compile_attrs",
"proto_compile_impl",
)
def _ts_proto_compile_impl(ctx):
base_env = {
# Make up for https://github.com/bazelbuild/bazel/issues/15470.
"BAZEL_BINDIR": ctx.bin_dir.path,
}
return proto_compile_impl(ctx, base_env = base_env)
# Create compile rule
typescript_proto_library = rule(
implementation = _ts_proto_compile_impl,
attrs = dict(
proto_compile_attrs,
_plugins = attr.label_list(
providers = [ProtoPluginInfo],
default = [
Label("//tools/protobuf-ts:protobuf-ts-plugin"),
],
doc = "List of protoc plugins to apply",
),
),
toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))],
)
typescript_grpc_library = rule(
implementation = _ts_proto_compile_impl,
attrs = dict(
proto_compile_attrs,
_plugins = attr.label_list(
providers = [ProtoPluginInfo],
default = [
Label("//tools/protobuf-ts:protobuf-ts-plugin-grpc"),
],
doc = "List of protoc plugins to apply",
),
),
toolchains = [str(Label("@rules_proto_grpc//protobuf:toolchain_type"))],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment