Skip to content

enable conditional exports #4448

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

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open

enable conditional exports #4448

wants to merge 3 commits into from

Conversation

yaacovCR
Copy link
Contributor

@yaacovCR yaacovCR commented Jul 2, 2025

depends on #4446

before:

image

i.e. importing from esm gives you cjs

after:

image

we have conditional exports, and it looks like nothing has changed.....

BUT:

Any version of Node honoring the module-sync condition i.e. where require(esm) is enabled will load ESM both when imported and required, i.e. ^20.19.0 || ^22.12.0 || >=23.0.0, i.e. latest versions of all current Node.JS releases.

We have been delaying this for some time secondary to dual-package-hazard concerns, but this looks like we can release ESM to the broader community via package exports while retaining whatever legacy CJS build we might need for those environments that need it => looking at you Jest.

We can look into whether we want to drop cjs support entirely as a follow-on PR => see #4437

@yaacovCR yaacovCR requested a review from a team as a code owner July 2, 2025 11:13
@yaacovCR yaacovCR added the PR: feature 🚀 requires increase of "minor" version number label Jul 2, 2025
@yaacovCR
Copy link
Contributor Author

yaacovCR commented Jul 2, 2025

The secret sauce is here:

https://github.com/yaacovCR/graphql-js/blob/dual/resources/build-npm.ts#L204-L221

function buildExports(filepath: string): ConditionalExports {
  const { dir, name } = path.parse(filepath);
  const base = `./${path.join(dir, name)}`;
  return {
    types: {
      module: `${base}.d.mts`,
      'module-sync': `${base}.d.mts`,
      node: `${base}.d.ts`,
      require: `${base}.d.ts`,
      default: `${base}.d.mts`,
    },
    module: `${base}.mjs`,
    'module-sync': `${base}.mjs`,
    node: `${base}.js`,
    require: `${base}.js`,
    default: `${base}.mjs`,
  };
}

Based on:

nodejs/node#52174

@yaacovCR
Copy link
Contributor Author

yaacovCR commented Jul 2, 2025

This solves node -- but what about bun and deno:

Bun seems to support: ["bun", "node", "require", "import", "default"] so our current scheme would also seem to give cjs.
https://bun.sh/docs/runtime/modules#importing-packages
oven-sh/bun#20768

Deno seems to support: ["deno", "node", "import", "default"] so our current scheme would seem to give cjs.
https://docs.deno.com/runtime/fundamentals/node/#conditional-exports
denoland/deno#29970

We would probably need to add custom conditions for bun and deno.

@yaacovCR
Copy link
Contributor Author

yaacovCR commented Jul 4, 2025

With further testing, it seems that require(esm) on deno, at least how I am using it, does not honor the deno condition? So we can set deno to cjs or just drop it, because deno does consistently honor the node condition….

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: feature 🚀 requires increase of "minor" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant