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:
*[Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
475
491
@@ -478,152 +494,7 @@ Documentation
478
494
479
495
Command line
480
496
------------
481
-
482
-
The `pbjs` command line utility can be used to bundle and translate between .proto and .json files. It also generates static code.
483
-
484
-
```
485
-
Consolidates imports and converts between file formats.
486
-
487
-
-t, --target Specifies the target format. Also accepts a path to require a custom target.
488
-
489
-
json JSON representation
490
-
json-module JSON representation as a module
491
-
proto2 Protocol Buffers, Version 2
492
-
proto3 Protocol Buffers, Version 3
493
-
static Static code without reflection
494
-
static-module Static code without reflection as a module
495
-
496
-
-p, --path Adds a directory to the include path.
497
-
498
-
-o, --out Saves to a file instead of writing to stdout.
499
-
500
-
Module targets only:
501
-
502
-
-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
503
-
504
-
default Default wrapper supporting both CommonJS and AMD
505
-
commonjs CommonJS wrapper
506
-
amd AMD wrapper
507
-
es6 ES6 wrapper (implies --es6)
508
-
509
-
-r, --root Specifies an alternative protobuf.roots name.
510
-
511
-
-l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:
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