Tree Shaking

Learn how to reduce Sentry bundle size by tree shaking unused code.

The Sentry SDK supports tree shaking in various ways. To fully utilize the tree shaking capabilities of modern bundlers like webpack or Rollup, some additional configurations must be applied. If you want to minimize the bundle size of the Sentry SDK, we recommend reading through this page and applying the tree shaking configurations as shown.

The Sentry SDK ships with code that is not strictly required for it to collect your errors. This includes code to debug your Sentry configuration or code to enable tracing, for example. While debug code can be very useful in development environments, it's not typically necessary to include it in your production bundles where it takes up valuable space. The JavaScript SDK includes special flags in its CommonJS and ESM distributions, which can be used to facilitate tree shaking (removal) of this kind of code during the build process.

If you want to tree shake optional code, remove the code from your build output by replacing various flags in the Sentry SDK. Note that if you already configured tree shaking via the Sentry Bundler Plugins, you do not need to do this manually - the plugins will take care of it for you.

The following flags are available:

__SENTRY_DEBUG__

Replacing this flag with false will tree shake any SDK code that's related to debug logging.

__SENTRY_TRACING__

Replacing this flag with false will tree shake any SDK code that's related to tracing.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").