pinojs / real-require

Keep require and import consistent after bundling or transpiling.
MIT License
8 stars 2 forks source link

Support importing modules from typescript files ? #25

Closed Niceplace closed 2 years ago

Niceplace commented 2 years ago

Hello there!

Note: I know I am mentioning a lot of different repositories from the pinojs organisation but the reason why I am opening this issue here is I believe I have tracked down the root cause of my problem to the realImport method. Feel free to redirect me somewhere else if this is not the appropriate place to have this discussion.

Problem description

I'm in a bit of a tough spot, trying to build a custom transport for pino. My project is built in Typescript and I use Ts-Node to run it. It handles transpilation + execution in memory so I have no generated js files.

The problem I am facing is that I am unable to create a custom transport in Typescript. I have detailed my investigation (with a reproducible example) in an issue in the pinojs repository, mentioning Typescript & v7 transports (apologies for the long texts):

I believe that the root cause is triggered in the thread-stream library, which uses real-require: https://github.com/pinojs/thread-stream/blob/22d484fa3b7a8125e0fec872b6fd6a5d8ed2049a/lib/worker.js#L21. This is why I decided to open the issue here.

When using a custom transport written in typescript, I can see the following error if I put a breakpoint on the catch statement right after the call to realImport

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/sbeaulieu/workspace/test-datadog-pino/app/ts/customTransport.ts [...]

I do not get that error if my transport is a javascript file (which makes sense).

Proposed solution

I am wondering if it would be possible for real-require to be aware of the existence of ts-node (or any other transpiler) and import modules from typescript files successfully? I'm wondering if this solution could be adapted somehow: https://github.com/pinojs/pino/issues/1243#issuecomment-994183709

I want to point out that this issue does not happen in the scenario where typescript code is transpiled first and then the javascript code is executed. My use case is specific to using ts-node to transpile+run everything on the fly.

Also, does it make sense to approach this problem like that? Maybe the detection of ts-node should happen somewhere else ? I'm not sure.

As of now, I have no idea of a specific solution to my problem but I figured I would at least ask.

Cheers !

mcollina commented 2 years ago

Thanks for the long explanation! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Niceplace commented 2 years ago

@mcollina While I would absolutely love to send a PR (with tests !), I still have unanswered questions about how to approach the problem 😅 I have no idea how to do it or if it's even possible. I think I would need a bit of guidance first.

mcollina commented 2 years ago

Check how https://github.com/fastify/fastify-autoload detects ts-node.

castarco commented 2 years ago

I believe we don't really have to touch real-require. I pushed a PR for thread-stream that should be able to help in this situation: https://github.com/pinojs/thread-stream/pull/71