protobuf.js
protobuf.js copied to clipboard
Probably invalid `$protobuf` import statement in static module
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?
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.
Any update on this?
should be
import $protobuf from "protobufjs/minimal.js";
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.