protobufjs对import 其它proto文件的处理问题
发布于 8 年前 作者 mrluoming 11914 次浏览 来自 问答

前提: 1、定义了2个.proto文件 1)common.proto

package common;
 message item
{
required int32 index = 1;
required string score = 2;
}

2)lm.helloworld.proto(引用了common.proto)

import "common.proto";
package lm;
message hellounit
{
    required int32 index = 1;
    required string score = 2;
}
message helloworld 
{ 
   required int32     id = 1;  // ID 
   required string    str = 2;  // str
   repeated hellounit     helloArr = 3;  //optional field
}

3)使用pbjs命令:pbjs proto/lm.helloworld.proto -t commonjs > proto_to_js/lm.helloworld.js 生成对应的js代码:

module.exports = require("protobufjs").newBuilder({})['import']({
    "package": null,
    "messages": [
        {
            "name": "common",
            "fields": [],
            "messages": [
                {
                    "name": "item",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "index",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "score",
                            "id": 2
                        }
                    ]
                }
            ]
        },
        {
            "name": "lm",
            "fields": [],
            "messages": [
                {
                    "name": "hellounit",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "index",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "score",
                            "id": 2
                        }
                    ]
                },
                {
                    "name": "helloworld",
                    "fields": [
                        {
                            "rule": "required",
                            "type": "int32",
                            "name": "id",
                            "id": 1
                        },
                        {
                            "rule": "required",
                            "type": "string",
                            "name": "str",
                            "id": 2
                        },
                        {
                            "rule": "repeated",
                            "type": "hellounit",
                            "name": "helloArr",
                            "id": 3
                        }
                    ]
                }
            ]
        }
    ]
}).build();

问题: 被引入的common.proto文件的内容在最终的js文件被展开了,如果common.proto文件有新的定义增加进来,则凡是引入该文件的proto对应的js代码都需要更新; 有没有办法让common.proto文件的内容在相关的js文件不被展开,而只是require 'common.js’就可以了,这样关联的js代码不需要更新。

5 回复

@alsotang 我就是基于这个库出现这个问题的

@mrluoming 用这个库的话,不用 pbjs 提前编译啊。不是有这个 api 可以当场编译吗? ProtoBuf.loadProtoFile("path/to/file.proto");

@alsotang 谢谢 我试试

回到顶部