Skip to content

Webpack loader to translate .proto definitions to ProtoBuf.js modules

Notifications You must be signed in to change notification settings

yellowbeee/protobufjs2-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protobufjs2-loader

Webpack loader to translate .proto definitions to ProtoBuf.js modules. Equivalent to running your definitions through the pbjs CLI.

Install

npm install -D protobufjs2-loader

Usage

// webpack.config.js

module.exports = {
    ...
    module: {
        rules: [{
            test: /\.proto$/,
            use: {
              loader: 'protobufjs2-loader',
              options: {
                target: 'json-module'
                /* controls the "target" flag to pbjs - true for
                 * json-module, false for static-module.
                 */
              }
            }
        }]
    }
}

// webpack-chain

config.module
    .rule('proto')
    .test(/\.proto$/)
    .use('protobufjs2-loader')
    .loader('protobufjs2-loader')
    .options({
      target: 'json-module',
    })
    .end()

Examples

Using .proto files

// src/proto/awesome.proto
package awesomepackage;
syntax = "proto3";

message AwesomeMessage {
    string awesome_field = 1; // becomes awesomeField
}
// index.js

import PB from 'src/proto/awesome.proto'

const AwesomeMessage = PB.lookup('awesomepackage.AwesomeMessage')

// Exemplary payload
var payload = { awesomeField: "AwesomeString" };

// Create a new message
var message = AwesomeMessage.create(payload);

// Encode a message to an Uint8Array (browser) or Buffer (node)
var buffer = AwesomeMessage.encode(message).finish();

// Decode an Uint8Array (browser) or Buffer (node) to a message
var message = AwesomeMessage.decode(buffer);

About

Webpack loader to translate .proto definitions to ProtoBuf.js modules

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published