5.1.0 • Published 2 days ago

@mrgrain/cdk-esbuild v5.1.0

Weekly downloads
154
License
MIT
Repository
github
Last release
2 days ago

CDK constructs for esbuild, an extremely fast JavaScript bundler

Getting started | Documentation | API Reference | Python, .NET, & Go | FAQ

View on Construct Hub

Why?

esbuild is an extremely fast bundler and minifier for TypeScript and JavaScript. This package makes esbuild available to deploy AWS Lambda Functions, static websites or publish assets for further usage.

AWS CDK supports esbuild for AWS Lambda Functions, but the implementation cannot be used with other Constructs and doesn't expose all of esbuild's API.

Getting started

Install cdk-esbuild for Node.js using your favorite package manager:

# npm 
npm install @mrgrain/cdk-esbuild@5
# Yarn
yarn add @mrgrain/cdk-esbuild@5
# pnpm
pnpm add @mrgrain/cdk-esbuild@5

For Python, .NET or Go, use these commands:

# Python
pip install mrgrain.cdk-esbuild

# .NET
dotnet add package Mrgrain.CdkEsbuild

# Go
go get github.com/mrgrain/cdk-esbuild-go/cdkesbuild/v5

AWS Lambda: Serverless function

💡 See Lambda (TypeScript) and Lambda (Python) for working examples of a how to deploy an AWS Lambda Function.

Use TypeScriptCode as the code of a lambda function:

const bundledCode = new TypeScriptCode("src/handler.ts");

const fn = new lambda.Function(stack, "MyFunction", {
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: "index.handler",
  code: bundledCode,
});

AWS S3: Static Website

💡 See React App (TypeScript) for a working example of a how to deploy a React app to S3.

Use TypeScriptSource as one of the sources of a static website deployment:

const websiteBundle = new TypeScriptSource("src/index.tsx");

const websiteBucket = new s3.Bucket(stack, "WebsiteBucket", {
  autoDeleteObjects: true,
  publicReadAccess: true,
  removalPolicy: cdk.RemovalPolicy.DESTROY,
  websiteIndexDocument: "index.html",
});

new s3deploy.BucketDeployment(stack, "DeployWebsite", {
  destinationBucket: websiteBucket,
  sources: [websiteBundle],
});

Amazon CloudWatch Synthetics: Canary monitoring

💡 See Monitored Website (TypeScript) for a working example of a deployed and monitored website.

Synthetics runs a canary to produce traffic to an application for monitoring purposes. Use TypeScriptCode as the code of a Canary test:

const bundledCode = new TypeScriptCode("src/canary.ts", {
  buildOptions: {
    outdir: "nodejs/node_modules", // This is required by AWS Synthetics
  },
});

const canary = new synthetics.Canary(stack, "MyCanary", {
  runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_5_1,
  test: synthetics.Test.custom({
    code: bundledCode,
    handler: "index.handler",
  }),
});

Documentation

The package exports constructs for use with AWS CDK features. The guiding design principal of this package is "extend, don't replace". Expect constructs that you can provide as props, not complete replacements.

For use with Lambda Functions and Synthetic Canaries, implementing lambda.Code (reference) and synthetics.Code (reference):

  • TypeScriptCode

Inline code is only supported by Lambda:

  • InlineTypeScriptCode

For use with S3 bucket deployments, implementing s3deploy.ISource (reference):

  • TypeScriptSource

Code and Source constructs seamlessly plug-in to other high-level CDK constructs. They share the same set of parameters, props and build options.

The following classes power the other features. You normally won't have to use them, but they are there if you need them:

  • TypeScriptAsset implements s3.Asset (reference) \ creates an asset uploaded to S3 which can be referenced by other constructs

  • EsbuildBundler implements core.BundlingOptions (reference) \ provides an interface for a esbuild bundler wherever needed

  • EsbuildProvider implements IBuildProvider and ITransformProvider \ provides the default esbuild API implementation and can be replaced with a custom implementation

API Reference

Auto-generated reference for Constructs, Classes and Structs. This information is also available as part of your IDE's code completion.

Python, .NET, Go

Esbuild requires a platform and architecture specific binary and currently has to be installed with a Node.js package manager like npm.

When using cdk-esbuild with Python, .NET or Go, the package will automatically detect local and global installations of the esbuild npm package. If none can be found, it will fall back to dynamically installing a copy into a temporary location. The exact algorithm of this mechanism must be treated as an implementation detail and should not be relied on. It can however be configured to a certain extent. See the examples below for more details.

This "best effort" approach makes it easy to get started. But is not always desirable, for example in environments with limited network access or when guaranteed repeatability of builds is a concern. You have several options to opt-out of this behavior.

Provide a controlled installation of esbuild

