Skip to content

Add support for pkg.exports.module or similar #11014

@ctavan

Description

@ctavan

I would like to move the discussion that started in uuidjs/uuid#462 (comment) and rollup/rollup#3514 (comment) into this project.

TL;DR: We do believe that, apart from support for pkg.exports.browser that is being added in #10953, we may need to support an additional key in the pkg.exports object.

Let's illustrate this with an example taken from uuid. Before pkg.exports we used to specify the following in package.json:

  "main": "./dist/index.js",
  "module": "./dist/esm-node/index.js",
  "browser": {
    "./dist/md5.js": "./dist/md5-browser.js",
    "./dist/rng.js": "./dist/rng-browser.js",
    "./dist/sha1.js": "./dist/sha1-browser.js",
    "./dist/esm-node/index.js": "./dist/esm-browser/index.js"
  },

The goal was the following:

  • Node.js native should pick main
  • Bundlers targeting Node.js should pick module
  • Bundlers targeting the browser would pick "module": "./dist/esm-node/index.js", but then use the override from browser: "./dist/esm-node/index.js": "./dist/esm-browser/index.js"
  • (Before we added the esm build, bundlers targeting the browser would have used the CommonJS build from main, hence the additional browser-overrides)

Now that Node.js has added and webpack is about to add pkg.exports support the above can no longer be achieved for webpack:

  "main": "./dist/index.js",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "browser": "./dist/esm-browser/index.js",
      "require": "./dist/index.js",
      "import": "./wrapper.mjs"
    }
  },
  "module": "./dist/esm-node/index.js",
  "browser": {
    "./dist/md5.js": "./dist/md5-browser.js",
    "./dist/rng.js": "./dist/rng-browser.js",
    "./dist/sha1.js": "./dist/sha1-browser.js",
    "./dist/esm-node/index.js": "./dist/esm-browser/index.js"
  },

Since Node.js potentially suffers from the dual package hazard uuid provides a ./wrapper.mjs which wraps the CJS build and exports it as ESM for pkg.exports.import.

While the browser use case is covered by pkg.exports.browser for webpack, things no longer work as expected when bundling for Node.js

While it would technically work to just fall back to pkg.exports.import and pkg.exports.require that is at least not an option for ESM-only bundlers like rollup, see rollup/rollup#3514.

Hence the need for one additional pkg.exports key that allows specifying a "pure esm" build to be used for Node.js bundling.

For more details see the discussion in uuidjs/uuid#462 (comment) onwards.

/cc @sokra @lukastaegert @guybedford @jkrems @evilebottnawi

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