Protocol Buffers
Bazel

Protocol Buffers


ts_proto_library

ts_proto_library(name, deps, output_name)

Wraps https://github.com/dcodeIO/protobuf.js for use in Bazel.

ts_proto_library has identical outputs to ts_library, so it can be used anywhere a ts_library can appear, such as in the deps[] of another ts_library.

load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_proto_library")

proto_library(
    name = "car_proto",
    srcs = ["car.proto"],
)

ts_proto_library(
    name = "car",
    deps = [":car_proto"],
)

ts_library(
    name = "test_lib",
    testonly = True,
    srcs = ["car.spec.ts"],
    deps = [":car"],
)

Note in this example we named the ts_proto_library rule car so that the result will be car.d.ts. This means our TypeScript code can just import {symbols} from './car'. Use the output_name attribute if you want to name the rule differently from the output file.

The JavaScript produced by protobuf.js has a runtime dependency on a support library. Under devmode (e.g. ts_devserver, ts_web_test_suite) you'll need to include these scripts in the bootstrap phase (before Require.js loads). You can use the label @build_bazel_rules_typescript//:protobufjs_bootstrap_scripts to reference these scripts in the bootstrap attribute of ts_web_test_suite or ts_devserver.

To complete the example above, you could write a ts_web_test_suite:

load("@build_bazel_rules_typescript//:defs.bzl", "ts_web_test_suite")

ts_web_test_suite(
    name = "test",
    deps = ["test_lib"],
    bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"],
    browsers = [
      "@io_bazel_rules_webtesting//browsers:chromium-local",
      "@io_bazel_rules_webtesting//browsers:firefox-local",
    ],
)

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

proto_library targets

output_name

String; Optional; Default is ''

Name of the resulting module, which you will import from. If not specified, the name will match the target's name.