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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
/example/out
.DS_Store
*.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example/
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ npm install proto-loader
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

``` javascript
/*
protobufjs has a light build that does not include code for parsing .proto
files. The extra code is not typically necessary if you're using this loader,
but if you still need it, you can change the below line to:
var ProtoBuf = require('protobufjs');
*/
var ProtoBuf = require('protobufjs/dist/protobuf-light');

var protoDefinition = require('proto!./message.proto');
// => returns object converted from message.proto, resolves imports

var builder = ProtoBuf.loadJson(protoDefinition);
var protobuf = require('protobufjs');

var protoDefinition = require('json!proto-loader6!./message.proto');
// => returns json object converted from message.proto, resolves imports

var root = protobuf.Root.fromJSON(protoDefinition);
//...
```

Expand All @@ -33,7 +27,7 @@ module.exports = {
loaders: [
{
test: /\.proto$/,
loader: "proto-loader"
loaders: ['json-loader', "proto-loader6"]
}
]
}
Expand All @@ -42,6 +36,13 @@ module.exports = {

Then you only need to write: `require("./message.proto")`

## Run example with

```
webpack
node ./out/bundle.js
```


## License
MIT (http://www.opensource.org/licenses/mit-license.php)
54 changes: 49 additions & 5 deletions example/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
var ProtoBuf = require('protobufjs/dist/protobuf-light');
var protobuf = require('protobufjs')

var proto = require('./test.proto');
var protoDefinition = require('./proto/test.proto') //require .proto entry point

var pbuf = ProtoBuf.loadJson(proto).build();
console.log(Object.keys(pbuf));
console.log(Object.keys(pbuf.js));
console.log('ProtoDefinition\n')
console.log(protoDefinition)
console.log('\n\nProtoDefinition for Person\n')
console.log(protoDefinition.nested.Person);
console.log('\n\nProtoDefinition for js package\n')
console.log(protoDefinition.nested.js.nested);

var root = protobuf.Root.fromJSON(protoDefinition);

var protoValue = root.lookup("js.Value");
var protoPerson = root.lookup("Person");

var person = protoPerson.create({lastName: "V2", firstName: "C2" });

console.log('\n\Constructed Person\n')

console.log(person)

var buffer = protoPerson.encode(person).finish();

console.log('\n\Encoded Person\n')
console.log(buffer)

console.log('\n\Decoded Person\n')
var decodedMess = protoPerson.decode(buffer);
console.log(decodedMess)


// Create a new message
var message = protoValue.create(person);

//alternatively use:
//var valueAsBuffer = protoValue.encode({person: {lastName: "V2", firstName: "C2" }}).finish();
var valueAsBuffer = protoValue.encode({person: person}).finish();

console.log('\n\Encoded Value\n')

console.log(valueAsBuffer)

var value = protoValue.decode(valueAsBuffer);

console.log('\n\Decoded Value\n')

console.log(value)

console.log('\n\Print First Name from decoded Value\n')
console.log(value.person.firstName)
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
loaders: [
{
test: /\.proto$/,
loader: path.join(__dirname, "..")
loaders: ['json-loader' , path.join(__dirname, "..")]
}
]
}
Expand Down
65 changes: 18 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,29 @@
'use strict';

var fs = require('fs');
var ProtoBuf = require('protobufjs');
var protoTarget = require('protobufjs/cli/pbjs/targets/json');
var protoUtil = require('protobufjs/cli/pbjs/util');
var path = require("path");
var loaderUtils = require('loader-utils');
//let fs = require('fs');
let protobuf = require('protobufjs');
let protoTarget = require('protobufjs/cli/targets/json');
//let protoUtil = require('protobufjs/cli/util');
//let path = require("path");
let loaderUtils = require('loader-utils');

module.exports = function (source) {
var options = loaderUtils.parseQuery(this.query) || {};
var self = this;
let options = loaderUtils.parseQuery(this.query) || {};
//let self = this;

function readProto(src, options, loaded) {
var parser = new ProtoBuf.DotProto.Parser(src),
data = parser.parse();

if (!loaded) {
loaded = {};
}

if (Array.isArray(data.imports)) {
data.imports.forEach(function (imp, index) {
var i,
fileName;

// Skip pulled imports and legacy descriptors
if (typeof imp !== 'string' || (protoUtil.isDescriptor(imp) && !options.legacy)) {
return;
}

fileName = path.resolve(self.context + '/' + imp);
if (fs.existsSync(fileName)) {
if (loaded[fileName]) {
data.imports[index] = {};
return;
}
loaded[fileName] = true;
data.imports[index] = readProto(fs.readFileSync(fileName).toString('utf8'));
return;
}
throw Error('File not found: ' + imp);
});
}

return data;
}
let callback = this.async();

if (!callback) {
throw 'proto-loader currently only supports async mode.';
}

if (this.cacheable()) {
this.cacheable();
}

var data = readProto(source, options);
var builder = ProtoBuf.newBuilder(protoUtil.getBuilderOptions(options, 'using'));
builder['import'](data);

return 'module.exports = ' + protoTarget(builder, options) + ';';
new protobuf.Root().load(this.resourcePath,function(err, root) {
if(err) return callback(err);
protoTarget(root,options,callback);
});
};
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "proto-loader",
"version": "0.2.0",
"description": "Protocol Buffer loader module for webpack",
"name": "proto-loader6",
"version": "0.4.0",
"description": "Protocol Buffer loader module for webpack. Supports protobufjs v6+",
"main": "index.js",
"keywords": [
"webpack",
Expand All @@ -10,7 +10,7 @@
"protocol",
"buffer"
],
"author": "Brian Chirls @bchirls",
"author": "Chris Van Vranken @cessationoftime",
"license": "MIT",
"licenses": [
{
Expand All @@ -20,13 +20,14 @@
],
"repository": {
"type": "git",
"url": "https://github.com/brianchirls/proto-loader.git"
"url": "https://github.com/cessationoftime/proto-loader6.git"
},
"peerDependencies": {
"webpack": "^1.12.9",
"protobufjs": "^5.0.0"
"devDependencies": {
"json-loader": "^0.5.4",
"webpack": "^1.13.3"
},
"dependencies": {
"loader-utils": "^0.2.11"
"loader-utils": "^0.2.16",
"protobufjs": "^6.3.1"
}
}
Loading