Skip to content

ESM target that is compatible with node #1253

Description

@lgarron

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

Footnotes

  1. 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 of import.meta.resolve(…).

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions