💡 Feature description
I maintain several libraries (one big one in particular), with the following constraints:
We publish a single ESM build that runs directly without any further transformation in the following environments:
- All modern browsers
node
bun and similar environments that implement most browser APIs
- bundlers (i.e. can be transformed by bundlers into code that stays compatible with the above environments)
... given the following two assumptions:
- Package imports are supported (e.g. using import maps or a mapping to
node_modules)
new URL(…, import.meta.url)1 is correctly resolved or transformed for relative source files (particularly, for worker entry points and WASM entry files).
To this end, we are happy to include dynamic workarounds and imports for node compatibility, but we do not want to publish separate builds for different environments2. In the past, it was reasonable to make assumptions like "require means node, import means browser", but this is not the case anymore and all environments are converging on compatible functionality.
To that end, could I ask for an ESM target that works both in browsers and node directly? I believe this is possible in node using e.g. createRequire.
I'd be happy to contribute code to support this, if someone would indicate that it would be appropriate for a PR.
💡 Feature description
I maintain several libraries (one big one in particular), with the following constraints:
We publish a single ESM build that runs directly without any further transformation in the following environments:
nodebunand similar environments that implement most browser APIs... given the following two assumptions:
node_modules)new URL(…, import.meta.url)1 is correctly resolved or transformed for relative source files (particularly, for worker entry points and WASM entry files).To this end, we are happy to include dynamic workarounds and imports for
nodecompatibility, but we do not want to publish separate builds for different environments2. In the past, it was reasonable to make assumptions like "require means node, import means browser", but this is not the case anymore and all environments are converging on compatible functionality.To that end, could I ask for an ESM target that works both in browsers and
nodedirectly? I believe this is possible innodeusing e.g.createRequire.I'd be happy to contribute code to support this, if someone would indicate that it would be appropriate for a PR.
Footnotes
In the future, this should be
import.meta.resolve(…). Browsers have supported this for the better part of a year, but most bundlers still need to. As far as I'm concerned here,new URL(…, import.meta.url)is a backwards-compatible version ofimport.meta.resolve(…). ↩I've learned the hard way that I'd much rather deal with the challenge of maintaining a single published build like this, compare to publishing multiple builds and having to figure out which one to debug for a given report issue. ↩