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

Software development Company

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 @swc/cli

*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
} 

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

    "build:swc": "npx swc --out-dir dist -w src",
    "start:swc": "nodemon dist/main",
    "dev": "concurrently   \"npm run build:swc\" \"npm run start:swc\" ",
  } 

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

Software development Company
Amir Alam

Hello, I hope you are enjoying reading blog post. Experience a groundbreaking transformation in your Nest.js development workflow as you revolutionize compilation time. Prepare to be amazed as SWC takes the stage, boosting your speed by an incredible 20x. Witness the magic unfold as you effortlessly build and deploy with unrivaled efficiency. Embrace the future of Nest.js development and embrace the true power of SWC. Thank you for your time.

agile software development company

Contact Us

We’re looking forward to hear from you! Let’s make the next big product together.

Software Development Company in Aligarh

India

B3, HK Compound, NH 509, Sikandarpur, Chherat, Uttar Pradesh 202002

Phone +91-9045-708-272

Email: [email protected]

Software Development Company in UK

UK

16 Maryatt Avenue, London, U.K.

+44-7342-8931-85

[email protected]

Software Development Company in Riyadh

Riyadh

AlSulymaniah - Prince Mamdouh Street AlSafwa Building, Gate 1

+966-5959-6363-5

[email protected]

Sofyrus Technologies Company
Startup India
Good Firms
MicroSoft for Startup
ISO

© 2019-2023 Sofyrus Technologies | All Right Reserved |