Skip to content

Commit

Permalink
Add grpc-node support (#134)
Browse files Browse the repository at this point in the history
* Add `grpc-node` support

- Refactor `typescript_proto_library` to generate only messages
- Add `typescript_grpc_web_library` to provide previous functionality
- Add `typescript_grpc_node_library` to add grpc-node support
- Improve documentation
- Add migration guide when coming from previous versions
- Update `ts-protoc-gen`

Co-authored-by: Jens Ulrich Hjuler Fosgerau <[email protected]>
  • Loading branch information
mc0 and Multiply authored Dec 24, 2020
1 parent aed1c82 commit c0b4f4f
Show file tree
Hide file tree
Showing 14 changed files with 987 additions and 2,902 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

Bazel rules for generating TypeScript declarations for JavaScript protocol buffers using the
[ts-protoc-gen](https://github.com/improbable-eng/ts-protoc-gen) protoc plugin. These rules can also
generate service definitions for use by [grpc-web](https://github.com/improbable-eng/grpc-web).
generate service definitions for use by [grpc-web](https://github.com/improbable-eng/grpc-web) or
[grpc-node](https://github.com/grpc/grpc-node).

## Getting Started

> If you're migrating from the ts-protoc-gen rules, see [here](docs/migrating_from_ts_protoc_gen.md) for a migration guide
> If you're upgrading from a previous version and experiencing issues with missing `_pb_service.d.ts` files, see
> [here](docs/migrating_to_multi_rules.md) for a migration guide.
Before you can use `rules_typescript_proto`, you must first setup:

Expand Down Expand Up @@ -61,7 +63,29 @@ will need to include the following dependencies at runtime yourself:
- `@improbable-eng/grpc-web`
- `browser-headers`

See `//test:pizza_service_proto_test_suite` for an example.
For generating grpc output files, you'll also need the following in your `BUILD` file:
```python
# For grpc-web support use:
typescript_grpc_web_library(
name = "test_ts_grpc_web",
proto = ":test_proto",
)

# For grpc-node support use:
typescript_grpc_node_library(
name = "test_ts_grpc_node",
proto = ":test_proto",
)

# For grpc-node support with grpc-js use:
typescript_grpc_node_library(
name = "test_ts_grpc_node_grpc_js",
proto = ":test_proto",
use_grpc_js = True,
)
```

See `//test:pizza_service_proto_test_suite` and `//test:grpc_node_test` for examples.

## IDE Code Completion

Expand All @@ -83,7 +107,8 @@ To get code completion working for the generated protos in your IDE, add the fol
}
```

> NOTE: This has only been tested in IntelliJ with the bazel plugin
> NOTE: This has only been tested in IntelliJ and VSCode with the bazel plugin
> NOTE: This assumes a default `--symlink_prefix` value.
## Contributing

Expand Down
27 changes: 27 additions & 0 deletions docs/migrating_to_multi_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Migrating to multi_rules

Previously, the `typescript_proto_library` rule would also build grpc-web outputs automatically.

This is now split into multiple rules to align with other languages:
- `typescript_proto_library` for outputting protobuf messages files (_pb.d.ts, _pb.js)
- `typescript_grpc_node_library` for outputting protobuf service and client files (_grpc_pb.d.ts, _grpc_pb.js); supports both grpc and grpc-js requires
- `typescript_grpc_web_library` for outputting protobuf service and client files (_pb_service.d.ts, _pb_service.js)

To keep the same behavior, add to your definition:
```python
# Before:
typescript_proto_library(
name = "test_ts_proto",
proto = ":test_proto",
)

# After:
typescript_proto_library(
name = "test_ts_proto",
proto = ":test_proto",
)
typescript_grpc_web_library(
name = "test_ts_grpc_web",
proto = ":test_proto",
)
```
4 changes: 4 additions & 0 deletions index.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
load("//src:typescript_proto_library.bzl", _typescript_proto_library = "typescript_proto_library")
load("//src:typescript_grpc_node_library.bzl", _typescript_grpc_node_library = "typescript_grpc_node_library")
load("//src:typescript_grpc_web_library.bzl", _typescript_grpc_web_library = "typescript_grpc_web_library")
load("//src:rules_typescript_proto_dependencies.bzl", _rules_typescript_proto_dependencies = "rules_typescript_proto_dependencies")

rules_typescript_proto_dependencies = _rules_typescript_proto_dependencies
typescript_proto_library = _typescript_proto_library
typescript_grpc_node_library = _typescript_grpc_node_library
typescript_grpc_web_library = _typescript_grpc_web_library
Loading

0 comments on commit c0b4f4f

Please sign in to comment.