DEV Community

Amir Alam
Amir Alam

Posted on

Revolutionize Your Nest.js Compilation Time: Boosting Speed by 20x with SWC!

Are you tired of waiting ages for your Nest.js applications to compile? Say goodbye to sluggish build times and embrace a lightning-fast development experience with SWC. In this guide, we'll explore how you can supercharge your Nest.js compilation process and achieve a jaw-dropping 20x speed improvement. Buckle up and get ready to witness a remarkable transformation in your development workflow as we unlock the immense power of SWC. Say hello to faster builds and increased productivity, all thanks to this game-changing tool. Let's dive in and revolutionize your Nest.js compilation time like never before!

Existing Compiler

Nest.js utilize the TypeScript compiler (tsc) as the default compiler in standard mode and webpack in monorepo mode. However, the tsc compiler is known to thave relatively slower compilation speed. In comparison, SWC, a Rust-based tool, offers a faster alternative for compiling and bundling Typescript and javascript.

Getting started

After generating a new Nest.js project using the Nest CLI, the project is set up to use the default TypeScript compiler (tsc). However, to leverage the benefits of SWC, let's migrate it.

Step 1

Install Required dependencies

  • @swc/core : SWC core compiler

npm --save-dev @swc/core

  • @swc/cli : CLI to interact with swc core

npm --save-dev @swc/cli

  • nodemon : for keeping process alive and watching files for changes

npm --save-dev nodemon

  • concurrently : for running commands concurrently

npm --save-dev concurrently

Step 2

Setting up swc config

create a .swcrc file in your roor directory and paste this configuration

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "module": {
    "type": "commonjs"
  },
  "jsc": {
    "target": "es2017",
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "keepClassNames": true,
    "baseUrl": "./"
  },
  "minify": false
}
Enter fullscreen mode Exit fullscreen mode

Step 3

Now we have setup the SWC, let's test it

Compile Nest js

npx swc --out-dir dist -w src

this command will watch the files and immediately compile it to dist folder,
Run Nest js

node dist/main.js

Now you should see the nest js app running.

Step 4

In the previous step, we successfully ran our new project with SWC compilation. However, writing the long command every time can become cumbersome. To simplify this process, we can shorten the command by adding it as a script in the package.json file.

Add this to root package.json

 "scripts": {
    "build:swc": "npx swc --out-dir dist -w src",
    "start:swc": "nodemon dist/main",
    "dev": "concurrently   \"npm run build:swc\" \"npm run start:swc\" ",
  }
Enter fullscreen mode Exit fullscreen mode

Now test it by writing

npm run dev

.
and you should see your app running quickly.

End Note

Note: SWC isn't compatible with NestJS CLI plugins.
If you're using a plugin, you should stick to tsc or webpack (nest build and nest start).

Top comments (2)

Collapse
 
sayedabutahir profile image
Sayedabutahir • Edited

@mrsharpp , might be a typo or something. Below are the corrected commands where ( i or install ) Flag was missing from Step 1 for installing the required dependencies in one go:

Install Required Dependencies

  • @swc/core: SWC core compiler
  • @swc/cli: CLI to interact with SWC core
  • nodemon: For keeping the process alive and watching files for changes
  • concurrently: For running commands concurrently

Anyone can install all of these in one command like so:

npm i --save-dev @swc/core @swc/cli nodemon concurrently
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zoltanr profile image
Zoltan Rakottyai

Thanks for sharing!
I was immediately able to update my build script and reduce my overall build times, from a 8s to a blazing .6s. I'm impressed. CI feedback time improved which is a huge win for me.

I had a struggle with setting up to start script with nodemon.
For some reason it stuck and got in a loop if I remove the --watch dist part from the below script.

"start:swc:prod": "bun run build:swc && nodemon -L --verbose --watch dist dist/main.js",