TypeStrong / ts-node

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

Compile not outputing expected format #2121

Open nonara opened 1 month ago

nonara commented 1 month ago

Hey Andrew (@cspotcode)! Hope you're doing well.

I wanted to drop a quick heads up here on a potential issue. I'm not entirely sure if this is unexpected behaviour, so I'll just give the highlights and if it sounds like something is up, I can rewrite this with proper bug-report format & make a repro.

Backstory

We needed to make some changes to ts-patch to support the forthcoming typescript 5.5 release. They're doing some pretty interesting things to be able to support ESM. Not sure exactly what the status is on that as yet, but I know a lot of ground work has been laid.

After completing the changes, I ran into something that looks like it may be an issue on ts-node side, but again, I'm not certain.

Issue

We have been using ts-node to pre-compile typescript ESM transformers to JS, so we can use the esm library to require them. We did this via tsNodeInstance.compile(code, filePath).

This still works if I give it a file whose path is explicitly ".mts", but if it's ".ts", it seems to compile as cjs code.

Workaround & Other Details

I've confirmed and the config + compiler options specify both module and target as ESNext

As a workaround, I made it change the provided path to explicitly be "mts".

You can see the workaround here:

https://github.com/nonara/ts-patch/blob/master/projects/patch/src/plugin/esm-intercept.ts#L87-L94

Here is where we specify tsConfig:

https://github.com/nonara/ts-patch/blob/master/projects/patch/src/plugin/register-plugin.ts#L111-L120

I've confirmed it does register with those options via debugger.

If I'm just missing something, feel free to let me know! Otherwise, if you'd like to check into it, let me know, and I can make you a repro.