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

Support importing modules from typescript files ? #25

Closed
Niceplace opened this issue Feb 18, 2022 · 4 comments
Closed

Support importing modules from typescript files ? #25

Niceplace opened this issue Feb 18, 2022 · 4 comments

Comments

@Niceplace
Copy link

Niceplace commented Feb 18, 2022

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: pinojs/pino#1243 (comment)

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
Copy link
Member

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

@Niceplace
Copy link
Author

@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
Copy link
Member

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

@castarco
Copy link

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: pinojs/thread-stream#71

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants