TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open jakebailey opened this issue 1 year ago • 9 comments

https://github.com/nodejs/node/pull/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 https://github.com/nodejs/node/issues/52599 and https://github.com/nodejs/node/pull/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.

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.
  • [x] Consider looking for require if process.getBuiltinModule is not found
  • [ ] Export maps? Actually make the package importable? (does this work?)
  • [x] ~Maybe still ship CJS behind a condition to get this out sooner?~
    • ~Dual package hazard is very scary here. Maybe acceptable with module condition/top-level prop?~
  • [x] ~Stop bundling? Share code between tsc.js/typescript.js?~
  • [ ] Is this even a good idea before we figure out a better public API than our current huge omnibus?
  • [x] @types/diff is typed wrong

jakebailey avatar May 02 '24 22:05 jakebailey

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.

typescript-bot avatar May 02 '24 23:05 typescript-bot

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 avatar May 07 '24 01:05 jakebailey

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.

typescript-bot avatar May 07 '24 03:05 typescript-bot

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

jakebailey avatar May 07 '24 04:05 jakebailey

https://github.com/nodejs/node/pull/52762 was merged a bit ago; I should be able to retarget this PR at nightly.

jakebailey avatar May 31 '24 22:05 jakebailey