Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: implement the "module-sync" exports condition #54648

Merged
merged 1 commit into from
Sep 25, 2024

Commits on Sep 23, 2024

  1. module: implement the "module-sync" exports condition

    This patch implements a "module-sync" exports condition
    for packages to supply a sycnrhonous ES module to the
    Node.js module loader, no matter it's being required
    or imported. This is similar to the "module" condition
    that bundlers have been using to support `require(esm)`
    in Node.js, and allows dual-package authors to opt into
    ESM-first only newer versions of Node.js that supports
    require(esm) while avoiding the dual-package hazard.
    
    ```json
    {
      "type": "module",
      "exports": {
        "node": {
          // On new version of Node.js, both require() and import get
          // the ESM version
          "module-sync": "./index.js",
          // On older version of Node.js, where "module" and
          // require(esm) are not supported, use the transpiled CJS version
          // to avoid dual-package hazard. Library authors can decide
          // to drop support for older versions of Node.js when they think
          // it's time.
          "default": "./dist/index.cjs"
        },
        // On any other environment, use the ESM version.
        "default": "./index.js"
      }
    }
    ```
    
    We end up implementing a condition with a different name
    instead of reusing "module", because existing code in the
    ecosystem using the "module" condition sometimes also expect
    the module resolution for these ESM files to work in CJS
    style, which is supported by bundlers, but the native
    Node.js loader has intentionally made ESM resolution
    different from CJS resolution (e.g. forbidding `import
    './noext'` or `import './directory'`), so it would be
    semver-major to implement a `"module"` condition
    without implementing the forbidden ESM resolution rules.
    For now, this just implments a new condition as semver-minor
    so it can be backported to older LTS.
    
    Refs: https://webpack.js.org/guides/package-exports/#target-environment-independent-packages
    joyeecheung committed Sep 23, 2024
    Configuration menu
    Copy the full SHA
    e96699e View commit details
    Browse the repository at this point in the history