Enigmatic-Smile / serverless-plugin-optimize

⛔️ DEPRECATED ⛔️ Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript
https://www.npmjs.com/package/serverless-plugin-optimize
MIT License
130 stars 54 forks source link

Usage with NestJS #109

Closed Will-Mann-16 closed 3 years ago

Will-Mann-16 commented 3 years ago

I'm trying to deploy a NestJS project to lambda, and I want to use this plugin to reduce the file sizes down so I can deploy without errors. At the moment I'm getting this error: `Error: Parsing file /Users/willmann/Documents/Programming/uni-haus/server/node_modules/fast-glob/out/readers/stream.js: Unexpected token (16:10) at Deps.parseDeps (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/serverless-plugin-optimize/node_modules/module-deps/index.js:519:15) at getDeps (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/serverless-plugin-optimize/node_modules/module-deps/index.js:447:44) at /Users/willmann/Documents/Programming/uni-haus/server/node_modules/serverless-plugin-optimize/node_modules/module-deps/index.js:430:38 at ConcatStream. (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/concat-stream/index.js:37:43) at ConcatStream.emit (events.js:326:22) at ConcatStream.EventEmitter.emit (domain.js:486:12) at finishMaybe (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/readable-stream/lib/_stream_writable.js:630:14) at endWritable (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/readable-stream/lib/_stream_writable.js:638:3) at ConcatStream.Writable.end (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/readable-stream/lib/_stream_writable.js:594:41) at DuplexWrapper.onend (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/readable-stream/lib/_stream_readable.js:577:10) at Object.onceWrapper (events.js:420:28) at DuplexWrapper.emit (events.js:326:22) at DuplexWrapper.EventEmitter.emit (domain.js:486:12) at endReadableNT (/Users/willmann/Documents/Programming/uni-haus/server/node_modules/readable-stream/lib/_stream_readable.js:1010:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) From previous event: at PluginManager.invoke (/Users/willmann/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:521:22) at /Users/willmann/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:556:24 From previous event: at PluginManager.run (/Users/willmann/.config/yarn/global/node_modules/serverless/lib/classes/PluginManager.js:556:8) at /Users/willmann/.config/yarn/global/node_modules/serverless/lib/Serverless.js:203:33 at processImmediate (internal/timers.js:458:21) From previous event: at Serverless.run (/Users/willmann/.config/yarn/global/node_modules/serverless/lib/Serverless.js:190:74) at /Users/willmann/.config/yarn/global/node_modules/serverless/scripts/serverless.js:50:26

Get Support -------------------------------------------- Docs: docs.serverless.com Bugs: github.com/serverless/serverless/issues Issues: forum.serverless.com

Your Environment Information --------------------------- Operating System: darwin Node Version: 14.8.0 Framework Version: 2.15.0 Plugin Version: 4.2.0 SDK Version: 2.3.2 Components Version: 3.4.3`

For context my severless.yml `service: name: uni-haus plugins:

package: individually: true include:

provider: name: aws runtime: nodejs12.x profile: uni-haus region: eu-west-2

functions: automated: handler: src/lambda-automated.handler # run 'nest build' before you deploy main: # The name of the lambda function handler: src/lambda.handler events:

custom: optimize:

external: ['fast-glob', 'fsevents']

`

I have tried this with different approaches - building using nestjs then deploying, same error. I've also tried excluding the modules responsible, but that just results in a different error of 'fast-glob module not found' on lambda, and 'cannot find module xxx' offline (xxx being my custom ts file containing a class type). Does anyone have any ideas? All help is greatly appreciated. (This project does work with the regular NestJS start, and I was following this tutorial)

andrew-talentfish commented 3 years ago

I'm experiencing the same issue.

andrew-talentfish commented 3 years ago

@nestjs/graphql uses a dependency, globby. The dependency globby uses a dependency fast-glob fast-glob#3.x is compiled into es2017 which breaks the parsing of serverless-plugin-optimize. In a nutshell, the fast-glob package needs to be compiled to es5. I did a quick/hacky fix that works. When I have time, I will create a more elegant solution. In your nestjs package.json, add the following to your dev-dependencies:

Will-Mann-16 commented 3 years ago

@nestjs/graphql uses a dependency, globby. The dependency globby uses a dependency fast-glob fast-glob#3.x is compiled into es2017 which breaks the parsing of serverless-plugin-optimize. In a nutshell, the fast-glob package needs to be compiled to es5. I did a quick/hacky fix that works. When I have time, I will create a more elegant solution. In your nestjs package.json, add the following to your dev-dependencies:

  • "fast-glob": "3.2.5",
  • "serverless-plugin-fix-optimize-bug": "https://github.com/andrew-talentfish/serverless-plugin-fix-optimize-bug.git"

You saved my bacon, thanks!

andrew-talentfish commented 3 years ago

@Will-Mann-16 For the later versions of nestjs/graphql, this fixes the fast-glob issue. However, there is another dependency, fsevents, where "optimize" tries to include a binary and fails. I think a better approach is to use external option as follows:

external: ['fsevents', 'fast-glob']

I was struggling getting this to work as other errors appeared. If this doesn't work for you, baseline your project using https://github.com/hardyscc/aws-nestjs-starter. It doesn't use the optimize plugin, but I'm able to get the app under 45MB with the latest and greatest libs including typeorm, graphql, etc... Deploys no problem to serverless.