|
| 1 | +--- |
| 2 | +title: Tree Shaking |
| 3 | +sidebar_order: 11000 |
| 4 | +description: "Learn how to reduce Sentry bundle size by tree shaking unused code." |
| 5 | +keywords: ["bundle size", "webpack", "rollup", "debug"] |
| 6 | +--- |
| 7 | + |
| 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. |
| 9 | + |
| 10 | +To make debugging code eligible for tree shaking, you must replace the `__SENTRY_DEBUG__` flag in the Sentry SDK with `false`. |
| 11 | + |
| 12 | +<PlatformSection notSupported={["javascript.nextjs"]}> |
| 13 | + |
| 14 | +## Tree Shaking Debug Code With Webpack |
| 15 | + |
| 16 | +To tree shake Sentry debug code in your webpack bundles, we recommend using webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/). |
| 17 | + |
| 18 | +```javascript {filename:webpack.config.js} |
| 19 | +const webpack = require("webpack"); |
| 20 | + |
| 21 | +module.exports = { |
| 22 | + // ... other options |
| 23 | + plugins: [ |
| 24 | + new webpack.DefinePlugin({ |
| 25 | + __SENTRY_DEBUG__: false, |
| 26 | + }), |
| 27 | + // ... other plugins |
| 28 | + ], |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +</PlatformSection> |
| 33 | + |
| 34 | +<PlatformSection notSupported={["javascript.nextjs"]}> |
| 35 | + |
| 36 | +## Tree Shaking Debug Code With Rollup |
| 37 | + |
| 38 | +If you're using `rollup.js`, we recommend using [Rollup's `replace` plugin](https://github.com/rollup/plugins/tree/master/packages/replace). |
| 39 | + |
| 40 | +```javascript {filename:rollup.config.js} |
| 41 | +import replace from "@rollup/plugin-replace"; |
| 42 | +import { terser } from "rollup-plugin-terser"; |
| 43 | + |
| 44 | +export default { |
| 45 | + // ... other options |
| 46 | + treeshake: "smallest", // recommended for best tree shaking results |
| 47 | + plugins: [ |
| 48 | + replace({ |
| 49 | + __SENTRY_DEBUG__: false, |
| 50 | + }), |
| 51 | + // ... other plugins (best placed after) |
| 52 | + ], |
| 53 | +}; |
| 54 | +``` |
| 55 | + |
| 56 | +</PlatformSection> |
| 57 | + |
| 58 | +<PlatformSection supported={["javascript.nextjs"]}> |
| 59 | + |
| 60 | +## Tree Shaking Debug Code With Next.js |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +```javascript {filename:next.config.js} |
| 65 | +const nextConfig = { |
| 66 | + webpack: (config, { webpack }) => { |
| 67 | + config.plugins.push( |
| 68 | + new webpack.DefinePlugin({ |
| 69 | + __SENTRY_DEBUG__: false, |
| 70 | + }) |
| 71 | + ); |
| 72 | + |
| 73 | + // return the modified config |
| 74 | + return config; |
| 75 | + }, |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +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. |
| 80 | + |
| 81 | +</PlatformSection> |
0 commit comments