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

No export from protobufjs/minimal causing errors in es6

Open Jman0519 opened this issue 1 year ago • 1 comments

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

Jman0519 avatar Mar 06 '25 02:03 Jman0519

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

nicolasroger17 avatar Apr 10 '25 09:04 nicolasroger17