1.52.1 • Published 2 months ago

serverless-esbuild v1.52.1

Weekly downloads
3,101
License
MIT
Repository
github
Last release
2 months ago

💨 serverless-esbuild

Serverless Framework plugin for zero-config JavaScript and TypeScript code bundling using promising fast & furious esbuild bundler and minifier

serverless npm version npm downloads build status semantic-release

Features

  • Zero-config: Works out of the box without the need to install any additional plugins
  • Works with Typescript and Javascript projects
  • Supports sls package, sls deploy, sls deploy function
  • Integrates with Serverless Invoke Local & serverless-offline

Table of Contents

Install

# install `serverless-esbuild` and `esbuild`
yarn add --dev serverless-esbuild esbuild
# or
npm install -D serverless-esbuild esbuild
# or
pnpm install -D serverless-esbuild esbuild

Add the following plugin to your serverless.yml:

plugins:
  - serverless-esbuild

Configuration

By default, no configuration is required, but you can override the default behavior via the custom.esbuild section in the serverless.yml file.

custom:
  esbuild:
    bundle: true
    minify: false

Examples

See example folder for some example configurations.

Options

OptionDescriptionDefault
Esbuild OptionsThis plugin can take almost any Esbuild Javascript Build Option.Default Esbuild Options
concurrencyThe number of concurrent bundle operations to run at once. eg. 8. NOTE: This can be memory intensive and could produce slower builds.Infinity
zipConcurrencyThe number of concurrent zip operations to run at once. eg. 8. NOTE: This can be memory intensive and could produce slower builds.Infinity
excludeAn array of dependencies to exclude from the Lambda. This is passed to the esbuild external option. Set to * to disable packaging node_modules['aws-sdk']
installExtraArgsOptional arguments passed to npm or yarn for external dependency resolution. eg. ['--legacy-peer-deps'] for npm v7+ to use legacy peerDependency resolution behavior[]
keepOutputDirectoryKeeps the .esbuild output folder. Useful for debugging.false
nativeZipUses the system's zip executable to create archives. NOTE: This will produce non-deterministic archives which causes a Serverless deployment update on every deploy.false
outputBuildFolderThe output folder for Esbuild builds within the work folder. You will also need to manually override the watch ignore config if used.'.build'
outputWorkFolderThe output folder for this plugin where all the bundle preparation is done. You will also need to manually override the watch ignore config if used.'.esbuild'
outputFileExtensionThe file extension used for the bundled output file. This will override the esbuild outExtension option'.js'
packagePathPath to the package.json file for external dependency resolution.'./package.json'
packagerPackager to use for external dependency resolution. Values: npm, yarn, pnpm'npm'
packagerOptionsExtra options for packagers for external dependency resolution.Packager Options
watchWatch options for serverless-offline.Watch Options
skipBuildAvoid rebuilding lambda artifacts in favor of reusing previous build artifacts.false
skipRebuildA boolean defining whether rebuild is avoided. Generally rebuild produces faster builds but in some context scenarios with many lambdas or low memory computer (like Github Actions) it can cause memory leaks.false
skipBuildExcludeFnsAn array of lambda names that will always be rebuilt if skipBuild is set to true and bundling individually. This is helpful for dynamically generated functions like serverless-plugin-warmup.[]
stripEntryResolveExtensionsA boolean that determines if entrypoints using custom file extensions provided in the resolveExtensions ESbuild setting should be stripped of their custom extension upon packing the final bundle for that file. Example: myLambda.custom.ts would result in myLambda.js instead of myLambda.custom.js.
disposeContextAn option to disable the disposal of the context.(Functions can override the global disposeContext configuration by specifying their own disposeContext option in their individual configurations.)true

Default Esbuild Options

The following esbuild options are automatically set.

OptionDefaultNotes
bundletrueEsbuild requires this for use with external
entryPointsN/ACannot be overridden
outDirN/ACannot be overridden
platform'node'Set format to esm to enable ESM support
target'node16'We dynamically set this. See Supported Runtimes
watchN/ACannot be overridden

Packager Options

