protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

pbjs overwrite package so seperate compile multiple file of one package is not allowed

Open yangjuncode opened this issue 5 years ago • 1 comments

protobuf.js version: 6.8.8

we have two proto file a.proto and b.proto

syntax = "proto3";
package ttt;
message A{
    string RID = 1;
}
syntax = "proto3";
package ttt;
message B{
    string RID = 1;
}

when build with pbjs with two file together, the generated file is ok,$root.ttt.A and $root.ttt.B is set right.

pbjs -t static-module -w es6 a.proto b.proto > ttt.js

when build a.proto and b.proto seperately, each output has reset the $root.ttt

export const ttt = $root.ttt = (() => {

    /**
     * Namespace ttt.
     * @exports ttt
     * @namespace
     */
    const ttt = {};

so when import one generated js file will overwrite any previous setting with the same package name.

the solution is changing cli/targets/static.js line 132 to below

push((config.es6 ? "const" : "var") + " " + escapeName(ns.name) + " = {};");

to

push((config.es6 ? "const" : "var") + " " + escapeName(ns.name) + " = $root."+escapeName(ns.name)+" || {};");

yangjuncode avatar Mar 31 '20 10:03 yangjuncode