Skip to content

(aws-lambda-nodejs): support additional esbuild configurations(main-fields) #17768

@AllanZhengYP

Description

@AllanZhengYP

Description

Currently only a subset of esbuild configs are exposed to users via BundlerOptions:

export interface BundlingOptions {

However there are much more configs supported by esbuild: https://esbuild.github.io/api/

A good example is main-fields: by default esbuild will prefer main entry of package.json for Node.js app. However, this is usually the entry point for CommonJS module, thus, esbuild cannot treeshake it well. If we specify the esbuild to prefer the module entry point, the function bundle can be reduced more with tree shaking.

Use Case

If main-fields is specified for AWS SDK v3, the SDK size can be further reduced by more than 50% with tree shake enabled.

Proposed Solution

Even though this request is asking main-fields config explicitly, CDK can also expose interface for arbitrary esbuild config. Just like buildArgs for the image building step:

readonly buildArgs?: { [key:string] : string };

There are 2 alternatives for it:

  1. Expose additional options in BundlingOptions like { esbuildConfigs: { [key: string]: string } }. This alternative may requires validating the duplicated options with other existing esbuild configs.
  2. Expose a path option for users to provide custom esbuild.js file, which calls the esbuild JS API. This will ignore all other esbuild configs. The advantage is that it supports all the future esbuild configs.

Other information

No response

Acknowledge

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions