All Projects → trivago → pbf-loader

trivago / pbf-loader

Licence: other
Webpack loader for .proto files to be used within mapbox/pbf

Programming Languages

javascript
184084 projects - #8 most used programming language

Webpack loader for .proto files

Installation

npm install pbf-loader

Usage

see example for sample implementation.

Given your webpack.config.js like this:

module.exports = {
    module: {
        loaders: [
            {
                test: /\.proto$/,
                loader: "pbf-loader"
            }
        ]
    }
}

Instead of:

const Pbf = require('pbf'); 
const compile = require('pbf/compile');
const fs = require('fs');
const schema = require('protocol-buffers-schema');

const data = 'somestring';
const proto = schema.parse(fs.readFileSync('./test.proto'));
const test = compile(proto).test; // assuming message definition: message test {}

using this webpack loader, simply require your .proto file like this:

const proto = require('./index.proto');
const Pbf = require('pbf');

const data = "somestring"; // data that you want to write;

const schema = proto.test; // accessing the message definition
const pbf = new Pbf();

// now you can write data to your pbf with pbf.writeMessage etc.
pbf.writeString(data);
const buffer = pbf.finish();
// now you can read back your message using schema.read(new Pbf(buffer))

You can refer to index.proto for how the .proto file looks like.

Test

Assuming you already did npm install, you can:

npm test

License

This project is released under the terms of the Apache 2.0 license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].