Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swc as an option to use with node/nestjs builder #8900

Closed
1 task
tonivj5 opened this issue Feb 8, 2022 · 39 comments
Closed
1 task

Add swc as an option to use with node/nestjs builder #8900

tonivj5 opened this issue Feb 8, 2022 · 39 comments
Labels
scope: node Issues related to Node, Express, NestJS support for Nx type: feature

Comments

@tonivj5
Copy link

tonivj5 commented Feb 8, 2022

Description

As swc-project/swc#3459 has fixed the use of legacy-decorators, it'd awesome to be able of use swc as compiler/bundler to reduce typescript compilation time 🚀

Motivation

Reduce time of tsc.

Suggested Implementation

swc is used with @nrwl/js and others, use it with @nrwl/node (nestjs) too

Alternate Implementations

esbuild doesn't support legacy decorators emitDecoratorMetadata 😢

@FrozenPandaz FrozenPandaz added the scope: node Issues related to Node, Express, NestJS support for Nx label Feb 14, 2022
@github-actions
Copy link

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 🙏

@github-actions github-actions bot added the stale label Oct 23, 2022
@tonivj5
Copy link
Author

tonivj5 commented Oct 23, 2022

Up

@kdawgwilk
Copy link
Contributor

Looks like its coming to NestCLI so it would be great for Nx to also provide it as an option https://twitter.com/kammysliwiec/status/1664192006019506178?s=20

@arivera-xealth
Copy link

bump

@drackp2m
Copy link

I think many of us would love to see this.

@AdhamMoussa
Copy link

Up

@kdawgwilk
Copy link
Contributor

NestJS CLI shipped this in v10 https://trilon.io/blog/nestjs-10-is-now-available#nestjs-%EF%B8%8F-swc

@ggagosh
Copy link

ggagosh commented Jul 11, 2023

Given that the latest version of Nx seems to be slowing down the NestJS development process, I was wondering if there are any plans to integrate SWC to potentially improve performance. 🤔

@michaeljauk
Copy link

Is there anyone willing to work on this?

@DavidFencl
Copy link

+1 on this. Adding support for SWC would be great for NestJs apps. I've managed to get SWC working with NestJs, but compilation fails on NX library imports.

@om-mahato
Copy link

+1

@thenglong
Copy link
Contributor

+1. This will make development experience so much better.

@itspers
Copy link

itspers commented Jul 29, 2023

Is it possible to use this nest start -b swc instead of default nx builder?
This nx has interesting curve: first when you start using it - it feels 'right', but then with every update more and more problems occur with angular, react, nest.. to the point where im now sure maintaining just 10 stanalone apps would be much simpler.

@mhdismail
Copy link

Up.

@dragomir-parvanov-riskmethods
Copy link

dragomir-parvanov-riskmethods commented Aug 10, 2023

@itspers

I thought I made it working with nest start -b swc, but when I tried to import an nx generated library, the paths were not compiled at all

@jacqueslareau
Copy link

NestJS so slow in dev since upgrade....

@mustafaersoyer
Copy link

+1

@Jeff909Dev
Copy link

Up

@DavidFencl
Copy link

BTW is there separate issue talking about slow NestJS watch mode? It's seriously annoying. My pretty small app takes 20-30 seconds to recompile every time something changes. It also takes a lot of time for deamon to even notice something changed. It used to be almost instant in previous NX versions. I can provide link to my repo if necessary.

@DobroslavR
Copy link

Same @DavidFencl

@ak2g
Copy link

ak2g commented Sep 20, 2023

☝️

@jurienhamaker
Copy link

🆙

@DobroslavR
Copy link

DobroslavR commented Sep 28, 2023

Can't believe this is not ready yet.

Seeing extremely slow compilation of Nest apps in newest NX versions makes me cry sometimes.

This would solve it pretty easily.

@viniciusLouzada
Copy link

🆙

@c0dewriter
Copy link

I got my NestJS working with SWC (p.s. I used npx create-nx-workspace@latest for workspace creation)

1. Install parts of SWC that you needed: $ npm install @swc/cli @swc/core @swc/jest

2. Reconfigure your project.json file (generated by NX) like this:

  "name": "nx-nest-boilerplate",
  "$schema": "node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/js:swc",   // <---- Important
      "outputs": [
        "{options.outputPath}"
      ],
      "defaultConfiguration": "production",
      "options": {
        "target": "node",
        "compiler": "swc",       // <---- Important
        "swcrc": ".swcrc",       // <---- Important
        "outputPath": "dist/nx-nest-boilerplate",
        "main": "src/main.ts",
        "tsConfig": "tsconfig.app.json",
        "assets": [
          "src/assets"
        ],
        "isolatedConfig": true
      },
      "configurations": {
        "development": {
          "swcrc": ".swcrc"      // <---- Your development swcrc file
        },
        "production": {
          "swcrc": ".swcrc"      // <---- Your production swcrc file
        }
      }
    },
    "serve": {
     ....

3. Create your .swcrc file:

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "module": {
    "type": "commonjs",       // <---- Important
    "strict": true,
    "strictMode": true
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    }
  },
  "minify": true
}

Here's my tsconfig.app.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "strictNullChecks": true,
    "strict": true,
    "outDir": "./dist/out-tsc",
    "module": "CommonJS",       // <---- Important
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,   // <---- Important (I think)
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "incremental": true,
    "types": ["node"],
    "target": "ES2021"
  },
  "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
  "include": ["src/**/*.ts"]
}

