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

Probably invalid `$protobuf` import statement in static module

Open charlie-wasp opened this issue 4 years ago • 3 comments

protobuf.js version: 6.9.0

Hi! I want to use CLI to generate static code from my .proto file. I used the next command:

$ yarn pbjs -t static-module -w es6 -o packages/protobuf-encoder/generated/message_pb.js message.proto

But it seems that generated file contains invalid import statement on top:

// message_pb.js file
import * as $protobuf from "protobufjs/minimal";

...

If we look at minimal.js file, we'll find there CommonJS-style export module.exports = require("./src/index-minimal");. AFAIK in ES6 we can import such modules, using only default export. So the valid import should be import $protobuf from "protobufjs/minimal". I edited the generated file manually and it works now.

Am I missing something here?

charlie-wasp avatar Sep 27 '21 14:09 charlie-wasp

Yeah we are seeing this as well. It's kindof a shame that static module is not fully standalone. even if there a separate npm package of protofubjs-minimal rather than pulling in the entire dependency tree of protobufjs would event be sufficient.

untra avatar Feb 15 '22 14:02 untra

Any update on this?

kpruden avatar Aug 23 '22 23:08 kpruden

should be

import $protobuf from "protobufjs/minimal.js";

SteveCruise avatar Aug 31 '22 02:08 SteveCruise

Same problem here. We originally solved this with a sed statement in our script:

sed -i'' 's/import \* as $$protobuf from \"protobufjs\/minimal\";/import $$protobuf from \"protobufjs\/minimal\.js\";/' gen/protos_pb.js;

But that obviously felt wrong. After some digging, realized you can specify a path to a custom wrapper in your pbjs command:

pbjs -t static-module -w path/to/my_wrapper.js --es6 -o gen/protos_pb.js test.proto

where my_wrapper.js looks like this:

import $protobuf from "protobufjs/minimal.js";

$OUTPUT;

export { $root as default };

Still not great and would be nice if this was handled by the native es6 wrapper, but the custom wrapper feels better than sed.

smaye81 avatar Feb 09 '23 17:02 smaye81