serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

offline runs before webpack is ready

Open serusko opened this issue 4 years ago • 2 comments

This is a Bug Report

there was similar issue from 2017 but i cant find it anymore, and also I am not sure who is responsible for that but:

it looks like local development sls-offline executes TS file before webpack is ready and then switches to webpack output

reproducible demo: https://github.com/serusko/sls-problem-demo/blob/master/src/functions/hello/handler.ts#L10

Steps to reproduce:

  1. clone
  2. install deps by yarn install
  3. run yarn run sls offline

Symptomes:

you should see

------------------------------------------------------------------------------------------
/Users/%user%/Dev/Personal/test/src/functions/hello
-----------------------------------------------------------------------------------------

then hit url GET http://localhost:3000/dev/hello and you will see different path for exact same code

------------------------------------------------------------------------------------------
/Users/%user%/Dev/Personal/test/.webpack/service/src/functions/hello
-----------------------------------------------------------------------------------------

its pathname of handler function

Problem:

  • we are not able to use TS features like relative imports this https://github.com/serusko/sls-problem-demo/blob/master/src/functions/hello/handler.ts#L4 could be ...from '@libs/apiGateway'; but because TS its not processed by webpack for first run, it will throw error (you can change it to @libs... in watch mode and it will work same

  • if i want to use Decorator e.g. @Lambda() - i cant "autogenerate" lambda handler path using __dirname

OS: mac bigSur node: v14.15.4 yarn: 1.22.5

serusko avatar Feb 18 '21 11:02 serusko

the idea behind its to use decorators for DI and generating serverless functions section and also "automagic" swagger file like NestJs do, but it does not work if you try to import any working code in serverless.ts

decorator example:

/**
 * Lambda definition decorator
 * @param conf
 */
export function Lambda(conf: Record<string, any>): any {
  return (target) => {
    target.config = {
      ...conf,
      name: target.name,
      handler: `${__dirname.split(process.cwd())[1].substring(1)}/${target.name}.main`
  };
}

usage

@Lambda()
export class User {
  @Post()
  @SuccessResponse(UserSchema)
  createUser() ...

  @Put('/{id}')
  @StatusCode(202)
  updateUser() ... 

serusko avatar Feb 18 '21 11:02 serusko

hm i think its deadlock, bcs serverless.ts needs t be loaded by offline to know how to setup project,

any ideas please?

serusko avatar Feb 18 '21 11:02 serusko