And here's my environment:

"dependencies": {
    "@nestjs/common": "^10.0.2",
    "@nestjs/core": "^10.0.2",
    "@nestjs/platform-express": "^10.0.2",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.92",
    "@swc/jest": "^0.2.29",
    "axios": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.0",
    "tslib": "^2.3.0"
},

"devDependencies": {
    "@nestjs/schematics": "^10.0.1",
    "@nestjs/testing": "^10.0.2",
    "@nx/eslint-plugin": "16.10.0",
    "@nx/jest": "16.10.0",
    "@nx/js": "16.10.0",
    "@nx/linter": "16.10.0",
    "@nx/nest": "16.10.0",
    "@nx/node": "16.10.0",
    "@nx/webpack": "16.10.0",
    "@nx/workspace": "16.10.0",
    "@types/jest": "^29.4.0",
    "@types/node": "~18.7.1",
    "@typescript-eslint/eslint-plugin": "^5.60.1",
    "@typescript-eslint/parser": "^5.60.1",
    "eslint": "~8.46.0",
    "eslint-config-prettier": "8.1.0",
    "jest": "^29.4.1",
    "jest-environment-node": "^29.4.1",
    "nx": "16.10.0",
    "nx-cloud": "latest",
    "prettier": "^2.6.2",
    "ts-jest": "^29.1.0",
    "ts-node": "10.9.1",
    "typescript": "~5.1.3"
}

@DobroslavR
Copy link

@c0dewriter can you make public repo with this?

@wilson208
Copy link

{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "module": {
    "type": "commonjs",       // <---- Important
    "strict": true,
    "strictMode": true
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    }
  },
  "minify": true
}

@c0dewriter thanks for these snippets. I can compile a new standalone NestJS app in NX, however imports from libs aren't working for me with this error at build time

 File 'libs/nest-stripe/index.ts' is not under 'rootDir' 'apps/api'. 'rootDir' is expected to contain all source files

@pf1gura
Copy link

pf1gura commented Oct 9, 2023

This is the main issue here and probably why it takes so long. Executor do more stuff than just wrapping underlying tech (like swc). It also has to rewrite your imports depending on the command you run. serve has to read all your sources directly (most likely TypeScript files) from packages/libs directory. In build mode it uses compiled source code from dist. In order to do that it needs to create dynamic configuration files depending on the command and do few other things to wire everything.

If you want all the nice things that nx gives you for monorepo management you have to accept that you won't always be able to use latest and greatest from the framework that nx wraps.

@wilson208
Copy link

wilson208 commented Oct 9, 2023

@pf1gura thanks for your insight. Given the solution above uses the existing '@nx/js:swc' executor I made an assumption that this executor would already handle imports from libs. @c0dewriter posted a potential solution, I tried it and gave feedback, I'm just keen for a solution because my NestJS build times have been very slow after the release of v10 and the nx monorepo I work with day to day has 6 different NestJS applications and the DX isn't great right now.

@pf1gura
Copy link

pf1gura commented Oct 9, 2023

If I recall correctly existing swc executor is for libraries only. You can use it in your shared TypeScript libraries to speed up library build times but final build of your application still has to go through node executor (which does not use swc)

@quesurifn
Copy link

@wilson208 I'm actually seeing the same rootDir bug. Have you found a fix yet?

@mandarini
Copy link
Member

mandarini commented Oct 25, 2023

@quesurifn @wilson208 and anyone else, is it possible that your build target names (of two interdependent libs) are different? This bug can appear if you are importing one buildable lib into the other, and the build target names are different.

related: #11289

For anyone having that error, solution: #17798 (comment)

@ggagosh
Copy link

ggagosh commented Nov 18, 2023

@quesurifn @wilson208 and anyone else, is it possible that your build target names (of two interdependent libs) are different? This bug can appear if you are importing one buildable lib into the other, and the build target names are different.

related: #11289

For anyone having that error, solution: #17798 (comment)

I'm currently working on integrating SWC into my NestJS application using NX. In my setup, I have shared libraries that are non-buildable, so the solution you mentioned doesn't seem applicable in my case.

Is there an alternative approach or workaround that could make this integration possible?

@leosvelperez
Copy link
Member

There are many things being discussed in this thread. Let's try to keep it focused on the original request (supporting swc).

Anyone with the 'rootDir' is expected to contain all source files error, please see #11289 (comment). You can follow the conversation over there.

@quesurifn
Copy link

@mandarini Indeed that was the issue. I encountered this with TS now. For some reason TS didn't trigger the same error until I did a complete refactor but I indeed did have circular dependencies that were two buildable libs referencing each other. The fix was to put them into one lib and then break them up with ts paths so semantically, at least, there is some differentiation.

@quesurifn
Copy link

quesurifn commented Nov 29, 2023

@ggagosh I would try looking even if they are not buildable. Also if you're using TS they are indeed buildable. I fixed mine from putting libs that imported each other into the same lib.

@kdawgwilk
Copy link
Contributor

I also used the https://github.com/pahen/madge tool to help find some circular deps issues that was blocking swc from being able to compile my projects

@SahilMahadwar
Copy link

+1

@sasha-flow
Copy link

SWC support for NestJS apps is still required

@nrwl nrwl locked and limited conversation to collaborators Apr 2, 2024
@FrozenPandaz FrozenPandaz converted this issue into discussion #22632 Apr 2, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
scope: node Issues related to Node, Express, NestJS support for Nx type: feature
Projects
None yet
Development

No branches or pull requests