Skip to content

Commit ebc0790

Browse files
Lms24imatwawana
authored andcommitted
Add tree shaking guide for default integrations (#4996)
Co-authored-by: Isabel <[email protected]>
1 parent 4f47cc4 commit ebc0790

File tree

1 file changed

+63
-7
lines changed
  • src/platforms/javascript/common/configuration/tree-shaking

1 file changed

+63
-7
lines changed

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

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

8-
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.
911

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
1117

1218
To make optional code eligible for tree shaking, you can replace various flags in the Sentry SDK with `false`.
1319

@@ -21,9 +27,9 @@ To make optional code eligible for tree shaking, you can replace various flags i
2127

2228
<PlatformSection notSupported={["javascript.nextjs"]}>
2329

24-
## Tree Shaking Optional Code With Webpack
30+
### Tree Shaking Optional Code With Webpack
2531

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

2834
```javascript {filename:webpack.config.js}
2935
const webpack = require("webpack");
@@ -44,9 +50,9 @@ module.exports = {
4450

4551
<PlatformSection notSupported={["javascript.nextjs"]}>
4652

47-
## Tree Shaking Optional Code With Rollup
53+
### Tree Shaking Optional Code With Rollup
4854

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

5157
```javascript {filename:rollup.config.js}
5258
import replace from "@rollup/plugin-replace";
@@ -69,7 +75,7 @@ export default {
6975

7076
<PlatformSection supported={["javascript.nextjs"]}>
7177

72-
## Tree Shaking Optional Code With Next.js
78+
### Tree Shaking Optional Code With Next.js
7379

7480
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.
7581

@@ -92,3 +98,53 @@ const nextConfig = {
9298
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.
9399

94100
</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+
const client = new BrowserClient({
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+
new Breadcrumbs(),
136+
new GlobalHandlers(),
137+
new LinkedErrors(),
138+
new Dedupe(),
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.
149+
150+
</Note>

0 commit comments

Comments
 (0)