https://github.com/pengzhanbo/protobufjs-webpack-plugin
protobuf to js
https://github.com/pengzhanbo/protobufjs-webpack-plugin
protobuf protobufjs protobufjs-webpack-plugin webpack webpack-plugin
Last synced: 8 months ago
JSON representation
protobuf to js
- Host: GitHub
- URL: https://github.com/pengzhanbo/protobufjs-webpack-plugin
- Owner: pengzhanbo
- License: mit
- Archived: true
- Created: 2018-03-01T08:19:13.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-06-06T19:02:01.000Z (almost 4 years ago)
- Last Synced: 2025-01-06T03:32:05.709Z (about 1 year ago)
- Topics: protobuf, protobufjs, protobufjs-webpack-plugin, webpack, webpack-plugin
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome - pengzhanbo/protobufjs-webpack-plugin - protobuf to js (JavaScript)
README
# protobufjs-webpack-plugin
转译protobuf文件为js文件
### Install
``` bash
npm install --save-dev protobufjs-webpack-plugin
```
### Usage
Add it to your `webpack.config.js`
``` js
var ProtobufPlugin = require('protobufjs-webpack-plugin');
module.exports = {
plugins: [
new ProtobufPlugin({
input: path.join('proto', '*.proto'),
output: path.join('dist', 'proto')
})
]
};
```
### Config
``` js
{
input: '', // 输入文件匹配
output: '', // 输出路径, 如果是文件夹,单文件输出则以 basic.proto.js命名,多文件以文件名拼接
outputType: 1, // 输入方式,默认多文件形式, 如果设置为 0,则所有合并为单文件
target: 'static-module', // 类型 json|json-module|static\static-module
format: 'es6', // 输出格式: es6|commonjs|amd|closure
create: true, // 是否需要 create方法
encode: true, // 是否需要 encode 方法
decode: true, // 是否需要 decode 方法
verify: true, // 是否需要 verify 方法
delimited: true, // 是否需要 delimited 方法
beautify: true, // 是否需要美化代码
comments: true, // 是否需要代码注释文档
convert: true // 是否需要 from/toObject
}
```
### Example
`demo.proto`
``` protobuf
package base;
syntax = "proto3";
message base {
string base = 1;
}
```
compile to `base.js`
In your js
``` js
var axios = require('axios');
var base = require('base.js');
// decode data
function fetchDecode(proto, data) {
return proto.decode(new Uint8Array(data));
}
// encode data
function fetchEncode(proto, data) {
return proto.encode(proto.create(data)).finish();
}
let buffer = fetchEncode(base.base, {'base': '1'});
axios.post('http://localhost:8080/protobuf', buffer, {
responseType: 'arraybuffer',
headers: {
'Content-type': 'application/octet-stream'
}
})
.then(res => {
let data = fetchDecode(base.base, res);
console.log(data);
});
```