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

[How To] Use with tsx runner #148

Open
joliss opened this issue Mar 6, 2024 · 1 comment
Open

[How To] Use with tsx runner #148

joliss opened this issue Mar 6, 2024 · 1 comment

Comments

@joliss
Copy link

joliss commented Mar 6, 2024

I'm using tsx, a ts-node alternative for running .ts files with Node. Is there a some way to run ts-patch projects with tsx?

@joliss joliss changed the title [How To] Use with tsx [How To] Use with tsx runner Mar 6, 2024
@joliss joliss changed the title [How To] Use with tsx runner [How To] Use with tsx runner Mar 6, 2024
@cmidgley
Copy link

For those interested in tsx with ts-patch, I have it working with my transformers. The reason it doesn't "just work" is because tsx uses esbuild, which means it does not call the typescript compiler API's that ts-patch modifies.

The trick is to get it to load your transformer before tsx so the input to esbuild is the output from your transformer. To do this, you need to use the node cli and not tsx, since once it starts running it will go directly to esbuild and never call your transformer.

For example, with modern register-based transformers (and having previously used ts-patch install):

node --import yourTransformer --import tsx myFile.ts

or this for legacy loader-based transformers:

node --loader yourTransformer --import tsx myFile.ts

This will cause your transformer to load before tsx, allowing the files to be processed by it before sending on to tsx and esbuild.

Also, if you have custom tsconfig.json file, you can specify that with the environment variable TSX_CONFIG_PATH

TSX_CONFIG_PATH=tsconfig.different.json node --import yourTransformer --import tsx myFile.ts

I have this all working with node 22.3.0, ts-patch 3.2.1, typescript 5.5.3 and tsx 4.16.2.

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

2 participants