2

I'm using esbuild to package my lambdas functions. However, when generating the build of them to perform the deployment, I get an alert that the package is too big, as in the image below.

File to big

1

I found in the documentation a way to remove certain packages with the --external: flag. However, I have many lambdas, and for each lambda I would have to add this flag in my build script whenever there is a new devDependencie package. I would like to know if there is a more practical and easier way to solve this?

build script:

for function in $(ls functions/typescript); do
  esbuild functions/typescript/$function/index.ts --platform=node --bundle --minify --external:@types/aws-lambda --external:@types/aws-sdk --external:@types/node-forge --external:@types/pem --external:aws-sdk --outfile=functions/__compiled__/$function/index.js
done

P.S: remembering that not all lambdas will need these devDependencies packages

Edit1:

package.json

{
    "name": "clinicSettings",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT",
    "dependencies": {
        "aws-lambda": "^1.0.7",
        "lambda-utils": "private-repo",
        "node-forge": "^1.3.1",
        "pem": "^1.14.6",
        "squel": "^5.13.0"
    },
    "devDependencies": {
        "@types/aws-lambda": "^8.10.101",
        "@types/aws-sdk": "^2.7.0",
        "@types/node-forge": "^1.0.4",
        "@types/pem": "^1.9.6",
        "aws-sdk": "^2.1177.0"
    }
}
vimuth
  • 5,064
  • 33
  • 79
  • 116
  • Welcome to SO! Would you please [edit] your question and copy-paste the `package.json` dependencies and devDependencies? – lepsch Aug 03 '22 at 05:58

1 Answers1

1

I think you can already exclude all @types as they should be automatically remove by the TypeScript transpiler. Then, there should be not much left, mainly the AWS SDK that is good practice to exclude from the bundle, like you did. So ultimately, most of the time you will only need to do --external:@aws-sdk/* or --external:aws-sdk, depending which SDK and node version you are using. There might be few other case to exclude, for example if you use postgres --external:pg-native (you will need to find on your own).

Concerning --platform=node I am not sure that you need it, but this you should double check or try out. So far I don't use it and seem to work.

Alexandre
  • 3,088
  • 3
  • 34
  • 53