protobuf.js
protobuf.js copied to clipboard
No export from protobufjs/minimal causing errors in es6
Trying to import protobufjs/minimal (from the generated code) generates an error because protobufjs/minimal provides no exports.
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
import * as $protobuf from "protobufjs/minimal";
// Common aliases
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
// Exported root namespace
const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
putting a break point shows $protobuf == undefined because it appears this library relies on side effects rather than exporting a variable. The generated code can be fixed by changing the first few lines to:
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
import "protobufjs/minimal";
let $protobuf = protobuf;
// Common aliases
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
// Exported root namespace
const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
Those PRs were open almost a year ago to solve the issue
- https://github.com/protobufjs/protobuf.js/pull/1930
- https://github.com/protobufjs/protobuf.js/pull/1931