You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Sentry SDK ships with code that is not strictly required for it to collect your errors. This includes, for example, code to debug your Sentry configuration or code to enable performance monitoring. 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 a special flags 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.
9
11
10
-
## List Of Flags
12
+
## Tree Shaking Optional Code
13
+
14
+
The Sentry SDK ships with code that is not strictly required for it to collect your errors. This includes, for example, code to debug your Sentry configuration or code to enable performance monitoring. 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 a special flags in its CommonJS and ESM distributions, which can be used to facilitate tree shaking (removal) of such code during the build process.
15
+
16
+
### List Of Flags
11
17
12
18
To make optional code eligible for tree shaking, you can replace various flags in the Sentry SDK with `false`.
13
19
@@ -21,9 +27,9 @@ To make optional code eligible for tree shaking, you can replace various flags i
To tree shake Sentry debug code in Next.js projects, you can use webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/) in your Next.js configuration.
75
81
@@ -92,3 +98,53 @@ const nextConfig = {
92
98
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.
93
99
94
100
</PlatformSection>
101
+
102
+
## Tree Shaking Default Integrations
103
+
104
+
By default, the Sentry SDK sets up a list of [default integrations](../integrations/default) that extend your
105
+
SDK functionality. You can also add [additional](../integrations/plugin) or [custom](../integrations/custom)
106
+
integrations to your SDK configuration.
107
+
If you don't want to include default integrations in your config, you can [disable](../options/#integration-configuration)
108
+
them and add your custom array of integrations.
109
+
However, if you also want to tree shake the unused default integrations, you can do so by creating a `Client` yourself.
110
+
By doing this, you essentially bypass `Sentry.init` which normally creates a `Client` for you.
111
+
112
+
The following example shows how to create and bind a `Client` which enables tree shaking of unused default integrations:
113
+
114
+
```javascript {filename:main.js}
115
+
import {
116
+
BrowserClient,
117
+
Breadcrumbs,
118
+
Dedupe,
119
+
defaultStackParser,
120
+
getCurrentHub,
121
+
GlobalHandlers,
122
+
makeFetchTransport,
123
+
LinkedErrors,
124
+
} from"@sentry/browser";
125
+
126
+
constclient=newBrowserClient({
127
+
// all options you normally pass to Sentry.init
128
+
dsn:"your DSN",
129
+
// ...
130
+
131
+
transport: makeFetchTransport,
132
+
stackParser: defaultStackParser,
133
+
// Only the integrations listed here will be used
134
+
integrations: [
135
+
newBreadcrumbs(),
136
+
newGlobalHandlers(),
137
+
newLinkedErrors(),
138
+
newDedupe(),
139
+
],
140
+
});
141
+
142
+
getCurrentHub().bindClient(client);
143
+
```
144
+
145
+
<Note>
146
+
147
+
In contrast to `Sentry.init`, the `Client` constructor expects options of type `ClientOptions` instead of `Options`.
148
+
This means that the `ClientOptions.integrations` property is the final array of all integrations that will be used.
0 commit comments