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 5 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? Actually make the package importable? (does this work?)
  • 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?
  • 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!

@jakebailey
Copy link
Member Author

nodejs/node#52762 was merged a bit ago; I should be able to retarget this PR at nightly.

@jakebailey
Copy link
Member Author

@typescript-bot perf test this faster hosts=node@23.0.0

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 17, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
perf test this faster hosts=node@23.0.0 ✅ Started 👀 Results

@typescript-bot
Copy link
Collaborator

@jakebailey
The results of the perf run you requested are in!

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v23.0.0, x64)
Errors 31 31 ~ ~ ~ p=1.000 n=6
Symbols 62,340 62,340 ~ ~ ~ p=1.000 n=6
Types 50,379 50,379 ~ ~ ~ p=1.000 n=6
Memory used 193,727k (± 0.02%) 194,442k (± 0.02%) +715k (+ 0.37%) 194,412k 194,500k p=0.005 n=6
Parse Time 1.14s (± 1.99%) 1.17s (± 1.21%) +0.03s (+ 3.08%) 1.15s 1.19s p=0.023 n=6
Bind Time 0.56s 0.58s (± 0.71%) +0.02s (+ 3.27%) 0.57s 0.58s p=0.002 n=6
Check Time 7.49s (± 0.36%) 7.60s (± 0.81%) +0.11s (+ 1.42%) 7.53s 7.70s p=0.008 n=6
Emit Time 2.28s (± 0.33%) 2.40s (± 0.53%) 🔻+0.12s (+ 5.19%) 2.39s 2.42s p=0.004 n=6
Total Time 11.47s (± 0.34%) 11.74s (± 0.41%) +0.27s (+ 2.38%) 11.69s 11.81s p=0.005 n=6
angular-1 - node (v23.0.0, x64)
Errors 33 33 ~ ~ ~ p=1.000 n=6
Symbols 947,886 947,886 ~ ~ ~ p=1.000 n=6
Types 410,840 410,840 ~ ~ ~ p=1.000 n=6
Memory used 1,225,465k (± 0.00%) 1,226,690k (± 0.00%) +1,225k (+ 0.10%) 1,226,609k 1,226,739k p=0.005 n=6
Parse Time 5.65s (± 3.19%) 5.70s (± 3.64%) ~ 5.39s 5.85s p=0.228 n=6
Bind Time 1.74s (±11.31%) 1.83s (±10.71%) 🔻+0.10s (+ 5.56%) 1.71s 2.14s p=0.046 n=6
Check Time 25.87s (± 0.50%) 26.35s (± 0.35%) +0.48s (+ 1.85%) 26.23s 26.49s p=0.005 n=6
Emit Time 13.00s (± 0.66%) 13.68s (± 0.55%) 🔻+0.68s (+ 5.22%) 13.63s 13.83s p=0.005 n=6
Total Time 46.26s (± 0.45%) 47.57s (± 0.27%) +1.31s (+ 2.84%) 47.42s 47.76s p=0.005 n=6
mui-docs - node (v23.0.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,530,445 2,530,445 ~ ~ ~ p=1.000 n=6
Types 916,594 916,594 ~ ~ ~ p=1.000 n=6
Memory used 2,340,106k (± 0.00%) 2,341,324k (± 0.00%) +1,218k (+ 0.05%) 2,341,198k 2,341,420k p=0.005 n=6
Parse Time 9.33s (± 0.77%) 9.55s (± 0.55%) +0.22s (+ 2.30%) 9.47s 9.62s p=0.005 n=6
Bind Time 2.23s (± 0.54%) 2.30s (± 0.59%) +0.07s (+ 3.14%) 2.28s 2.32s p=0.004 n=6
Check Time 74.99s (± 1.12%) 75.36s (± 0.73%) ~ 74.86s 76.39s p=0.229 n=6
Emit Time 0.38s (±48.10%) 0.30s (± 1.35%) ~ 0.30s 0.31s p=0.248 n=6
Total Time 86.93s (± 1.01%) 87.51s (± 0.59%) +0.58s (+ 0.67%) 87.04s 88.46s p=0.045 n=6
self-build-src - node (v23.0.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,258,032 1,258,086 +54 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,259 266,404 +145 (+ 0.05%) ~ ~ p=0.001 n=6
Memory used 2,422,147k (± 0.01%) 2,433,135k (± 0.01%) +10,988k (+ 0.45%) 2,432,859k 2,433,402k p=0.005 n=6
Parse Time 4.08s (± 0.89%) 4.27s (± 0.65%) 🔻+0.19s (+ 4.65%) 4.23s 4.31s p=0.005 n=6
Bind Time 1.75s (± 0.60%) 1.80s (± 0.29%) +0.05s (+ 2.75%) 1.80s 1.81s p=0.004 n=6
Check Time 26.72s (± 0.23%) 26.97s (± 0.49%) +0.25s (+ 0.93%) 26.87s 27.21s p=0.005 n=6
Emit Time 1.97s (± 0.75%) 2.02s (± 0.49%) +0.05s (+ 2.71%) 2.01s 2.04s p=0.005 n=6
Total Time 34.56s (± 0.26%) 35.07s (± 0.40%) +0.51s (+ 1.48%) 34.92s 35.32s p=0.005 n=6
self-build-src-public-api - node (v23.0.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,258,032 1,258,086 +54 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,259 266,404 +145 (+ 0.05%) ~ ~ p=0.001 n=6
Memory used 2,503,622k (± 0.01%) 2,526,649k (± 0.02%) +23,026k (+ 0.92%) 2,526,334k 2,527,559k p=0.005 n=6
Parse Time 4.34s (± 1.03%) 4.47s (± 0.72%) +0.13s (+ 3.11%) 4.43s 4.51s p=0.005 n=6
Bind Time 1.51s (± 0.27%) 1.56s (± 0.33%) +0.04s (+ 2.98%) 1.55s 1.56s p=0.003 n=6
Check Time 27.55s (± 0.31%) 27.89s (± 0.25%) +0.35s (+ 1.25%) 27.82s 27.98s p=0.005 n=6
Emit Time 1.98s (± 0.87%) 2.08s (± 0.66%) 🔻+0.10s (+ 5.21%) 2.06s 2.10s p=0.005 n=6
Total Time 35.38s (± 0.32%) 36.01s (± 0.23%) +0.63s (+ 1.78%) 35.94s 36.15s p=0.005 n=6
self-compiler - node (v23.0.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 261,789 261,815 +26 (+ 0.01%) ~ ~ p=0.001 n=6
Types 106,511 106,599 +88 (+ 0.08%) ~ ~ p=0.001 n=6
Memory used 439,547k (± 0.02%) 440,625k (± 0.03%) +1,078k (+ 0.25%) 440,483k 440,879k p=0.005 n=6
Parse Time 3.09s (± 1.50%) 3.19s (± 1.11%) +0.10s (+ 3.18%) 3.15s 3.22s p=0.005 n=6
Bind Time 1.13s (± 1.22%) 1.16s (± 1.15%) +0.03s (+ 2.96%) 1.14s 1.17s p=0.007 n=6
Check Time 14.80s (± 0.35%) 15.09s (± 0.15%) +0.29s (+ 1.94%) 15.05s 15.11s p=0.005 n=6
Emit Time 1.15s (± 0.65%) 1.20s (± 1.01%) +0.04s (+ 3.91%) 1.18s 1.21s p=0.005 n=6
Total Time 20.18s (± 0.27%) 20.63s (± 0.22%) +0.46s (+ 2.27%) 20.58s 20.71s p=0.005 n=6
ts-pre-modules - node (v23.0.0, x64)
Errors 68 68 ~ ~ ~ p=1.000 n=6
Symbols 225,919 225,919 ~ ~ ~ p=1.000 n=6
Types 94,415 94,415 ~ ~ ~ p=1.000 n=6
Memory used 372,382k (± 0.01%) 373,221k (± 0.01%) +839k (+ 0.23%) 373,153k 373,283k p=0.005 n=6
Parse Time 2.50s (± 1.06%) 2.58s (± 2.04%) +0.08s (+ 3.13%) 2.51s 2.65s p=0.015 n=6
Bind Time 1.40s (± 1.05%) 1.43s (± 0.73%) +0.03s (+ 2.38%) 1.42s 1.45s p=0.012 n=6
Check Time 12.60s (± 0.52%) 12.85s (± 0.50%) +0.24s (+ 1.93%) 12.80s 12.97s p=0.005 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 16.52s (± 0.50%) 16.87s (± 0.49%) +0.35s (+ 2.13%) 16.76s 17.00s p=0.005 n=6
vscode - node (v23.0.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 3,118,670 3,118,670 ~ ~ ~ p=1.000 n=6
Types 1,074,955 1,074,955 ~ ~ ~ p=1.000 n=6
Memory used 3,211,514k (± 0.02%) 3,212,413k (± 0.00%) +899k (+ 0.03%) 3,212,340k 3,212,534k p=0.005 n=6
Parse Time 12.42s (± 1.14%) 12.73s (± 0.52%) +0.31s (+ 2.51%) 12.64s 12.83s p=0.005 n=6
Bind Time 4.02s (± 3.00%) 4.16s (± 2.99%) ~ 4.03s 4.28s p=0.065 n=6
Check Time 70.77s (± 0.49%) 72.30s (± 0.36%) +1.53s (+ 2.16%) 71.94s 72.70s p=0.005 n=6
Emit Time 22.72s (±10.89%) 25.78s (± 0.86%) 🔻+3.05s (+13.43%) 25.52s 26.13s p=0.005 n=6
Total Time 109.93s (± 2.00%) 114.97s (± 0.35%) 🔻+5.04s (+ 4.58%) 114.39s 115.37s p=0.005 n=6
webpack - node (v23.0.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 286,241 286,241 ~ ~ ~ p=1.000 n=6
Types 116,005 116,005 ~ ~ ~ p=1.000 n=6
Memory used 437,913k (± 0.02%) 438,988k (± 0.03%) +1,075k (+ 0.25%) 438,769k 439,069k p=0.005 n=6
Parse Time 4.18s (± 0.47%) 4.24s (± 0.67%) +0.06s (+ 1.52%) 4.19s 4.27s p=0.009 n=6
Bind Time 1.66s (± 1.25%) 1.73s (± 1.34%) 🔻+0.07s (+ 4.33%) 1.70s 1.76s p=0.005 n=6
Check Time 16.35s (± 0.32%) 16.75s (± 0.07%) +0.40s (+ 2.45%) 16.74s 16.77s p=0.005 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 22.19s (± 0.30%) 22.72s (± 0.21%) +0.54s (+ 2.43%) 22.67s 22.80s p=0.005 n=6
xstate-main - node (v23.0.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 543,130 543,130 ~ ~ ~ p=1.000 n=6
Types 181,889 181,889 ~ ~ ~ p=1.000 n=6
Memory used 486,951k (± 0.01%) 487,800k (± 0.01%) +849k (+ 0.17%) 487,769k 487,844k p=0.005 n=6
Parse Time 3.46s (± 1.35%) 3.55s (± 0.96%) +0.09s (+ 2.75%) 3.51s 3.60s p=0.005 n=6
Bind Time 1.11s (± 0.57%) 1.16s (± 1.52%) 🔻+0.04s (+ 4.05%) 1.13s 1.18s p=0.004 n=6
Check Time 17.81s (± 0.40%) 18.09s (± 0.32%) +0.28s (+ 1.54%) 18.02s 18.17s p=0.005 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 22.38s (± 0.52%) 22.79s (± 0.28%) +0.41s (+ 1.85%) 22.69s 22.89s p=0.005 n=6
System info unknown
Hosts
  • node (v23.0.0, x64)
Scenarios
  • Compiler-Unions - node (v23.0.0, x64)
  • angular-1 - node (v23.0.0, x64)
  • mui-docs - node (v23.0.0, x64)
  • self-build-src - node (v23.0.0, x64)
  • self-build-src-public-api - node (v23.0.0, x64)
  • self-compiler - node (v23.0.0, x64)
  • ts-pre-modules - node (v23.0.0, x64)
  • vscode - node (v23.0.0, x64)
  • webpack - node (v23.0.0, x64)
  • xstate-main - node (v23.0.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@jakebailey
Copy link
Member Author

Huh, where is the perf hit coming from here...?

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.

2 participants