Skip to content

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

License

Notifications You must be signed in to change notification settings

trivago/pbf-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published