serverless-esbuild-layers
v0.1.9
Published
[](https://codeclimate.com/github/beforeyoubid/serverless-esbuild-layers/test_coverage) [
Installation
yarn add --dev serverless-esbuild-layers serverless-esbuild esbuild-node-externals
npm install --save-dev serverless-esbuild-layers serverless-esbuild esbuild-node-externals
pnpm install --dev serverless-esbuild-layers serverless-esbuild esbuild-node-externalsSetup
Once installed, you need to add this to your serverless plugins:
plugins:
- serverless-esbuild
- serverless-esbuild-layersYou also need to configure serverless-esbuild to externalise all node modules:
custom:
esbuild:
plugins: esbuild-plugins.js
exclude:
- '*'the serverless-esbuild library supports custom plugins to configure esbuild. In order to leverage this library, you need to use esbuild-node-externals to externalise all node modules
const { nodeExternalsPlugin } = require('esbuild-node-externals');
module.exports = [nodeExternalsPlugin()];Adding layers
Once the plugin is configured in your serverless file, you need to add Layer definitions to your serverless file:
layers:
lib:
path: '.serverless'
name: my-modules
description: node_modules
compatibleRuntimes:
- nodejs14.xThen you can reference the relevant layers in each function (the ref should match NameLambdaLayer where name is the key of your layer with the first character uppercase)
functions:
test:
handler: handler.default
layers:
- { Ref: LibLambdaLayer }If you have multiple functions, it's recommended to add different layers depending on node module overlap. This library will identify which modules are needed by all the functions attached to each layer.
Config
This library can be configured by adding options into your serverless file:
custom:
esbuild-layers:
packager: 'auto' # can be specified as 'npm' | 'yarn' | 'pnpm';
forceExclude:
- some-library
forceInclude:
- some-other-libraryPackage.json pathing
If using a monorepo, you may need to specify a different package.json file. You can do so by using the packageJsonPath config variable like so:
custom:
esbuild-layers:
packageJsonPath: ../