# Tree Shaking | Sentry for Cloud Functions for Firebase

The Sentry SDK supports [tree shaking](https://developer.mozilla.org/en-US/docs/Glossary/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.

## [Tree Shaking Optional Code](https://docs.sentry.io/platforms/javascript/guides/firebase/configuration/tree-shaking.md#tree-shaking-optional-code)

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.

Anything that you don't import and use will *automatically* be tree shaken away when using any modern bundler like Webpack, Rollup, Vite, or similar. This means that optional integrations like Replay, BrowserTracing, BrowserProfiling, and any unused utility methods won't be included in your bundle unless you import and use them yourself. The rest of this page relates to ways to tree shake internal SDK code, which isn't strictly required unless you use certain features.

### [Tree Shaking With Sentry Bundler Plugins](https://docs.sentry.io/platforms/javascript/guides/firebase/configuration/tree-shaking.md#tree-shaking-with-sentry-bundler-plugins)

*This configuration is available starting with v2.9.0 of the bundler plugins.*

If you're using one of our bundler plugins, you can use the `bundleSizeOptimizations` configuration option to tree shake optional code:

```javascript
// For example, the @sentry/webpack-plugin passed to the webpack config
sentryPlugin({
  // other config
  bundleSizeOptimizations: {
    excludeDebugStatements: true,
    excludePerformanceMonitoring: true,
  },
});
```

For more details, see the documentation for the specific bundler plugin you're using:

* [Sentry Webpack Plugin](https://www.npmjs.com/package/@sentry/webpack-plugin)
* [Sentry Vite Plugin](https://www.npmjs.com/package/@sentry/vite-plugin)
* [Sentry Esbuild Plugin](https://www.npmjs.com/package/@sentry/esbuild-plugin)
* [Sentry Rollup Plugin](https://www.npmjs.com/package/@sentry/rollup-plugin)

### [Manual Tree Shaking](https://docs.sentry.io/platforms/javascript/guides/firebase/configuration/tree-shaking.md#manual-tree-shaking)

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.

`__SENTRY_TRACING__` must not be replaced with `false` when you're using any tracing-related SDK features (for example, `Sentry.startSpan()`). This flag is intended to be used in combination with packages like `@sentry/next` or `@sentry/sveltekit`, which automatically include the tracing functionality.

### [Tree Shaking With Webpack](https://docs.sentry.io/platforms/javascript/guides/firebase/configuration/tree-shaking.md#tree-shaking-with-webpack)

To tree shake Sentry debug code in your webpack bundles, we recommend using webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/):

`webpack.config.js`

```javascript
const webpack = require("webpack");

module.exports = {
  // ... other options
  plugins: [
    new webpack.DefinePlugin({
      __SENTRY_DEBUG__: false,
      __SENTRY_TRACING__: false,
    }),
    // ... other plugins
  ],
};
```

### [Tree Shaking With Rollup](https://docs.sentry.io/platforms/javascript/guides/firebase/configuration/tree-shaking.md#tree-shaking-with-rollup)

If you're using `rollup.js`, we recommend using [Rollup's `replace` plugin](https://github.com/rollup/plugins/tree/master/packages/replace):

`rollup.config.js`

```javascript
import replace from "@rollup/plugin-replace";
import { terser } from "rollup-plugin-terser";

export default {
  // ... other options
  treeshake: "smallest", // recommended for best tree shaking results
  plugins: [
    replace({
      __SENTRY_DEBUG__: false,
      __SENTRY_TRACING__: false,
    }),
    // ... other plugins (best placed after)
  ],
};
```
