-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add guide for tree shaking in JavaScript #4900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fed5dbe
Add guide for tree shaking in JavaScript
lforst 5a55c67
Add tree shaking section for Next.js
lforst 13ef01a
Apply suggestions from code review
lforst 41391dc
Show only next.js tree shaking section in next.js troubleshooting guide
lforst c96c944
Apply suggestions from code review
lforst 8705554
Move tree shaking tutorial to configuration
lforst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -403,3 +403,75 @@ document.body.addEventListener( | |
``` | ||
|
||
Remember to pass in `true` as the second parameter to `addEventListener()`. Without it, the event handler won't be called, since it's being added to the event target's ancestor rather than the event target itself, and unlike JavaScript runtime errors, `error` events resulting from load failures don't bubble, and therefore must be captured during the `capture` phase. For more information, see [the W3C spec](https://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Events-phases). | ||
|
||
## Reducing Bundle Size | ||
|
||
Sentry ships with code that enables you to <PlatformLink to="/configuration/options/#debug">debug your Sentry configuration</PlatformLink>. While this code can be very useful in development environments, it is usually not necessary to include it in your production bundles where it takes up precious bytes. The JavaScript SDK uses special flags in its CommonJS and ESM distributions to facilitate tree-shaking of code sections that are used for debugging. Tree shaking is a way to remove unused code in your application during the build process. | ||
|
||
To mark any debug code as unused, we must replace debug flags in the sentry SDK with `false`. We outlined examples of how to do this in popular toolchains below. | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Tree-shaking Debug Code with webpack | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we hide these for the nextjs platform? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah you're right. For nextjs, only the nextjs config section makes sense. Updated in: 41391dc
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To tree-shake Sentry debug code in your webpack bundles, we recommend using webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/). Use it to replace all occurrences of the `__SENTRY_DEBUG__` flag with `false`: | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript {filename:webpack.config.js} | ||
const webpack = require("webpack"); | ||
|
||
module.exports = { | ||
// ... other options | ||
plugins: [ | ||
new webpack.DefinePlugin({ | ||
__SENTRY_DEBUG__: false, | ||
}), | ||
// ... other plugins | ||
], | ||
}; | ||
``` | ||
|
||
### Tree-shaking Debug Code with rollup.js | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
If you are using rollup.js, we recommend [rollup's replace plugin](https://www.npmjs.com/package/@rollup/plugin-replace) to replace all occurrences of `__SENTRY_DEBUG__` with `false`: | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript {filename:rollup.config.js} | ||
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({ | ||
preventAssignment: false, // not necessary because flags are not assigned to | ||
values: { | ||
__SENTRY_DEBUG__: false, | ||
}, | ||
}), | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ... other plugins (best placed after) | ||
], | ||
}; | ||
``` | ||
|
||
<PlatformSection supported={["javascript.nextjs"]}> | ||
|
||
### Tree-shaking Debug Code with Next.js | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To tree-shake Sentry debug code in Next.js projects, you can use webpack's [DefinePlugin](https://webpack.js.org/plugins/define-plugin/). The following Next.js configuration will replace all occurrences of `__SENTRY_DEBUG__` in your bundles with `false`: | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript {filename:next.config.js} | ||
const nextConfig = { | ||
webpack: (config, { webpack }) => { | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config.plugins.push( | ||
new webpack.DefinePlugin({ | ||
__SENTRY_DEBUG__: false, | ||
}) | ||
); | ||
|
||
// return the modified config | ||
return config; | ||
}, | ||
}; | ||
``` | ||
|
||
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. | ||
lforst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</PlatformSection> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.