nx-plugins icon indicating copy to clipboard operation
nx-plugins copied to clipboard

[FEAT] Nx-Serverless | option to exclude ALL dependencies from build

Open AtlasRW opened this issue 3 years ago • 1 comments

Hi everyone, and first of all, thanks for the hard work you're putting in this project !

Feature Request's related problem

I need to be able to exclude ALL node_modules from the build/deploy result .zip file, in order to keep functions as lightweight as possible (less than 1mb) while using npm libraries like pg for database queries among others, with any of those being around 500kb each, at least.

My solution is to put them into an AWS Layer containing all my runtime dependencies, so any function would only contain the code it needs, but there is no way to exclude them properly, as far as I know.

Combined with the fact that every function's build contains all the service's functions without being able to build every function truly independently as already mentioned #81 and the other fact that when I use one of my /libs the whole library is included even with the import { A, B, C } from '@project/lib' syntax, my functions are becoming more and more bulky with time, even a 5 lines function.

The problem with /libs is for another issue, so my focus here is about excluding ALL npm dependencies if needed, not just devDependencies.

Desired Solution

Either an option for builders/executors with a list of glob patterns to forceExclude so one could write something like node_modules/*, but the most straightforward and most simple option would be a property in serverless.yml or an option for builder/executor to exclude all dependencies, including runtime.

This would need to be explicit as this behaviour in serverless-webpack is to prevent runtime errors, but as a grown person, one could take their own risks if they explicitly ask for it, with maybe a warning during the build to prevent about the possible issues, just in case.

In order for the function in Lambda to recognise the libraries like casual node_modules directly from the layer, maybe it must be included in the function's package.json, meaning this option for excluding all dependencies would mean no yarn install, or just avoiding the node_modules while zipping.

Considered Alternatives

  • Tried to put all my dependencies as devDependencies but I get this error from serverless-webpack
ERROR: Runtime dependency 'pg' found in devDependencies. Move it to dependencies or use forceExclude to explicitly exclude it.
  • By trying to forceExclude as recommended by serverless-webpack, my runtime dependencies are silently included in the final build anyway. Even if it worked as expected, one should write every module I use explicitly for every service, which would be a real pain in the a**.

Affected Providers :

[X] AWS [?] Azure [?] Google Cloud Platform

Affected Frameworks :

[?] Angular [?] Nodejs [X] Serverless [X] Lambda [?] Infrastructure as a code

Additional Context

This option would probably cause problems with serverless-offline as it is based on the same build target. If this hypothetical option (an excludeNodeModules boolean for this example) is put in the build target options, it would be possible to base the serve and deploy on different build configurations :

{
  "build": {
    "executor": "@flowaccount/nx-serverless:build",
    "configurations": {
      "dev-offline": {
        "excludeNodeModules": false
      },
      "production-offline": {
        "excludeNodeModules": false
      },
      "production-no-deps": {
        "excludeNodeModules": true
      }
    }
  },
  "serve": {
    "executor": "@flowaccount/nx-serverless:offline",
    "configurations": {
      "dev": {
        "buildTarget": "myApp:build:dev-offline"
      },
      "production": {
        "buildTarget": "myApp:build:production-offline"
      }
    }
  },
  "deploy": {
    "executor": "@flowaccount/nx-serverless:deploy",
    "options": {
      "buildTarget": "myApp:build:production-no-deps"
    }
  }
}

AtlasRW avatar Mar 14 '22 00:03 AtlasRW

Must precise that I found that this issue may be related to #48 Sorry about that

AtlasRW avatar Mar 14 '22 01:03 AtlasRW