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
**Note** that this library's versioning scheme is not semver-compatible for historical reasons. For guaranteed backward compatibility, always depend on `~6.A.B` instead of `^6.A.B` (hence the `--save-prefix` above).
64
62
65
63
### Browsers
@@ -597,171 +595,6 @@ Other notes:
597
595
598
596
**ProTip!** Not as pretty, but you can [use decorators in plain JavaScript](https://github.com/dcodeIO/protobuf.js/blob/master/examples/js-decorators.js) as well.
599
597
600
-
Command line
601
-
------------
602
-
603
-
**Note** that moving the CLI to [its own package](./cli) is a work in progress. At the moment, it's still part of the main package.
604
-
605
-
The command line interface (CLI) can be used to translate between file formats and to generate static code as well as TypeScript definitions.
606
-
607
-
### pbjs for JavaScript
608
-
609
-
```
610
-
Translates between file formats and generates static code.
611
-
612
-
-t, --target Specifies the target format. Also accepts a path to require a custom target.
613
-
614
-
json JSON representation
615
-
json-module JSON representation as a module
616
-
proto2 Protocol Buffers, Version 2
617
-
proto3 Protocol Buffers, Version 3
618
-
static Static code without reflection (non-functional on its own)
619
-
static-module Static code without reflection as a module
620
-
621
-
-p, --path Adds a directory to the include path.
622
-
623
-
-o, --out Saves to a file instead of writing to stdout.
624
-
625
-
--sparse Exports only those types referenced from a main file (experimental).
626
-
627
-
Module targets only:
628
-
629
-
-w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.
630
-
631
-
default Default wrapper supporting both CommonJS and AMD
632
-
commonjs CommonJS wrapper
633
-
amd AMD wrapper
634
-
es6 ES6 wrapper (implies --es6)
635
-
closure A closure adding to protobuf.roots where protobuf is a global
636
-
637
-
-r, --root Specifies an alternative protobuf.roots name.
638
-
639
-
-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):
Picking up on the example above, the following not only 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:
726
-
727
-
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
728
-
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")`).
729
-
730
-
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.
740
-
741
-
Static code, on the other hand, requires just the minimal library, but generates additional source code without any reflection features. This also implies that there is a break-even point where statically generated code becomes larger than descriptor-based code once the amount of code generated exceeds the size of the full respectively light library.
742
-
743
-
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.
744
-
745
-
| Source | Library | Advantages | Tradeoffs
746
-
|--------|---------|------------|-----------
747
-
| .proto | full | Easily editable<br />Interoperability with other libraries<br />No compile step | Some parsing and possibly network overhead
| static | minimal | Works where `eval` access is restricted<br />Fully documented<br />Small footprint for small protos | Can be hard to edit<br />No reflection<br />Has a compile step
750
-
751
-
### Command line API
752
-
753
-
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
754
-
755
-
```js
756
-
var pbjs =require("protobufjs/cli/pbjs"); // or require("protobufjs/cli").pbjs / .pbts
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.
5
+
Command line interface (CLI) for [protobuf.js](https://github.com/dcodeIO/protobuf.js).
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 only 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:
133
+
134
+
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
135
+
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")`).
136
+
137
+
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.
147
+
148
+
Static code, on the other hand, requires just the minimal library, but generates additional source code without any reflection features. This also implies that there is a break-even point where statically generated code becomes larger than descriptor-based code once the amount of code generated exceeds the size of the full respectively light library.
149
+
150
+
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.
151
+
152
+
| Source | Library | Advantages | Tradeoffs
153
+
|--------|---------|------------|-----------
154
+
| .proto | full | Easily editable<br />Interoperability with other libraries<br />No compile step | Some parsing and possibly network overhead
| static | minimal | Works where `eval` access is restricted<br />Fully documented<br />Small footprint for small protos | Can be hard to edit<br />No reflection<br />Has a compile step
157
+
158
+
### Command line API
159
+
160
+
Both utilities can be used programmatically by providing command line arguments and a callback to their respective `main` functions:
161
+
162
+
```js
163
+
var pbjs =require("protobufjs-cli/pbjs"); // or require("protobufjs-cli").pbjs / .pbts
0 commit comments