Skip to content

Instantly share code, notes, and snippets.

@lewish
Created February 15, 2023 17:23

Revisions

  1. lewish created this gist Feb 15, 2023.
    26 changes: 26 additions & 0 deletions BUILD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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,
    )
    19 changes: 19 additions & 0 deletions BUILD.invocation
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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",
    ],
    )
    44 changes: 44 additions & 0 deletions defs.bzl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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"))],
    )