DEV Community

Amir Alam
Amir Alam

Posted on

3

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).

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay