Skip to content

Commit 56f0338

Browse files
author
Matt Mackay
authored
feat(ts_proto_library): expose protoc_gen_options on ts_proto_library (#650)
1 parent 1e74685 commit 56f0338

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

docs/proto.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Future work
4747
## ts_proto_library
4848

4949
<pre>
50-
ts_proto_library(<a href="#ts_proto_library-name">name</a>, <a href="#ts_proto_library-node_modules">node_modules</a>, <a href="#ts_proto_library-proto">proto</a>, <a href="#ts_proto_library-gen_connect_es">gen_connect_es</a>, <a href="#ts_proto_library-gen_connect_query">gen_connect_query</a>,
50+
ts_proto_library(<a href="#ts_proto_library-name">name</a>, <a href="#ts_proto_library-node_modules">node_modules</a>, <a href="#ts_proto_library-proto">proto</a>, <a href="#ts_proto_library-protoc_gen_options">protoc_gen_options</a>, <a href="#ts_proto_library-gen_connect_es">gen_connect_es</a>, <a href="#ts_proto_library-gen_connect_query">gen_connect_query</a>,
5151
<a href="#ts_proto_library-gen_connect_query_service_mapping">gen_connect_query_service_mapping</a>, <a href="#ts_proto_library-copy_files">copy_files</a>, <a href="#ts_proto_library-proto_srcs">proto_srcs</a>, <a href="#ts_proto_library-files_to_copy">files_to_copy</a>, <a href="#ts_proto_library-kwargs">kwargs</a>)
5252
</pre>
5353

@@ -61,6 +61,7 @@ A macro to generate JavaScript code and TypeScript typings from .proto files.
6161
| <a id="ts_proto_library-name"></a>name | name of resulting ts_proto_library target | none |
6262
| <a id="ts_proto_library-node_modules"></a>node_modules | 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 `gen_connect_es = True` then @bufbuild/proto-gen-connect-es should be linked as well. If `gen_connect_query = True` then @bufbuild/proto-gen-connect-query should be linked as well. | none |
6363
| <a id="ts_proto_library-proto"></a>proto | the `proto_library` target that contains the .proto files to generate code for. | none |
64+
| <a id="ts_proto_library-protoc_gen_options"></a>protoc_gen_options | dict of protoc_gen_es options. See https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#plugin-options | `{}` |
6465
| <a id="ts_proto_library-gen_connect_es"></a>gen_connect_es | whether protoc_gen_connect_es should generate grpc services, and therefore `*_connect.{js,d.ts}` should be written. | `True` |
6566
| <a id="ts_proto_library-gen_connect_query"></a>gen_connect_query | whether protoc_gen_connect_query should generate [TanStack Query](https://tanstack.com/query) clients, and therefore `*_connectquery.{js,d.ts}` should be written. | `False` |
6667
| <a id="ts_proto_library-gen_connect_query_service_mapping"></a>gen_connect_query_service_mapping | mapping from source proto file to the named RPC services that file contains. Needed to predict which files will be generated by gen_connect_query. See https://github.com/connectrpc/connect-query-es/tree/main/examples/react/basic/src/gen<br><br>For example, given `a.proto` which contains a service `Foo` and `b.proto` that contains a service `Bar`, the mapping would be `{"a.proto": ["Foo"], "b.proto": ["Bar"]}` | `{}` |

ts/private/ts_proto_library.bzl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
77
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
88

99
# buildifier: disable=function-docstring-header
10-
def _protoc_action(ctx, proto_info, outputs, options = {
11-
"keep_empty_files": True,
12-
"target": "js+dts",
13-
}):
10+
def _protoc_action(ctx, proto_info, outputs):
1411
"""Create an action like
1512
bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/protoc $@' '' \
1613
'--plugin=protoc-gen-es=bazel-out/k8-opt-exec-2B5CBBC6/bin/plugin/bufbuild/protoc-gen-es.sh' \
@@ -21,6 +18,16 @@ def _protoc_action(ctx, proto_info, outputs, options = {
2118
"""
2219
inputs = depset(proto_info.direct_sources, transitive = [proto_info.transitive_descriptor_sets])
2320

21+
options = dict({
22+
"keep_empty_files": True,
23+
"target": "js+dts",
24+
}, **ctx.attr.protoc_gen_options)
25+
26+
if not options["keep_empty_files"]:
27+
fail("protoc_gen_options.keep_empty_files must be True")
28+
if options["target"] != "js+dts":
29+
fail("protoc_gen_options.target must be 'js+dts'")
30+
2431
args = ctx.actions.args()
2532
args.add_joined(["--plugin", "protoc-gen-es", ctx.executable.protoc_gen_es.path], join_with = "=")
2633
for (key, value) in options.items():
@@ -105,6 +112,10 @@ ts_proto_library = rule(
105112
providers = [ProtoInfo],
106113
mandatory = True,
107114
),
115+
"protoc_gen_options": attr.string_dict(
116+
doc = "dict of protoc_gen_es options",
117+
default = {},
118+
),
108119
"protoc_gen_es": attr.label(
109120
doc = "protoc plugin to generate messages",
110121
mandatory = True,

ts/proto.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
4747
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library")
4848
load("//ts/private:ts_proto_library.bzl", ts_proto_library_rule = "ts_proto_library")
4949

50-
def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_connect_query = False, gen_connect_query_service_mapping = {}, copy_files = True, proto_srcs = None, files_to_copy = None, **kwargs):
50+
def ts_proto_library(name, node_modules, proto, protoc_gen_options = {}, gen_connect_es = True, gen_connect_query = False, gen_connect_query_service_mapping = {}, copy_files = True, proto_srcs = None, files_to_copy = None, **kwargs):
5151
"""
5252
A macro to generate JavaScript code and TypeScript typings from .proto files.
5353
@@ -58,6 +58,8 @@ def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_conne
5858
If `gen_connect_es = True` then @bufbuild/proto-gen-connect-es should be linked as well.
5959
If `gen_connect_query = True` then @bufbuild/proto-gen-connect-query should be linked as well.
6060
proto: the `proto_library` target that contains the .proto files to generate code for.
61+
protoc_gen_options: dict of protoc_gen_es options.
62+
See https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#plugin-options
6163
gen_connect_es: whether protoc_gen_connect_es should generate grpc services, and therefore `*_connect.{js,d.ts}` should be written.
6264
gen_connect_query: whether protoc_gen_connect_query should generate [TanStack Query](https://tanstack.com/query) clients, and therefore `*_connectquery.{js,d.ts}` should be written.
6365
gen_connect_query_service_mapping: mapping from source proto file to the named RPC services that file contains.
@@ -132,6 +134,7 @@ def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_conne
132134
gen_connect_query = gen_connect_query,
133135
gen_connect_query_service_mapping = gen_connect_query_service_mapping,
134136
proto = proto,
137+
protoc_gen_options = protoc_gen_options,
135138
)
136139

137140
js_library(

0 commit comments

Comments
 (0)