You can easily speed up our TypeScript environment with Fastify + TypeScript + NodeJs using swc. swc is a TypeScript compiler (Plus more..) written in Rust. And it's very very fast. Like 200ms it takes to start in my case.
Of course, you still need tsc to do a typescript build with all the bells & whistles that check on types. But for fast development, SWC can be very useful. I think the biggest down-side is when Bun you can't get rid of Bun. While using Fastify, that can be run on various runtimes. I hope you get my point.
In this example above I will use `swc` command to compile the src directory, outputs into the dist directory. I also don't want the leading path (eg. /src) but you can remove that flag if you wish. Then I just run node, which will execute the "main" from package.json (aka dist/index.js file created by SWC...).
In your `tsconfig.json` file you can configure also ts-node if you wish to use SWC with ts-node with nodemon or something like that:
Comment
You can easily speed up our TypeScript environment with Fastify + TypeScript + NodeJs using swc. swc is a TypeScript compiler (Plus more..) written in Rust. And it's very very fast. Like 200ms it takes to start in my case.
Of course, you still need tsc to do a typescript build with all the bells & whistles that check on types. But for fast development, SWC can be very useful. I think the biggest down-side is when Bun you can't get rid of Bun. While using Fastify, that can be run on various runtimes. I hope you get my point.
Replies
"our TypeScript environment"?
I've never actually understood how SWC works. There was talk about it being enabled by default in Next.js but Next.js is slow.
"using Fastify, that can be run on various runtimes"
Yeah, that's awesome!
Actually is quite easy. You first need to add SWC (core + cli) to your project: `npm i -D @swc/cli @swc/core`
Add an new `.swcrc` to your root folder, with the content:
```json
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": false,
"dynamicImport": false
},
"target": "esnext"
},
"module": {
"type": "es6"
},
"sourceMaps": true
}
```
Changing your package.json to use swc could be as simple as:
```json
"main": "dist/index.js",
"scripts": {
"start": "swc ./src -d dist --strip-leading-paths && node .",
....
....
}
```
In this example above I will use `swc` command to compile the src directory, outputs into the dist directory. I also don't want the leading path (eg. /src) but you can remove that flag if you wish.
Then I just run node, which will execute the "main" from package.json (aka dist/index.js file created by SWC...).
In your `tsconfig.json` file you can configure also ts-node if you wish to use SWC with ts-node with nodemon or something like that:
```
"ts-node": {
"files": true,
"swc": true,
"esm": true,
},
```
More info: https://swc.rs/docs/getting-started. And yes you're right NextJS also has support for it: https://docs.nestjs.com/recipes/swc