TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.9k stars 535 forks source link

TS's new node ESM support #1414

Closed cspotcode closed 2 years ago

cspotcode commented 3 years ago

Will need work on our side to integrate.

https://github.com/microsoft/TypeScript/pull/44501 https://github.com/weswigham/TypeScript/pull/67

Roughly

adds new module: node12 and module: nodeNext configs In them:

Questions I still have

Are any of the above assumed to be modules without needing to add export {};?

alvis commented 3 years ago

With 4.5 beta announced 2 weeks ago, and the final on the horizon, time for us to do something.

cspotcode commented 3 years ago

@alvis are you referring to a pull request that you have submitted?

cspotcode commented 3 years ago

I've assigned this to @alvis and added it to our next milestone. Let us know if you need any guidance to implement the necessary changes. I've added some notes below.

At a minimum, you'll need to add require.extensions hooks for mts, mjs, cts, and cjs extensions. You may also need to add them to the extensions passed to createResolve:

https://github.com/TypeStrong/ts-node/blob/3b403651193c0ce942b215c44f6db805b4218f4c/src/esm.ts#L50-L54 https://github.com/TypeStrong/ts-node/blob/3b403651193c0ce942b215c44f6db805b4218f4c/src/index.ts#L482-L491

Should the new extensions be enabled conditionally, only when your TS version is >= 4.5?

I don't expect you'll need to implement any fancy resolving logic beyond that. TS already handles resolving within the typechecker, and node's existing resolution logic should continue to behave.

PodaruDragos commented 2 years ago

Will this mean that we can now potentially have import { foo } from './foo', basically omitting the extensions just like in commonJS ?

cspotcode commented 2 years ago

@PodaruDragos you're probably looking for node's --experimental-specifier-resolution=node which we already support. Just as node requires you to pass that flag, we also require you to pass that flag, often via the NODE_OPTIONS environment variable.

https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm