dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.16k stars 794 forks source link

Hot reload with --reloadHandler runs very slowly and provides no CLI feedback when it is complete #1782

Closed ps2-controller closed 2 months ago

ps2-controller commented 2 months ago

Bug Report

Current Behavior

Serverless offline hot reload is

Sample Code

Here's a repository with a minimum viable example to recreate the issue. The README has instructions to reproduce the issue. (https://github.com/ps2-controller/serverless-offline-issue)

org: angara94dev
service: serverless-offline-issue

useDotenv: true
provider:
  name: aws
  runtime: nodejs16.x
  stage: 'dev'
  region: us-east-1
  profile: default
  environment:
  iam:
package:
  individually: true
  excludeDevDependencies: true
  exclude:
    - node_modules/aws-sdk/** # Exclude AWS SDK as it's already available in the Lambda environment
    - '**/*.js.map'
custom:
  serverless-offline:
  webpack:    
    webpackConfig: './webpack.config.cjs'   
    packager: 'npm'    
    includeModules:      
      forceExclude:        
        - aws-sdk
plugins:
  - serverless-webpack
  - serverless-offline

functions:
  ping:
    name: ${self:service}-${self:provider.stage}-ping
    handler: src/ping/ping.router
    timeout: 30
    events:
      - http:
          path: 'v0/ping'
          method: any
          cors:
            origins:
              - '*'  
            headers:
              - '*'  
resources:
'use strict'
import 'dotenv/config'
// Require and init API router module
import createApi, { NextFunction, Request, Response } from 'lambda-api';
import { APIGatewayProxyEvent, Context, Callback } from 'aws-lambda';
const app = createApi({ version: 'v0.0', base: 'v0' });

//----------------------------------------------------------------------------//
// Define Middleware
//----------------------------------------------------------------------------//

  // Add CORS Middleware
  app.use((req: Request, res: Response, next: NextFunction) => {
    next();
  });

  // Add Authorization Middleware
  app.use((req: Request, res: Response, next: NextFunction) => {
    next();
  });

//----------------------------------------------------------------------------//
// Build API routes
//----------------------------------------------------------------------------//

  app.post('/ping', (req: Request,res: Response) => {
    // Send the response

    console.log('RUN - 1')

    res.status(200).json({
      status: 'ok',
      message: 'ping successful',
      version: req.version,
      auth: req.auth,
      body: req.body,
      query: req.query
    })
  })

  app.options('/*', (req: Request,res: Response) => {
    res.status(200).json({})
  })

  //----------------------------------------------------------------------------//
  // Main router handler
  //----------------------------------------------------------------------------//
  export const router = (event: APIGatewayProxyEvent, context: Context, callback: Callback) => {
    // Set this flag to false, otherwise, the lambda function won't quit until all DB connections are closed
    context.callbackWaitsForEmptyEventLoop = false;

    // Run the request
    app.run(event, context, callback);
  };

Expected behavior/code

Environment

  "dependencies": {
    "aws-lambda": "^1.0.7",
    "lambda-api": "^1.0.3",
    "serverless-dotenv-plugin": "^6.0.0",
    "serverless-webpack": "^5.13.0"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.24.5",
    "@babel/preset-typescript": "^7.24.1",
    "@types/aws-lambda": "^8.10.137",
    "babel-loader": "^9.1.3",
    "fork-ts-checker-webpack-plugin": "^9.0.2",
    "serverless-offline": "^13.5.0",
    "ts-loader": "^9.5.1",
    "tsconfig-paths-webpack-plugin": "^4.1.0"
  }

Additional context/Screenshots Using serverless-webpack and typescript

ps2-controller commented 2 months ago

Additional experimentation indicates that this may be related to serverless-webpack plugin, not serverless offline. I've updated the sample repo.

I'm not 100% sure which of the two libraries is the real root cause, but happy to close this issue on request.

ps2-controller commented 2 months ago

I've left a note on an issue in serverless-webpack as well, which is possibly a more relevant place, but that issue has been open since 2020 with an awaiting reply tag since 2022

https://github.com/serverless-heaven/serverless-webpack/issues/566#issuecomment-2096726107

ps2-controller commented 2 months ago

At this stage, I've confirmed that the issue is with serverless-webpack. serverless-offline is working as expected. Closing the issue unless something new comes up.