npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pinnsg/serverless-esbuild-layers

v0.2.0

Published

[![Test Coverage](https://api.codeclimate.com/v1/badges/e5f4215f1f3f63aad0eb/test_coverage)](https://codeclimate.com/github/beforeyoubid/serverless-esbuild-layers/test_coverage) [![Maintainability](https://api.codeclimate.com/v1/badges/e5f4215f1f3f63aad0e

Downloads

3

Readme

serverless-esbuild-layers

Test Coverage Maintainability

Plugin for the Serverless framework that allows you to leverage Lambda layers to separate your node modules into layers attached to relevant functions.

This helps to keep the overall function size down.

This library is designed to be used in conjuction with serverless-esbuild to build your code.

If you are using serverless-webpack, you can try serverless-webpack-layers to achieve this functionality.

Diagram

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-externals

Setup

Once installed, you need to add this to your serverless plugins:

plugins:
  - serverless-esbuild
  - serverless-esbuild-layers

You also need to configure serverless-esbuild to externalise all node modules:

custom:
  esbuild:
    plugins: esbuild-plugins.js
    packages: 'external'  

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

// esbuild-plugins.js
const { nodeExternalsPlugin } = require('esbuild-node-externals');

module.exports = [nodeExternalsPlugin()];

For more complex configurations, you may also use a full javascript configuration file:

  • serverless.yml:
  esbuild:
    config: './esbuild.config.cjs'
  • esbuild.config.cjs:
const { nodeExternalsPlugin } = require('esbuild-node-externals')

module.exports = (serverless) => {
  return {
    package: 'external'
    platform: 'node',
    bundle: true,
    minify: false,
    sourcemap: false,
    treeShaking: true,
    plugins: [
      nodeExternalsPlugin({
        "allowWorkspaces": true
      }),
    ]
  }
}

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/layer'
    name: my-modules
    description: node_modules
    compatibleRuntimes:
      - nodejs20.x

Then 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-library

Package.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: ../