The first step is to install a version of esbuild that is controlled by you.

I strongly recommend to install esbuild as a local package. The easiest approach is to manage an additional Node.js project at the root of your AWS CDK project. esbuild can then be added to the package.json file and it is your responsibility to ensure required setup steps are taken in all environments like development machines and CI/CD systems.

Instead of installing the esbuild package in a local project, it can also be installed globally with npm install -g esbuild or a similar command. This approach might be preferred if a build container is prepared ahead of time, thus avoiding repeated package installation.

Change the automatic package detection

The second step is to make cdk-esbuild aware of your chosen install location. This step is optional, but it's a good idea to have the location or at least the method explicitly defined.

To do this, you can set the esbuildModulePath prop on a EsbuildProvider. Either pass a known, absolute or relative path as value, or use the EsbuildSource helper to set the detection method. Please refer to the EsbuildSource reference for a complete list of available methods.

// Use the standard Node.js algorithm to detect a locally installed package
new EsbuildProvider({
  esbuildModulePath: EsbuildSource.nodeJs(),
});

// Provide an explicit path
new EsbuildProvider({
  esbuildModulePath: '/home/user/node_modules/esbuild/lib/main.js',
});

As a no-code approach, the CDK_ESBUILD_MODULE_PATH environment variable can be set in the same way. An advantage of this is that the path can easily be changed for different systems. Setting the env variable can be used with any installation approach, but is typically paired with a global installation of the esbuild package. Note that esbuildModulePath takes precedence.

Override the default detection method

For an AWS CDK app with many instances of TypeScriptCode etc. it would be annoying to change the above for every single one of them. Luckily, the default can be changed for all usages per app:

const customModule = new EsbuildProvider({
  esbuildModulePath: EsbuildSource.globalPaths(),
});
EsbuildProvider.overrideDefaultProvider(customModule);

Customizing the Esbuild API

This package uses the esbuild JavaScript API. In most situations the default API configuration will be suitable. But sometimes it is required to configure esbuild differently or even provide a custom implementation. Common reasons for this are:

  • Using a pre-installed version of esbuild with Python, .NET or Go
  • If features not supported by the synchronous API are required, e.g. support for plugins
  • If the default version constraints for esbuild are not suitable
  • To use a version of esbuild that is installed by any other means than npm, including Docker

For these scenarios, this package offers customization options and an interface to provide a custom implementation:

declare const myCustomBuildProvider: IBuildProvider;

new TypeScriptCode("src/handler.ts", {
  buildProvider: myCustomBuildProvider,
});


declare const myCustomTransformProvider: ITransformProvider;

new InlineTypeScriptCode("let x: number = 1", {
  transformProvider: myCustomTransformProvider,
});

Esbuild binary path

It is possible to override the binary used by esbuild by setting a property on EsbuildProvider. This is the same as setting the ESBUILD_BINARY_PATH environment variable. Defining the esbuildBinaryPath prop takes precedence.

const buildProvider = new EsbuildProvider({
  esbuildBinaryPath: "path/to/esbuild/binary",
});

// This will use a different esbuild binary
new TypeScriptCode("src/handler.ts", { buildProvider });

Esbuild module path

The Node.js module discovery algorithm will normally be used to find the esbuild package. It can be useful to use specify a different module path, for example if a globally installed package should be used instead of a local version.

const buildProvider = new EsbuildProvider({
  esbuildModulePath: "/home/user/node_modules/esbuild/lib/main.js",
});

// This will use a different esbuild module
new TypeScriptCode("src/handler.ts", { buildProvider });

Alternatively supported by setting the CDK_ESBUILD_MODULE_PATH environment variable, which will apply to all uses. Defining the esbuildModulePath prop takes precedence.

If you are a Python, .NET or Go user, refer to the language specific guide for a more detailed explanation of this feature.

Custom Build and Transform API implementations

💡 See esbuild plugins w/ TypeScript for a working example of a custom Build API implementation.

A custom implementation can be provided by implementing IBuildProvider or ITransformProvider:

class CustomEsbuild implements IBuildProvider, ITransformProvider {
    buildSync(options: BuildOptions): void {
      // custom implementation goes here
    }
  
    transformSync(code: string, options?: TransformOptions): string {
      // custom implementation goes here, return transformed code
      return 'transformed code';
    }
}

// These will use the custom implementation
new TypeScriptCode("src/handler.ts", {
  buildProvider: new CustomEsbuild(),
});
new InlineTypeScriptCode("let x: number = 1", {
  transformProvider: new CustomEsbuild(),
});

Instead of esbuild, the custom methods will be invoked with all computed options. Custom implementations can amend, change or discard any of the options.

The IBuildProvider integration with CDK relies on the outdir/outfile values and it's usually required to use them unchanged.

