Open cspotcode opened 3 years ago
Hi @cspotcode, that's great to hear!
It'd be great to add ts-node
integration to this package. If you look at https://github.com/avajs/typescript/pull/12, perhaps we could have a compile: 'tsc' | 'ts-node'
option?
What would be your ideal integration for both CJS and ESM outputs?
Interesting, what is the motivation for #12? Is it to leverage incremental compilation or to support project references? Or is it to make sure typechecking output behaves identically to tsc
rather than worrying that ts-node
will output slightly different diagnostics?
If ava launches workers with --loader ts-node/esm
and with TS_NODE_PROJECT
pointing to the right tsconfig.json
, then I think ts-node
will handle both CommonJS and ESM transpilation. So that could be our ideal integration.
However, to truly match tsc
, ts-node
needs to make a few behavioral changes:
a) add support for incremental compilation and project references. I have some tickets and have done some of the research to get this done
b) Encourage or make --files
the default so that we don't mess with "files"
and "include"
arrays. Defaulting it has been requested on the TypeScript Discord many times. Without it can be faster in some situations but also causes confusion.
c) Have an option to emit all diagnostics up-front instead of filtering per-file. Currently, we throw
TS errors for a particular file when it is require()
d. This matches node's behavior of throw
ing for SyntaxErrors, so it kinda fits the philosophy of marrying TS and node, but this makes our error reporting different than tsc
Interesting, what is the motivation for #12?
For me personally, I run tsc
as a separate process. #12 would allow that compilation to take place when you invoke AVA itself.
If ava launches workers with
--loader ts-node/esm
and withTS_NODE_PROJECT
pointing to the righttsconfig.json
, then I thinkts-node
will handle both CommonJS and ESM transpilation. So that could be our ideal integration.
Sounds like one feature we need is the ability to configure a loader. Do you know if this can be done programmatically?
Presumably setting the environment variable programmatically is fine, as long as that's done before any loader code is loaded?
Have an option to emit all diagnostics up-front instead of filtering per-file. Currently, we
throw
TS errors for a particular file when it isrequire()
d. This matches node's behavior ofthrow
ing for SyntaxErrors, so it kinda fits the philosophy of marrying TS and node, but this makes our error reporting different thantsc
This current behavior is more suited to our use case here though. One open question I have with #12 is where those diagnostics go when they're emitted during a test run.
I can pick this up once I'm done with #12 :heart:
where those diagnostics go when they're emitted during a test run.
https://nodejs.org/api/cli.html#cli_experimental_loader_module
Sounds like one feature we need is the ability to configure a loader. Do you know if this can be done programmatically?
I'm not 100% sure, but I doubt this :( I think we'll have to pass the --loader
then.
Correct, today it cannot be done programmatically. I believe that node should add this feature, and hopefully pressure from projects like ours will encourage them to do so.
Another wrinkle is that, today, --loader ts-node/esm
runs in the main thread, so we can automatically install our CommonJS loader at the same time. On modern node, --loader ts-node/esm
is enough to enable both ESM and CommonJS hooks.
However, node is investigating moving the ESM loaders into another thread. When they do that, it may or may not break some workflows.
Just something to be aware of. It's one of the reasons I keep warning users that native ESM loader stuff is experimental.
I've been revamping ts-node's documentation. It's still very much a work-in-progress, but I've added a "Recipes" section inspired by yours and added a page for Ava.
https://typestrong.org/ts-node/docs/recipes/ava
I realize this duplicates information across Ava's and ts-node's documentation, but I figure it's better for users. Let me know if you have any corrections.
We could add a provider feature in AVA itself, so that, from here, you could configure a Node.js argument for the child process / worker thread that the tests are run in.
Given the highly experimental nature we could name this compile
option experimental-ts-node
?
I've been revamping ts-node's documentation. It's still very much a work-in-progress, but I've added a "Recipes" section inspired by yours and added a page for Ava.
That's great!
Is there a recipe for using swc
+ ES modules?
@vjpr You want to combine ts-node's swc
mode with the ESM loader?
I came across https://github.com/avajs/ava/issues/1109 which lists several criticisms of ts-node and predates some recent improvements. For example, ts-node has since implemented an ESM loader which I believe can handle #5.
20 -- source-map-support -- is enabled out-of-the-box in ts-node.
We allow all configuration to be placed in your tsconfig.json. And we have an
swc
integration that will be in the next release, allowing users to opt-in to much faster compilation than we can get with babel or typescript.All that to say, if you have time, I would love to talk about any other bugs , limitations, or inconveniences that your users hit with ts-node. At the very least it will give me some useful data points for improving ts-node. A big goal of mine is to make TS and node tooling feel cleaner, easier, and require less boilerplate.
If you have any thoughts, please let me know.