Open
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
.