Skip to content

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

Description

@yangjuncode

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)+" || {};");

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions