Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Tracking issue for browser compatibility #53

Open
21 of 22 tasks
kasperisager opened this issue Oct 29, 2021 · 7 comments
Open
21 of 22 tasks

Tracking issue for browser compatibility #53

kasperisager opened this issue Oct 29, 2021 · 7 comments

Comments

@kasperisager
Copy link
Contributor

kasperisager commented Oct 29, 2021

This issue tracks the remaining blockers for bundling and running Hypercore in a browser without shims. In no particular order, originally based on #52 (comment):

@kasperisager
Copy link
Contributor Author

kasperisager commented Nov 2, 2021

@mafintosh I'm at a bit of an impasse with regards to EventEmitter. It's simple enough to shim with the http://npmjs.com/events module but that module cannot be installed without forcing every other dependency that imports events to use it as well. This means that the onus will be on consumers to install that module if their bundler doesn't automagically include it. esbuild, for example, will throw an error if the module isn't installed.

One option is to adopt the EventTarget web API which is supported as of Node.js 14. It's quite verbose compared to the EventEmitter API, however. Another option is forking the http://npmjs.com/events module under a name that doesn't clash with the builtin module and maybe even just fall back to the builtin module in Node.js. Thoughts?

@mafintosh
Copy link
Collaborator

@kasperisager ya, i say we revisit that one later and for now people need to inject that. in practice i haven't heard any "complaints" about that so we can think it through a bit.

@kasperisager
Copy link
Contributor Author

kasperisager commented Nov 2, 2021

I can contribute the first use case: The thing I'm currently working on is a browser first, Node.js second type of thing that uses streamx. Being browser first, adding events as a direct dependency seems fine, but of course does incur an unnecessary overhead when running the thing in Node.js.

@mafintosh
Copy link
Collaborator

actually zero overhead right, cause node should just skip it?

@mafintosh
Copy link
Collaborator

if you manually npm install events before bundling, does that work atm?

@kasperisager
Copy link
Contributor Author

actually zero overhead right, cause node should just skip it?

That's absolutely true, I missed that! To actually import the events module in Node.js, and not the builtin module, one has to append a slash to the module name:

const events = require('events/')

if you manually npm install events before bundling, does that work atm?

The following import indeed uses the builtin module in Node.js and the events module in browsers, if installed:

const events = require('events')

No cow on the ice 😉 The only gotcha is of course the DX of the whole thing; modules that use the builtin events module without declaring events as a dependency won't Just Work(tm) when bundled.

@mafintosh
Copy link
Collaborator

Ya def, on the latter was more practical for now rather than a solution. I don’t mind potentially just including events everywhere, but we should enumerate how many modules that involves and it could be a hard ask for third party ones - let’s see

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants