- to be used along with mapbox/pbf
- uses protocol-buffers-schema as schema parser
- returns a compiled module ready to be used when you
require('./file.proto')
npm install pbf-loader
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.
Assuming you already did npm install
, you can:
npm test
This project is released under the terms of the Apache 2.0 license.