fastify / fastify-autoload

Require all plugins in a directory
MIT License
333 stars 67 forks source link

Support loading tsx files. #388

Closed Markyiptw closed 5 months ago

Markyiptw commented 5 months ago

Prerequisites

🚀 Feature Proposal

Currently the plugin only register route / plugins written in .js / .ts files, but not those in .jsx and .tsx, I hope them to be supported as well.

Motivation

With fastify-html-plugin, one can write a fastify app with jsx syntax.

With a tsc setup, a "include": ["src/**/*"] line in tsconfig.json would compile .tsx files to .js files, so the the compiled app can run normally.

However in a testing environment, the files are not compiled, but instead being run with ts-node (at least that's the configuration from fastify-cli's typescript template), and the plugin won't register route files ending with tsx, causing tests failed with route not found error.

Example

I'm not sure whether tsx file should be hardcoded to be loaded or an extra option should be provided.

jean-michelet commented 5 months ago

As I understand it, the main issue is related to test environment. So maybe we should just add support for tsx files that will be JIT compiled by ts-node.

But I need confirmation from other maintainers.

If they agree, can you send a PR with unit tests? Perhaps you should also document the behavior in the documentation.

jean-michelet commented 5 months ago

@climba03003

Can you take a look plz?

jean-michelet commented 5 months ago

Accepted scripts are here: https://github.com/fastify/fastify-autoload/blob/aded7b5a43059590fa89fcc1b945f5ec15e69d92/index.js#L12

But the issue is that it should work with @swc/register (deprecated but we still support it for now), @swc-node/register and others. You can see the tests in the scripts folder.

climba03003 commented 5 months ago

One can always change the pattern, although it is not documented.

I don't think we should support tsx or jsx by default because we have no mechanism to transpile those file. You should change the options and fit your needs.

Markyiptw commented 5 months ago

Setting scriptPattern works for me so I'm closing the issue. Thank you for the quick response!