Skip to content

Client packages can't be tree-shaken properly #6889

@spaceemotion

Description

@spaceemotion

Checkboxes for prior research

Describe the bug

I noticed that our lambda bundle contains pretty much all S3 and DynamoDB commands, even though I recently refactored to use the smaller, non-aggregated clients instead.

Image

Image

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/client-s3 3.749.0, @aws-sdk/client-dynamodb 3.749.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v22.2.0

Reproduction Steps

Our setup: esbuild, using the following config:

{
  bundle: true,
  treeShaking: true,
  minify: true,
  sourcemap: true,

  legalComments: "none",

  platform: "node",
  target: "es2020",
  mainFields: ['module', 'main'],

  conditions: [
    'wintercg',
    'import',
    'module',
    'node',
  ],

  define: {
    'process.env.NODE_ENV': '"production"',
  },

  loader: {
    ".node": "file",
  },

  alias: {
    'lodash': 'lodash-es',
  },
}

Observed Behavior

From what I can tell, the tree shaking stops working because the "S3.js" and "DynamoDB.js" files include a list of all commands to support on the clients. Even though we're not importing those files/clients (and use the raw S3Client and DynamoDBClient instead), the file likely still gets evaluated:

createAggregatedClient(commands, DynamoDB);

createAggregatedClient(commands, S3);

There already has been an issue up on StackOverflow about this:

https://stackoverflow.com/q/79169593/1397894

Here is an analysis by esbuild:

Imported file src/services/dynamoDbClient.ts contains:
  import "@aws-sdk/client-dynamodb";

Imported file ../../node_modules/.pnpm/@[email protected]/node_modules/@aws-sdk/client-dynamodb/dist-es/index.js contains:
  import "./commands";

Imported file ../../node_modules/.pnpm/@[email protected]/node_modules/@aws-sdk/client-dynamodb/dist-es/commands/index.js contains:
  import "./DescribeKinesisStreamingDestinationCommand";

So imported file ../../node_modules/.pnpm/@[email protected]/node_modules/@aws-sdk/client-dynamodb/dist-es/commands/DescribeKinesisStreamingDestinationCommand.js is included in the bundle.

Expected Behavior

Only the commands used should be included in the bundle.

Possible Solution

When importing the S3 client directly, via using the path directly, the issue goes away. However, this is impractical for the DynamoDB client, as we'd need to do the same for every command import that we use.

Additional Information/Context

This issue is based on taking the recommendations mentioned in #3542 already into account.

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.closed-for-stalenessp2This is a standard priority issuepotential-regressionMarking this issue as a potential regression to be checked by team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions