emscripten-core / emscripten-core/emscripten

Replace ENVIRONMENT_* logic with Web default + polyfills?

Open
#12,203 12 comments 6 reactions 0 assignees View on GitHub
code size
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Right now we support various "environments": web, node, other JS shells, etc. We need to use different code paths for loading a file, for example - fetch on the web versus node's `fs` package, and so forth. We have a layer of indirection for this, where in `shell.js` the environment is detected and hooks like `readBinary`, `readAsync` are set up.

This has two current downsides. First, that while you can specify that only a single target environment is supported, the detection + indirection layer may still end up increasing code size. Second, we get requests for more environments to be supported now and then, like electron variants or deno, and they require adding an entire new "environment".

Instead, what if we made the following change:
* The default code path uses Web APIs directly.
* To support other environments, they would polyfill the Web environment. For example, the default code path would use `fetch`, and other environments would need to implement a minimal polyfill for that. So for node, `fs.readFile` would be used in the polyfill, etc.

Benefits:
* In practice, the web target is the one that cares the most about code size. By having the default code path use Web APIs, code size would be minimal there.
* Supporting more environments can be done in a pluggable way: add a file with code to polyfill Web APIs for whatever you want. We can have `env/node.js, env/deno.js` etc. as needed, and a user can also add a custom one of their own just by including the code with `--pre-js`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.