juanjoDiaz / serverless-middleware

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

Path of .middleware folder when searching for modules is broken with default folderName in Serverless 3.0 #42

Closed bcrigler closed 2 years ago

bcrigler commented 2 years ago

The path of the .middleware folder using the default configuration is broken in serverless 3.0 + serverless-middleware 3.0.

Framework Core: 3.22.0 (local) 3.21.0 (global)
Plugin: 6.2.2
SDK: 4.3.2

Error: Error: Cannot find module '/project-name/.build/.middleware/service-name-functionName'

Serverless for some reason is assuming that the /.middleware/ directory is located inside of /.build/.middleware/ and that is not the default location of the folder.

The pathToRoot variable resolves to just .. without a preceding slash, (../) and I thought that was the problem although manually altering the following did not fix it. Although, it does print out the preceding slash in the logs and doesn't seem to hurt anything. Not sure if not adding a preceding slash was intentional or not.

const pathToRoot = path.relative(pathFolder, this.serviceDir); - original const pathToRoot = path.relative(pathFolder, this.serviceDir) + '/'; - modified

If I find a fix, I'm happy to commit it but since you likely can pinpoint it faster, I'm posting this for visibility into the issue as well as for archival purposes.

bcrigler commented 2 years ago

I used a workaround that I thought of to ensure the .middleware/ folder gets added to the .build/ directory at runtime/build.

Although it doesn't work! The folder is there and I still get the same error.

My workaround that does not work!

package:
  excludeDevDependencies: false
  patterns:
    - '.middleware/**'
custom:
  middleware:
    cleanFolder: false

Turning cleanFolder to false, ensures the .middleware directory sticks around, and in combination with the pattern I added ensures it gets added to the .build directory although either way the same exact error occurs on serverless-offline. Also, this is the order of my plugins for context:

plugins:
  - serverless-plugin-typescript
  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies
  - serverless-middleware
  - serverless-offline
bcrigler commented 2 years ago

So, really we have two issues here:

  1. Path Issue: Whatever path you are providing to the .middleware directory to the serverlesss framework is incorrect. (I fixed this with my workaround)

  2. Transpilation Issue: The reason it didn't fix it is because all the handlers that were created in the .middleware directory were not transpiled from Typescript to Javascript. So, essentially the only way to use this plugin on the latest version of everything is to use serverless-webpack w/ webpack 5 and write up a custom webpack.config.js file that would transpile Typescript files to Javascript that the Serverless Framework wouldn't generally look for.

As of now I'm going down the path of a custom webpack file, even though it's a huge burden to do that. If I can't find a solution, I'll be forced to use currying versus this plugin.

juanjoDiaz commented 2 years ago

Hi @bcrigler ,

Thanks for the deep research on the issue:

Can you create a repo so I can reproduce the issue? Then it will be a lot easier for me to fix it.

The Transpilation Issue is by design. This plugin glue together all the middlewares into a single handler in whatever your original language was. Then it's up to you to use a separate plugin to do the transpilation.

So, the problem I think is the order in which you are using the plugin. Both the typescrypt and the middleware plugins mingle with paths and handlers, typescript creates .build and middleware creates .middleware, and that's probably causing the error. Changing the order so the middleware plugin runs before the typescript plugin probably fixes the issue.

plugins:
  - serverless-middleware
  - serverless-plugin-typescript
  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies
  - serverless-offline

If it doesn't work, can you provide a sample project so I can reproduce the issue and work on it?

juanjoDiaz commented 2 years ago

Closing since a solution was provided and there was no more response. Feel free to reopen if you think that something is left to solve.