Nest-cli: esbuild support

Created on 19 May 2020  路  14Comments  路  Source: nestjs/nest-cli

Feature Request

Add support for esbuild as the build system.

Is your feature request related to a problem? Please describe.

Building modules on large project takes sizeable amount of time, which makes debugging a bit slow. esbuild can potentially increase the build speed up to 86 times.

Describe the solution you'd like

Include esbuild into the list of supported build systems.

Teachability, Documentation, Adoption, Migration Strategy

esbuild repo

What is the motivation / use case for changing the behavior?

Speed. Speed. Speed.

feature needs triage

Most helpful comment

I think swc does work with nestjs now. Using the @swc/cli @swc/core packages and this .swcrc configuration

{
    "jsc": {
      "loose": true,
      "target": "es2020",
      "parser": {
        "syntax": "typescript",
        "decorators": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      }
    },
    "module": {
        "type": "commonjs",
        "strict": true,
        "strictMode": true,
        "lazy": true,
        "noInterop": true
    }
  }
  ```

  I run these commands and everything works fine. See this [PR](https://github.com/swc-project/swc/pull/939)

"build": "swc src -s -d ./dist",
"start": "pnpm build && node dist/main.js",
```

All 14 comments

This looks pretty impressive, but a major problem is that it does not support decorators, which would kind of go against how Nest does all of its DI. I'm definitely going to keep an eye on the library though. @kamilmysliwiec if this ever gets to a point where it _could_ support Nest, what would you think?

Separate from that, decorators in TypeScript have been around for a while behind the experimentalDecorators flag, and have found a lot of adoption in the web development community. I think esbuild would be useful to many more people if it had support for TypeScript-style decorators. So I'm planning to implement those at some point.

https://github.com/evanw/esbuild/issues/104#issuecomment-627645920

It seems that TS decorators might be supported at some point. Let's circle back when it's implemented. For now, esbuild isn't compatible with Nest

This looks pretty impressive, but a major problem is that it does not support decorators, which would kind of go against how Nest does all of its DI. I'm definitely going to keep an eye on the library though. @kamilmysliwiec if this ever gets to a point where it _could_ support Nest, what would you think?

have you tried @swc/core? they support decorators
https://swc-project.github.io

This looks pretty impressive, but a major problem is that it does not support decorators, which would kind of go against how Nest does all of its DI. I'm definitely going to keep an eye on the library though. @kamilmysliwiec if this ever gets to a point where it _could_ support Nest, what would you think?

have you tried @swc/core? they support decorators
https://swc-project.github.io

SWC has an issue with decorators
https://github.com/swc-project/swc/issues/666

An initial implementation of TypeScript decorators has been released in version 0.4.10. You can read more about this in the release notes. Please let me know if you encounter any issues.

https://github.com/evanw/esbuild/issues/104#issuecomment-640559073

An initial implementation of TypeScript decorators has been released in version 0.4.10. You can read more about this in the release notes. Please let me know if you encounter any issues.

evanw/esbuild#104 (comment)

This still doesn't support emitDecoratorMetadata, which Nest makes use of, so I don't think it can be used yet. Also, it doesn't support commonjs unless working with bundles which also becomes a problem due to Nest's optional require statements

I think swc does work with nestjs now. Using the @swc/cli @swc/core packages and this .swcrc configuration

{
    "jsc": {
      "loose": true,
      "target": "es2020",
      "parser": {
        "syntax": "typescript",
        "decorators": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      }
    },
    "module": {
        "type": "commonjs",
        "strict": true,
        "strictMode": true,
        "lazy": true,
        "noInterop": true
    }
  }
  ```

  I run these commands and everything works fine. See this [PR](https://github.com/swc-project/swc/pull/939)

"build": "swc src -s -d ./dist",
"start": "pnpm build && node dist/main.js",
```

Using either swc and esbuild with Nest should be doable. The biggest issue with these compilers is a lack of type-checking which is one of the major benefits of TS. We currently don't plan to swap tsc with either one, but you should be able to safely use them in your projects.

Can we use swc or esbuild in dev server only? but still, use tsc in production build?
In dev, we can rely on IDEs to do type checks. This will gain a lot DX from fast rebuild.

Sure, just depends on the scripts you set up and where you run the production build. You can absolutely use swc or esbuild in dev (though I think esbuild still doesn't emit metadata) and let typescript be your CI/CD compiler. Scripts lead to versatility, right? All about customization.

@dethereum Could you please tell me how to use swc with nestjs monorepo?

For those who find this Issue, I created an example with esbuild and swc for local development
https://github.com/techvlad/nestjs-build
@axe-me @abrvsk

@techvlad that esbuild plugin you use is using tsc to compile ts file with decorators.

@axe-me I'm using https://github.com/anatine/esbuildnx/tree/main/packages/esbuild-decorators plugin, this plugin doesn't use tsc

Was this page helpful?
0 / 5 - 0 ratings