Skip to content

Conversation

@MichielVrins
Copy link

@MichielVrins MichielVrins commented Dec 27, 2025

Packages impacted by this PR

  • "@azure/identity"

Issues associated with this PR

None

Describe the problem that is addressed by this PR

This PR reduces the bundle size of this library by making it more tree shakable. Before this PR the following code would pull in 851KB of unminified js from @azure/identity. This PR brings that down to 753KB. Other authentication flows should see similar results.

import { ClientCertificateCredential } from "./credentials/clientCertificateCredential.js";

export const credential = new ClientCertificateCredential("tenant-id", "client-id", {
  certificatePath: "assets/fake-cert.pem",
});

Two fixes that are addressed in this PR are.

  1. Import required module's exported functions or constants directly instead of using import * (851kb to 790kb)
  2. Splitting of auth flows into exported functions and inject the client context instead of having 1 monolithic client which is inherently not tree shakable. (790kb -> 753kb)

Below are screenshots of a before and after bundle for only ClientCertificateCredential.

image image

Future improvements

Taking a look at the resulting bundles I can still see some easy wins to reduce the bundle size further. One that immediately stands out to me, is that jsonwebtoken pulls in quite some js. E.g. jsonwebtoken pulls in semver which accounts for almost 10% of the whole bundle size (61.3KB) and some more. Sadly this needs to be fixed upstream in the msal libraries which are not in this monorepo.

What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen?

The msalClient could have been trimmed down to only having a context and the auth flow specific code could have been moved to it's respective flows. Since many of the functions here follow a similar pattern they can easily get out of sync and get harder to maintain. I suspect that that's why they were structured like this in the first place, and that's why I kept it like this.

Are there test cases added in this PR? (If not, why?)

No, tests only had to get plumbed up correctly again. No new logic was added.

Provide a list of related PRs (if any)

None

Command used to generate this PR:**(Applicable only to SDK release request PRs)

None

Checklists

  • Added impacted package name to the issue description
  • Does this PR needs any fixes in the SDK Generator?** (If so, create an Issue in the Autorest/typescript repository and link it here)
  • Added a changelog (if necessary)

This change alone achieves a reduction in bundle size for a simple
test file with only ClientCertificateCredential from 851kb to 790kb
This is part of trying to make auth flows more treeshakable. This
change doesn't bring any direct benefits, because the same imports
are still happening in other places
@github-actions github-actions bot added Azure.Identity Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Dec 27, 2025
@github-actions
Copy link

Thank you for your contribution @MichielVrins! We will review the pull request and get back to you soon.

@MichielVrins
Copy link
Author

@microsoft-github-policy-service agree

Exporting one client with all flows in it as a function makes it
inherently not tree shakable increasing bundle size. This commit
splits all those flows into functions taking the context as a
parameter.

This resulted in a bundle size reduction from 790kb -> 753kb on
an artifical test bundle with only ClientCertificateCredential
@MichielVrins MichielVrins force-pushed the feat/bundle-size-reduction branch from 24b18c9 to 04f0175 Compare December 27, 2025 15:13
@MichielVrins MichielVrins changed the title Feat/bundle size reduction Reduce bundle size for @azure/identity with better tree shaking support Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure.Identity Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

1 participant