ITransformProvider must return the transformed code as a string.

Failures and warnings should be printed to stderr and thrown as the respective esbuild error.

Overriding the default implementation providers

The default implementation can also be set for all usages of TypeScriptCode etc. in an AWS CDK app. You can change the default for both APIs at once or set a different implementation for each of them.

const myCustomEsbuildProvider = new MyCustomEsbuildProvider();

EsbuildProvider.overrideDefaultProvider(myCustomEsbuildProvider);
EsbuildProvider.overrideDefaultBuildProvider(myCustomEsbuildProvider);
EsbuildProvider.overrideDefaultTransformationProvider(myCustomEsbuildProvider);

// This will use the custom provider without the need to define it as a prop
new TypeScriptCode("src/handler.ts");

Roadmap & Contributions

The project's roadmap is available on GitHub.

Please submit feature requests as issues to the repository. All contributions are welcome, no matter if they are for already planned or completely new features.

FAQ

Should I use this package in production?

This package is stable and ready to be used in production, as many do. However esbuild has not yet released a version 1.0.0 and its API is still in active development. Please read the guide on esbuild's production readiness.

Note that esbuild minor version upgrades are also introduced in minor versions of this package. Since esbuild is pre v1, these versions typically introduce breaking changes and this package will inherit them. To avoid this behavior, add the desired esbuild version as a dependency to your package.

How do I upgrade from cdk-esbuild v4?

Please refer to the v5 release notes for a list of backwards incompatible changes and respective upgrade instructions.

TS/JS Why am I getting the error Cannot find module 'esbuild'?

This package depends on esbuild as an optional dependencies. If optional dependencies are not installed automatically on your system (e.g. when using npm v4-6), install esbuild explicitly:

npm install esbuild

TS/JS How can I use a different version of esbuild?

Use the override instructions for your package manager to force a specific version, for example:

{
  "overrides": {
    "esbuild": "0.14.7"
  }
}

Build and Transform interfaces are relatively stable across esbuild versions. However if any incompatibilities occur, buildOptions / transformOptions can be cast to any:

const bundledCode = new TypeScriptCode("src/handler.ts", {
  buildOptions: {
    unsupportedOption: "value"
  } as any,
});

Python/.NET/Go How can I use a different version of esbuild?

Install the desired version of esbuild locally or globally as described in the documentation above.

Build and Transform interfaces are relatively stable across esbuild versions. However if any incompatibilities occur, use the appropriate language features to cast any incompatible buildOptions / transformOptions to the correct types.

Can I use this package in my published AWS CDK Construct?

It is possible to use cdk-esbuild in a published AWS CDK Construct library, but not recommended. Always prefer to ship a compiled .js file or even bundle a zip archive in your package. For AWS Lambda Functions, projen provides an excellent solution.

If you do end up consuming cdk-esbuild, you will have to set buildOptions.absWorkingDir. The easiest way to do this, is to resolve the path based on the directory name of the calling file:

// file: node_modules/construct-library/src/index.ts
const props = {
  buildOptions: {
    absWorkingDir: path.resolve(__dirname, ".."),
    // now: /user/local-app/node_modules/construct-library
  },
};

This will dynamically resolve to the correct path, wherever the package is installed. Please open an issue if you encounter any difficulties.

Can I use this package with AWS CDK v1?

Yes, v2 of cdk-esbuild is compatible with AWS CDK v1. You can find the documentation for it on the v2 branch.

Support for AWS CDK v1 and cdk-esbuild v2 has ended on June 1, 2023. Both packages are not receiving any updates or bug fixes, including for security related issues. You are strongly advised to upgrade to a supported version of these packages.

5.1.0

2 days ago

5.0.16

18 days ago

4.3.8

18 days ago

5.0.15

1 month ago

5.0.14

2 months ago

3.14.19

2 months ago

4.3.7

2 months ago

5.0.13

2 months ago

5.0.12

3 months ago

4.3.6

3 months ago

3.14.18

3 months ago

5.0.11

3 months ago

5.0.10

4 months ago

4.3.5

4 months ago

3.14.17

4 months ago

5.0.9

4 months ago

5.0.8

4 months ago

5.0.7

5 months ago

4.3.4

5 months ago

3.14.16

5 months ago

5.0.0-rc.0

8 months ago

5.0.6

5 months ago

5.0.5

6 months ago

5.0.4

6 months ago

5.0.3

7 months ago

5.0.2

7 months ago

5.0.1

8 months ago

5.0.0

8 months ago

4.3.2

7 months ago

4.3.1

8 months ago

4.3.3

6 months ago

4.3.0

8 months ago

3.14.11

8 months ago

3.14.14

7 months ago

3.14.15

6 months ago

3.14.10

8 months ago

4.2.3

