I have a codebase that should run in the browser, and in node. It needs to switch some functionality based on if it is running in node, or the browser.
Previously I wrote modules in a CommonJS format. And I had a "shim" module, where I'd test the environment for features, and export things based on that. So all conditional behavior was contained to this shim file.
Now, I've tried to create "shim" ESM module. I can't figure out how. Some of the switching behavior depends on checking for the presence of other ESM modules. But you can only do a test like this inside a dynamic ESM import, which is asynchronous. And because export statements must be at the top level, I can't export the result, unless top level await is available. But this isn't available in the browser.
In my current hack, I export a deferred-like object, and await it where needed. But this has made a lot of my unnecessarily wrapped in (async()=>{...}).
So how do I do this?
I'd prefer to not use a bundler, my browser target is guaranteed to be fairly recent.