juanjoDiaz / serverless-middleware

Serverless plugin to allow middleware handlers configured directly in serverless.yaml
MIT License
18 stars 11 forks source link

Cannot do dynamic imports when using a middleware #37

Closed justinasit closed 2 years ago

justinasit commented 2 years ago

Hi,

I've encountered an issue today when using "serverless-middleware". The plugin works well in general, but I cannot seem to do any dynamic imports in my app using "import()".

The problem is that the path in the compiled app changes to "/.middleware", but the imports are file in the level below. Here is an example error:

GET /accounts/me (λ: getAccount)
× Cannot find module './8.js'
  Require stack:
  - /usr/src/app/services/accounts/.webpack/service/.middleware/accounts-api-local-getAccount.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/index.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/handler-runner/HandlerRunner.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/handler-runner/index.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/LambdaFunction.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/LambdaFunctionPool.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/Lambda.js
  - /usr/src/app/node_modules/serverless-offline/dist/lambda/index.js
  - /usr/src/app/node_modules/serverless-offline/dist/ServerlessOffline.js
  - /usr/src/app/node_modules/serverless-offline/dist/index.js
  - /usr/src/app/node_modules/serverless-offline/dist/main.js
  - /usr/src/app/node_modules/serverless/lib/utils/import-module.js
  - /usr/src/app/node_modules/serverless/lib/classes/plugin-manager.js
  - /usr/src/app/node_modules/serverless/lib/serverless.js
  - /usr/src/app/node_modules/serverless/scripts/serverless.js
  - /usr/src/app/node_modules/serverless/bin/serverless.js

In this case the "8.js" would be in "/usr/src/app/services/accounts/.webpack/service/" and not in "/usr/src/app/services/accounts/.webpack/service/.middleware/".

Is there any known work around to this?

Thank you.

juanjoDiaz commented 2 years ago

Hi @justinasit,

Sorry for the delay. This issue totally missed my radar.

Did you solve your issue?

This library works by creating a wrapper function that requires all your middlewares and chain them. It does not move around your files so your references should be valid.

This might be an issue between the webpack and the middleware plugin.

justinasit commented 2 years ago

Hi @juanjoDiaz,

No worries, unfortunately I did not solve it! Ended up changing all the dynamic imports to static ones as they were small files anyways, so not a huge issue.

Have you or anyone else managed to successfully use dynamic imports with serverless-middleware? We're using "serverless-bundle" v4.4.0 to do all the webpack magic. Maybe it has something to do with that.

bcrigler commented 2 years ago

I was able to do this successfully, although I did not use serverless-webpack, I used a combination of two plugins.

  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies

My full plugin list and order and the inclusion of the external dependancies:

 plugins:
  - serverless-plugin-typescript
  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies
  - serverless-middleware
  - serverless-offline
package:
  excludeDevDependencies: false
  patterns:
    - 'node_modules/@srd/app-common-apis/middleware/**' # adds my custom node module as an external dependency of the build
    - 'node_modules/@srd/**/middleware/**'