Skip to content

Commit c407c11

Browse files
committed
feat(javascript): Add tree shaking for default integrations guide
1 parent d31f719 commit c407c11

File tree

1 file changed

+52
-3
lines changed
  • src/platforms/javascript/common/configuration/tree-shaking

1 file changed

+52
-3
lines changed

src/platforms/javascript/common/configuration/tree-shaking/index.mdx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ description: "Learn how to reduce Sentry bundle size by tree shaking unused code
55
keywords: ["bundle size", "webpack", "rollup", "debug"]
66
---
77

8-
Sentry ships with code that enables you to debug your Sentry configuration, primarily through logging. While this 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 a special flag in its CommonJS and ESM distributions which can be used to facilitate [tree shaking](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking) (removal) of such code during the build process.
8+
The Sentry SDK supports [tree shaking](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking) in various ways.
9+
To fully utilize the tree shaking capabilities of modern bundlers like Webpack or Rollup, some additional configurations must be applied.
10+
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.
11+
12+
## Tree Shaking Debug Code
13+
14+
Sentry ships with code that enables you to debug your Sentry configuration, primarily through logging. While this 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 a special flag in its CommonJS and ESM distributions which can be used to facilitate tree shaking (removal) of such code during the build process.
915

1016
To make debugging code eligible for tree shaking, you must replace the `__SENTRY_DEBUG__` flag in the Sentry SDK with `false`.
1117

1218
<PlatformSection notSupported={["javascript.nextjs"]}>
1319

14-
## Tree Shaking Debug Code With Webpack
20+
### Tree Shaking Debug Code With Webpack
1521

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

@@ -33,7 +39,7 @@ module.exports = {
3339

3440
<PlatformSection notSupported={["javascript.nextjs"]}>
3541

36-
## Tree Shaking Debug Code With Rollup
42+
### Tree Shaking Debug Code With Rollup
3743

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

@@ -79,3 +85,46 @@ const nextConfig = {
7985
For more information on custom webpack configurations in Next.js, see [Custom Webpack Config](https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config) in the Next.js docs.
8086

8187
</PlatformSection>
88+
89+
## Tree Shaking Default Integrations
90+
91+
By default, the Sentry SDK sets up a list of [default integrations](../integrations/default) that extend your
92+
SDK functionality. Furthermore, you can also add [additional](../integrations/plugin) or [custom](../integrations/custom)
93+
integrations to your SDK configuration.
94+
In case you do not want to include default integrations in your config, you can [disable](../options/#integration-configuration)
95+
them and add your custom array of integrations.
96+
However, if you also want to tree shake the unused default integrations, you can do so by creating a `Client` yourself.
97+
By doing this, you essentially bypass `Sentry.init` which normally creates a `Client` for you.
98+
99+
The following example shows how to create and bind a `Client` which enables tree shaking of unused default integrations:
100+
101+
```javascript {filename:main.js}
102+
import {
103+
BrowserClient,
104+
defaultStackParser,
105+
makeFetchTransport,
106+
Integrations,
107+
getCurrentHub,
108+
} from "@sentry/browser";
109+
110+
const client = new BrowserClient({
111+
// all options you normally pass to Sentry.init
112+
dsn: "your DSN",
113+
// ...
114+
115+
transport: makeFetchTransport,
116+
stackParser: defaultStackParser,
117+
// Only the integrations listed here will be used
118+
integrations: [
119+
new Integrations.Breadcrumbs(),
120+
new Integrations.GlobalHandlers(),
121+
new Integrations.LinkedErrors(),
122+
new Integrations.Dedupe(),
123+
],
124+
});
125+
126+
getCurrentHub().bindClient(client);
127+
```
128+
129+
Note that in contrast to `Sentry.init`, the `Client` constructor expects options of type `ClientOptions` instead of `Options`.
130+
This means that the `ClientOptions.integrations` property is the final array of integrations that will be used.

0 commit comments

Comments
 (0)