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

Make TypeScript itself ESM-only, made possible by require(ESM) #58419

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented May 2, 2024

nodejs/node#51977 (behind --experimental-require-module in Node 22) enables Node to require ESM so long as that ESM does not make use of top-level await. TypeScript would only need top-level await to construct ts.sys at startup, which must be defined when the environment is detected to be Node. Via nodejs/node#52599 and nodejs/node#51977, we can synchronously access Node's built-ins, and therefore can offer up a TLA-free public API, enabling TypeScript to ship as ESM-only without breaking CJS users, once require(ESM) is unflagged.

The bulk of this change is just the extension rewriting, which we may want to consider taking to main just to make a PR like this one clearer.

For casual onlookers, I don't think this PR is going to "happen" any time soon (where "now" is currently May 2024); require(ESM) is new, flagged, and TS will likely need to support versions of Node until they're sufficiently EOL for us to feel comfortable dropping them. But it's incredible that this is possible.

TODO:

  • Fix public API dts
  • Figure out __esModule
    • The real question is the context of require(ESM) having __esModule. But I suspect that exporting default fixes this.
  • Consider looking for require if process.getBuiltinModule is not found
  • Export maps?
  • Maybe still ship CJS behind a condition to get this out sooner?
  • Stop bundling? Share code between tsc.js/typescript.js?
  • Probably a billion other things
  • Is this even a good idea before we figure out a better public API than our current huge omnibus?
  • @types/diff is typed wrong

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels May 2, 2024
@typescript-bot
Copy link
Collaborator

Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @mjbvz, @zkat, and @joj for you. Feel free to loop in other consumers/maintainers if necessary.

@jakebailey
Copy link
Member Author

This has been rebased now that we're using extensions on main, condensing this down to a pretty small change (though there's still stuff not working).

@jakebailey jakebailey force-pushed the its-esm branch 2 times, most recently from bed73cc to 739c9bf Compare May 7, 2024 02:42
@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@jakebailey
Copy link
Member Author

There are still a bunch of TODOs, but this is actually green! Wow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants