Make TypeScript itself ESM-only, made possible by require(ESM) #58419
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 constructts.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, oncerequire(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:
__esModule
require(ESM)
having__esModule
. But I suspect that exportingdefault
fixes this.require
ifprocess.getBuiltinModule
is not found@types/diff
is typed wrong