8 months ago

4.2.2

9 months ago

4.2.4

8 months ago

4.2.1

9 months ago

4.2.0

9 months ago

4.1.10

10 months ago

4.1.11

10 months ago

4.1.12

9 months ago

4.1.13

9 months ago

4.1.8

12 months ago

4.1.7

1 year ago

4.1.9

11 months ago

3.14.6

1 year ago

4.1.4

1 year ago

4.1.3

1 year ago

4.1.6

1 year ago

4.1.5

1 year ago

3.14.3

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

3.13.2

1 year ago

3.13.1

1 year ago

3.13.4

1 year ago

3.13.3

1 year ago

3.13.5

1 year ago

4.0.0

1 year ago

3.14.1

1 year ago

3.14.0

1 year ago

3.14.2

1 year ago

3.13.0

1 year ago

3.11.6

1 year ago

4.0.0-beta.3

1 year ago

4.0.0-beta.2

1 year ago

4.0.0-beta.1

1 year ago

4.0.0-beta.0

1 year ago

4.1.0

1 year ago

3.11.5

1 year ago

3.11.4

1 year ago

3.11.3

2 years ago

3.11.0

2 years ago

3.11.2

2 years ago

3.11.1

2 years ago

4.0.0-alpha.7

2 years ago

4.0.0-alpha.8

2 years ago

4.0.0-alpha.5

2 years ago

4.0.0-alpha.6

2 years ago

4.0.0-alpha.3

2 years ago

4.0.0-alpha.4

2 years ago

4.0.0-alpha.1

2 years ago

3.7.2

2 years ago

4.0.0-alpha.2

2 years ago

4.0.0-alpha.0

2 years ago

3.6.0

2 years ago

3.9.0

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.8.1

2 years ago

3.7.0

2 years ago

3.4.0

2 years ago

3.5.0

2 years ago

3.3.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

2.2.0

2 years ago

2.0.0

2 years ago

3.0.0

2 years ago

1.132.0

2 years ago

1.130.0

2 years ago

2.1.0

2 years ago

3.0.0-rc.1

2 years ago

3.0.0-rc.0

2 years ago

1.131.0

2 years ago

1.133.0

2 years ago

2.0.0-rc.1

3 years ago

1.129.0

3 years ago

2.0.0-alpha.4

3 years ago

2.0.0-alpha.5

3 years ago

2.0.0-rc.0

3 years ago

2.0.0-alpha.0

3 years ago

2.0.0-alpha.1

3 years ago

2.0.0-alpha.2

3 years ago

1.128.0

3 years ago

1.126.0

3 years ago

1.127.0

3 years ago

0.0.0

3 years ago

1.126.0-beta.0

3 years ago

1.124.0

3 years ago

1.125.0

3 years ago

1.123.0

3 years ago

1.120.0

3 years ago

1.122.0

3 years ago

1.119.0

3 years ago

1.121.0

3 years ago

1.118.0

3 years ago

1.111.0

3 years ago

1.117.0

3 years ago

1.115.0

3 years ago

1.113.0

3 years ago

1.112.0

3 years ago

1.116.0

3 years ago

1.114.0

3 years ago

1.109.0

3 years ago

1.110.0

3 years ago

1.108.0

3 years ago

1.107.0

3 years ago

1.106.0

3 years ago

1.104.0

3 years ago

1.105.0

3 years ago

1.103.0

3 years ago

1.102.0

3 years ago

1.100.0

3 years ago

1.101.0

3 years ago

1.100.0-beta.1

3 years ago

1.100.0-beta.0

3 years ago

1.99.0

3 years ago

1.98.0

3 years ago

1.97.0

3 years ago

1.96.0

3 years ago

1.95.0

3 years ago

1.94.1

3 years ago

1.94.0

3 years ago

1.94.0-beta.0

3 years ago

1.93.1

3 years ago

1.93.0-beta1

3 years ago

1.93.0-beta.2

3 years ago

1.93.0

3 years ago

1.92.0

3 years ago

1.91.0

3 years ago

1.90.0

3 years ago

1.89.0

3 years ago

1.88.0

3 years ago

1.87.0

3 years ago

1.86.0

3 years ago

1.85.0

3 years ago

1.84.0

3 years ago

1.82.0

3 years ago

1.83.0

3 years ago

1.81.0

3 years ago

1.80.0

3 years ago

1.79.0

3 years ago

1.78.0-beta1

3 years ago

1.77.0-beta1

3 years ago

1.76.0-beta1

3 years ago

1.75.0-beta1

3 years ago

1.74.0-beta1

3 years ago

1.73.0-beta3

3 years ago

1.73.0-beta1

3 years ago

1.73.0-beta2

3 years ago

1.72.0-beta1

3 years ago