OptionDescriptionDefault
scriptsA string or array of scripts to be executed, currently only supports 'scripts' for npm, pnpm and yarnundefined
noInstallYarn only A boolean that deactivates the install stepfalse
ignoreLockfileYarn only A boolean to bypass lockfile validation, typically paired with external dependencies because we generate a new package.json with only the externalized dependencies.false

Watch Options

OptionDescriptionDefault
patternAn anymatch-compatible definition for the watcher to respond to./**/*.(js\|ts) (watches all .js and .ts files)
ignoreAn anymatch-compatible definition for the watcher to ignore['.esbuild', 'dist', 'node_modules', '.build']
chokidarAny Chokidar option{ ignoreInitial: true }

Function Options

OptionDescriptionDefault
skipEsbuildSet this property to true on a function definition to skip esbuildundefined

Supported Runtimes

This plugin will automatically set the esbuild target for the following supported Serverless runtimes:

AWS

RuntimeTarget
nodejs20.xnode20
nodejs18.xnode18
nodejs16.xnode16
nodejs14.xnode14
nodejs12.xnode12

Google

This plugin is compatible with the serverless-google-cloudfunctions plugin, and will set the runtimes accordingly.

RuntimeTarget
nodejs20node20
nodejs18node18
nodejs16node16
nodejs14node14
nodejs12node12

Azure

This plugin is compatible with the serverless-azure-functions plugin, and will set the runtimes accordingly.

RuntimeTarget
nodejs18node18
nodejs16node16
nodejs14node14
nodejs12node12

Please Note When using this package in conjunction with the serverless-azure-functions plugin, the following additional configuration is required to ensure function apps are built correctly:

package:
	patterns: ["host.json", "**/function.json"],

Non-Node functions

If you wish to use this plugin alongside non Node functions like Python or functions with images, this plugin will automatically ignore any function which does not contain a handler or use a supported Node.js runtime.

Note: If you are using Python functions with Serverless Offline you will need to change the outputWorkFolder and outputBuildFolder to folder names without fullstops.

Advanced Configuration

Config file

Esbuild configuration can be defined by a config file.

custom:
  esbuild:
    config: './esbuild.config.js'
// esbuild.config.js
module.exports = (serverless) => ({
  external: ['lodash'],
  plugins: [],
});

Including Extra Files

Serverless Package Configuration will behave in the same way as native packaging. You can use patterns, include and exclude to include extra files into your bundles.

External Dependencies

Packages that are marked as external and exist in the package.json's dependencies will be installed and included with your build under node_modules. You can customize this with a number of options.

custom:
  esbuild:
    external:
      - lodash
    packager: yarn
    packagePath: absolute/path/to/package.json
    packagerOptions:
      scripts:
        - echo 'Hello World!'
        - rm -rf node_modules
    installExtraArgs:
      - '--legacy-peer-deps'

To easily mark all the dependencies in package.json as external, you can utilize esbuild-node-externals plugin.

To mark one or more individual packages as external, use the following configuration:

custom:
  esbuild:
    external:
      - 'my-package-name'
      - 'another-package-name'

Esbuild Plugins

Note: The Esbuild plugins API is still experimental

You can configure esbuild plugins by passing a plugins' configuration file:

custom:
  esbuild:
    plugins: plugins.js

The plugins' configuration file must be a javascript file exporting an array of plugins (see examples/individually/plugins.js for a dummy plugin example):

let myPlugin = {
  name: 'my-plugin',
  setup(build) {
    // plugin implementation
  },
};

// default export should be an array of plugins
module.exports = [myPlugin];

