Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,22 @@ Misc methods:
For an example of a real-world usage of the library, see [vector-tile-js](https://github.com/mapbox/vector-tile-js).


## Proto Schema to JavaScript
## Proto Schema to JavaScript / TypeScript

If installed globally, `pbf` provides a binary that compiles `proto` files into JavaScript modules. Usage:

```bash
$ pbf <proto_path> [--no-write] [--no-read] [--browser]
$ pbf <proto_path> [--no-write] [--no-read] [--browser|--es6|--typescript]
```

The `--no-write` and `--no-read` switches remove corresponding code in the output.
The `--browser` switch makes the module work in browsers instead of Node.

You can select the JavaScript module type to generate:

* By default a CommonJS module is generated which works in Node.
* The `--browser` switch generates JavaScript which adds everything to the global namespace which works in a browser.
* The `--es6` switch generates a ES6 module.
* The `--typescript` switch generates a TypeScript module.

The resulting module exports each message by name with the following methods:

Expand Down
13 changes: 11 additions & 2 deletions bin/pbf
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ var resolve = require('resolve-protobuf-schema');
var compile = require('../compile');

if (process.argv.length < 3) {
console.error('Usage: pbf [file.proto] [--browser] [--no-read] [--no-write]');
console.error('Usage: pbf [file.proto] [--browser|--es6|--typescript] [--no-read] [--no-write]');
return;
}

var moduleType = 'common-js';
if (process.argv.indexOf('--browser') >= 0) {
moduleType = 'global';
} else if (process.argv.indexOf('--es6') >= 0) {
moduleType = 'es6';
} else if (process.argv.indexOf('--typescript') >= 0) {
moduleType = 'typescript';
}

var code = compile.raw(resolve.sync(process.argv[2]), {
exports: process.argv.indexOf('--browser') >= 0 ? 'self' : 'exports',
moduleType: moduleType,
noRead: process.argv.indexOf('--no-read') >= 0,
noWrite: process.argv.indexOf('--no-write') >= 0
});
Expand Down
Loading