floydspace / serverless-esbuild

💨 A Serverless framework plugin to bundle JavaScript and TypeScript with extremely fast esbuild
MIT License
451 stars 139 forks source link

Dependency Injection not working when using serverless-esbuild plugin #260

Open svetlasyrimis opened 2 years ago

svetlasyrimis commented 2 years ago

Hello,

I've encountered that weirdly dependency injection breaks(Nest.js project) when attempting to use serverless-esbuild in local environment. However when attempting to use serverless-webpack it's all working fine.

serverless-offline@8.3.1 serverless-esbuild@1.23.3 serverless@2.72.2 serverless-webpack@5.6.0

custom:
  serverless-offline:
    httpPort: 4000
    lambdaPort: 4001
  esbuild:
    bundle: true
    minify: false
    platform: 'node'
    external:
      - "cache-manager"
      - "class-transformer"
      - "class-validator"
      - "@nestjs/microservices"
      - "@nestjs/websockets/socket-module"
      - "fastify-swagger"
    watch:
      pattern: ['src/**/*.ts']
floydspace commented 2 years ago

Het @svetlasyrimis, thanks for raising it. Does it work in the deployed environment? Could you also try set all nestjs packages as external

svetlasyrimis commented 2 years ago

Thanks for your prompt reply, will try that.

Using custom esbuild script to deploy, not sls.

vinicius91carvalho commented 2 years ago

I used @anatine/esbuild-decorators plugin as this example here: https://docs.serverless-stack.com/constructs/Function, and it solved the problem.

svetlasyrimis commented 2 years ago

Thank you @vinicius91carvalho , will try that. So far still not working.

svetlasyrimis commented 2 years ago

Update: Using esbuild-plugin-ts-decorators and plugins option in serverless.yaml actually made it work. Thanks for the help @vinicius91carvalho :)

Bnic3 commented 1 year ago

Update: Using esbuild-plugin-ts-decorators and plugins option in serverless.yaml actually made it work. Thanks for the help @vinicius91carvalho :)

@svetlasyrimis Am i missing something? how do you use esbuild-plugin-ts-decorators in the serverless.ts file

senghuotlay commented 7 months ago

This is what she meant by

serverless.yml

  esbuild:
    plugins: esbuild-plugins.js
    bundle: true
    minify: true
    sourcemap: true
    external:
      - 'class-transformer'
      - 'class-validator'
      - '@nestjs/microservices'
      - '@nestjs/websockets/socket-module'

      - 'cache-manager'
      - 'fastify-swagger'

esbuild-plugins.js

/* eslint-disable @typescript-eslint/no-var-requires */
const { esbuildDecorators } = require('esbuild-plugin-typescript-decorators');

module.exports = () => {
  return [esbuildDecorators()];
};