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

fix usage of global object in browser environment

Open quanterion opened this issue 7 years ago • 4 comments

Browser code should not rely on things that are not available in browser environments. Otherwise it throws error global is undefined.

https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814

quanterion avatar Mar 07 '18 11:03 quanterion

The expectation here is that the sources won't be used in something else than node, while the dist files can be used in various environments. Anyway, there are multiple options to solve such issues:

  1. If a project runs in the browser, depend on the dist files instead of the sources.
  2. We could add a browser entry to package.json to automate this, but I am not sure that each and every bundler honors this.
  3. Feature-check in the node sources, possibly making bundlers with default settings pull in unnecessary / possibly large / possibly breaking shims. (That's the issue the linked comment above overlooks.)

dcodeIO avatar Mar 07 '18 12:03 dcodeIO

It would be great to implement 2nd option. Webpack is the most popular bundler in the wild, I think it should compatible with it. I can try to make a PR if you hint me how to do that. I can't understand how to add browser entry for full and minimal libraries.

quanterion avatar Mar 07 '18 16:03 quanterion

I can't understand how to add browser entry for full and minimal libraries.

Good point. Maybe the package should expose the dist files for everything, and just use the sources for building.

dcodeIO avatar Mar 07 '18 16:03 dcodeIO

Yeah.. As I can see in many modern libraries like RxJS, angular, typescript etc. They all expose dist files only

quanterion avatar Mar 12 '18 05:03 quanterion