You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The library utilizes a JSON format that is equivalent to a .proto definition (see also: [Command line usage](#command-line)).
256
-
257
-
The following is identical to the .proto definition seen above, but it can also be used with just the light library because it doesn't require the parser:
255
+
The library utilizes a JSON format that is equivalent to a .proto definition. For example, the following is identical to the .proto definition seen above:
258
256
259
257
```json
260
258
// awesome.json
@@ -272,6 +270,25 @@ The following is identical to the .proto definition seen above, but it can also
272
270
}
273
271
```
274
272
273
+
The JSON format closely resembles the internal reflection structure:
To achieve the same with static code generated by [pbjs](#command-line), there is the [pbts](#generating-typescript-definitions-from-static-modules) command line utility to generate type definitions from static code as well.
442
-
443
-
Let's say you generated your static code to `bundle.js` and its type definitions to `bundle.d.ts`, then you can do:
458
+
If you generated static code using the CLI to `bundle.js` and its type definitions to `bundle.d.ts`, then you can do:
For production environments it is recommended to bundle all your .proto files to a single .json file, which minimizes the number of network requests and avoids any parser overhead (hint: works with just the [light library](#distributions)):
Picking up on the example above, the following not just generates static code to a CommonJS module `compiled.js` but also its respective TypeScript definitions to `compiled.d.ts`:
Additionally, TypeScript definitions of static modules are compatible with their reflection-based counterparts (i.e. as exported by JSON modules), as long as the following conditions are met:
595
-
596
-
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
597
-
2. Types, services and enums must start with an uppercase letter to become available as properties of the reflected types as well (i.e. to be able to use `MyMessage.MyEnum` instead of `root.lookup("MyMessage.MyEnum")`).
598
-
599
-
For example, the following generates a JSON module `bundle.js` and a `bundle.d.ts`, but no static code:
While using .proto files directly requires the full library respectively pure reflection/JSON the light library, pretty much all code but the relatively short descriptors is shared.
609
-
610
-
Static code, on the other hand, requires just the minimal library, but generates additional, albeit editable, source code without any reflection features.
611
-
612
-
There is no significant difference performance-wise as the code generated statically is pretty much the same as generated at runtime and both are largely interchangeable as seen in the previous section.
613
-
614
-
### Using pbjs and pbts programmatically
615
-
616
-
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
Command line interface (CLI) for [protobuf.js](https://github.com/dcodeIO/protobuf.js). Translates between file formats and generates static code as well as TypeScript definitions.
For production environments it is recommended to bundle all your .proto files to a single .json file, which minimizes the number of network requests and avoids any parser overhead (hint: works with just the **light** library):
Picking up on the example above, the following not just generates static code to a CommonJS module `compiled.js` but also its respective TypeScript definitions to `compiled.d.ts`:
Additionally, TypeScript definitions of static modules are compatible with their reflection-based counterparts (i.e. as exported by JSON modules), as long as the following conditions are met:
151
+
152
+
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
153
+
2. Types, services and enums must start with an uppercase letter to become available as properties of the reflected types as well (i.e. to be able to use `MyMessage.MyEnum` instead of `root.lookup("MyMessage.MyEnum")`).
154
+
155
+
For example, the following generates a JSON module `bundle.js` and a `bundle.d.ts`, but no static code:
While using .proto files directly requires the full library respectively pure reflection/JSON the light library, pretty much all code but the relatively short descriptors is shared.
165
+
166
+
Static code, on the other hand, requires just the minimal library, but generates additional, albeit editable, source code without any reflection features.
167
+
168
+
There is no significant difference performance-wise as the code generated statically is pretty much the same as generated at runtime and both are largely interchangeable as seen in the previous section.
169
+
170
+
API
171
+
---
172
+
173
+
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
174
+
175
+
```js
176
+
var pbjs =require("protobufjs-cli/pbjs"); // or require("protobufjs-cli").pbjs / .pbts
0 commit comments