or a function that accepts serverless instance and returns an array of plugins (see issue #168 for an example):

module.exports = (serverless) => {
  const myPlugin = {
    name: 'my-plugin',
    setup(build) {
      // plugin implementation with `serverless` instance access
      console.log('sls custom options', serverless.service.custom);
    },
  };

  // an array of plugins must be returned
  return [myPlugin];
};

Usage

Automatic compilation

As long as the plugin is properly installed, all regular Serverless operations sls package, sls deploy, sls deploy function, sls invoke local, sls offline will automatically compile using serverless-esbuild.

Serverless Offline

The plugin integrates very well with serverless-offline to simulate AWS Lambda and AWS API Gateway locally.

Add the plugins to your serverless.yml file and make sure that serverless-esbuild precedes serverless-offline as the order is important:

plugins: ...
  - serverless-esbuild
  ...
  - serverless-offline
  ...

Run serverless offline or serverless offline start to start the Lambda/API simulation.

In comparison to serverless offline, the start command will fire an init and a end lifecycle hook which is needed for serverless-offline and e.g. serverless-dynamodb-local to switch off resources (see below)

Automatic compilation is available while using the plugin with serverless-offline.

custom:
  esbuild:
    watch:
      pattern: ['src/**/*.ts'] # match only typescript files in src directory
      ignore: ['temp/**/*']

Note: When overriding the ignore pattern, remember to ignore .build directory to avoid endless compilation.

Serverless Dynamodb Local

Configure your service the same as mentioned above, but additionally add the serverless-dynamodb-local plugin as follows:

plugins:
  - serverless-esbuild
  - serverless-dynamodb-local
  - serverless-offline

Run serverless offline start.

Invoke Local

This plugin supports the Serverless Invoke Local functionality and will automatically compile the selected function.

External Tools

Contributors

Inspired by serverless-plugin-typescript and serverless-webpack

1.52.1

2 months ago

1.52.0

2 months ago

1.51.0

3 months ago

1.50.2

3 months ago

1.50.1

5 months ago

1.49.0

6 months ago

1.50.0

5 months ago

1.48.2

8 months ago

1.48.1

8 months ago

1.48.4

7 months ago

1.48.3

8 months ago

1.48.5

7 months ago

1.48.0

8 months ago

1.45.1

11 months ago

1.45.0

12 months ago

1.46.0

10 months ago

1.47.1

8 months ago

1.47.0

8 months ago

1.37.3

1 year ago

1.37.1

1 year ago

1.37.2

1 year ago

1.40.0

1 year ago

1.44.0

12 months ago

1.38.0

1 year ago

1.41.0

1 year ago

1.39.0

1 year ago

1.42.0

1 year ago

1.43.1

1 year ago

1.43.0

1 year ago

1.35.1

1 year ago

1.37.0

1 year ago

1.35.2

1 year ago

1.33.1

1 year ago

1.35.0

1 year ago

1.33.2

1 year ago

1.35.3

1 year ago

1.36.0

1 year ago

1.34.0

1 year ago

1.32.9

2 years ago

1.33.0

2 years ago

1.29.0

2 years ago

1.32.0

2 years ago

1.32.1

2 years ago

1.30.0

2 years ago

1.32.4

2 years ago

1.32.5

2 years ago

1.32.2

2 years ago

1.32.3

2 years ago

1.32.8

2 years ago

1.32.6

2 years ago

1.32.7

2 years ago

1.28.1

2 years ago

1.28.0

2 years ago

1.31.1

2 years ago

1.31.0

2 years ago

1.27.0

2 years ago

1.27.1

2 years ago

1.26.0

2 years ago

1.26.1

2 years ago

1.26.2

2 years ago

1.25.0

2 years ago

1.23.2

2 years ago

1.23.3

2 years ago

1.23.0

2 years ago

1.23.1

2 years ago

1.23.4

2 years ago

1.23.5

2 years ago

1.24.1

2 years ago

1.24.2

2 years ago

1.22.1

2 years ago

1.24.0

2 years ago

1.22.2

2 years ago

1.21.2

2 years ago

1.22.0

2 years ago

1.21.0

3 years ago

1.21.1

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.3

3 years ago

1.18.2

3 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.17.1

3 years ago

1.17.0

3 years ago

1.16.2

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.15.0

3 years ago

1.15.1

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.10.8

3 years ago

1.11.0

3 years ago

1.10.5

3 years ago

1.10.4

3 years ago

1.10.3

3 years ago

1.10.7

3 years ago

1.10.6

3 years ago

1.10.2

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago