Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AtlasRW opened this issue Mar 14, 2022 · 1 comment
Closed
Assignees
Labels
enhancement New feature or request

Comments

@AtlasRW
Copy link

AtlasRW commented Mar 14, 2022

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 AtlasRW added the enhancement New feature or request label Mar 14, 2022
@AtlasRW AtlasRW changed the title [FEAT] Exclude ALL dependencies from build [FEAT] Exclude ALL dependencies / node_modules from build Mar 14, 2022
@AtlasRW AtlasRW changed the title [FEAT] Exclude ALL dependencies / node_modules from build [FEAT] Nx-Serverless | forceExclude ALL dependencies from build Mar 14, 2022
@AtlasRW AtlasRW changed the title [FEAT] Nx-Serverless | forceExclude ALL dependencies from build [FEAT] Nx-Serverless | option to exclude ALL dependencies from build Mar 14, 2022
@AtlasRW
Copy link
Author

AtlasRW commented Mar 14, 2022

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

@AtlasRW AtlasRW closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants