nodejs / amaro

Node.js TypeScript wrapper
MIT License
271 stars 14 forks source link

Create a TSX loader #102

Closed marco-ippolito closed 2 weeks ago

AugustinMauroy commented 2 weeks ago

We will need to use .swcrc or tsconfig.json

marco-ippolito commented 2 weeks ago

why? we dont use config so far, its just a matter of stripping types out of tsx and output a valid jsx

robpalme commented 2 weeks ago

I don't understand the scope of this issue.

A TSX loader that strips types and outputs JSX seems fine as a standalone library feature. But it would only be useful in Node as a loader if it were chained so that the output can then goes through a JSX loader to make it executable by Node.

Is that the intended use-case?

marco-ippolito commented 2 weeks ago

I mean its not intended for core usage rather than for amaro as standalone loader

AugustinMauroy commented 2 weeks ago

why? we dont use config so far, its just a matter of stripping types out of tsx and output a valid jsx

You'll agree with me that if you're importing a TSX component, you need to import something ‘javascript frindly’. So to represent a jsx element the transpiler, whatever it is, must transform this jsx into an object. This is why it needs ‘rules’. These are either put in the tsconfig.json or the .swcrc. These rules allow the transpiler to know that a given jsx element is worth that in a js object. Because JSX is not only used by React.

So the loader must know how to transpile the TSX/JSX

@JakobJingleheimer Can you confirm? It was you who originally created the tsx loader REF: https://github.com/JakobJingleheimer/nodejs-loaders/blob/main/tsx.mjs

marco-ippolito commented 2 weeks ago

Support for config file goes against the spirit of the typescript support in Node. The tsx loader should output jsx that another loader will consume

AugustinMauroy commented 2 weeks ago

I understand. So can amaro offer two loaders TSX2JSX and JSX2JS? And JSX2JS would not necessarily be used in node core but that users can use themselves.

marco-ippolito commented 2 weeks ago

This would not be used in node core. The utility I was thinking would be to be able to run something like next.js in dev mode. Im not 100% convince though about it

AugustinMauroy commented 2 weeks ago

I use Jackob's for unit testing react components. It's the only use I've found at the moment. Because doing SSR with node and a loader would be horrible from a performance point of view.

The utility I was thinking would be to be able to run something like next.js in dev mode.

They use SWC through webpack/turbopack so idk if they need a loader (we can still ask them).

AugustinMauroy commented 2 weeks ago

And if people want to have a tsx loader based on SWC there is https://github.com/swc-project/swc-node/tree/master/packages/register#readme . So if node core doesn't intend to support tsx I think we can close this issue as not planned.

JakobJingleheimer commented 1 week ago

@JakobJingleheimer Can you confirm?

Is the question about whether a config file is required? That depends on the transpiler. For example, esbuild configuration is required in order to enable j|tsx handling; BUT we could pass those here (ex if file extension is .jsx or .tsx) because which (for esbuild) is used doesn't matter for the purposes of just getting it to run (ex for component tests).

We'll be publishing the above cited loader probably later this week.