Tree Shaking
了解如何通过 tree shaking 未使用的代码来减少 Sentry 打包大小。
Sentry SDK 以多种方式支持 tree shaking。为了充分利用现代打包工具(如 webpack 或 Rollup)的 tree shaking 功能,需要应用一些额外的配置。如果您希望最小化 Sentry SDK 的打包大小,我们建议阅读本页面并按照所示应用 tree shaking 配置。
Sentry SDK 包含了一些并非严格必需的代码,例如用于调试您的 Sentry 配置或启用跟踪的代码。虽然调试代码在开发环境中非常有用,但在生产环境中通常不需要包含这些代码,因为它们会占用宝贵的打包空间。JavaScript SDK 在其 CommonJS 和 ESM 发行版中包含了特殊的标志,可以在构建过程中帮助 tree shaking(移除)这类代码。
您没有导入和使用的任何代码将自动在使用现代打包工具(如 Webpack、Rollup、Vite 等)时被 tree shaken 掉。这意味着像 Replay、BrowserTracing、BrowserProfiling 等可选集成以及任何未使用的实用方法不会包含在您的打包中,除非您自己导入并使用了它们。本页面的其余部分涉及如何 tree shake 内部 SDK 代码,这些代码除非您使用某些特定功能,否则不是必需的。
此配置从打包工具插件 v2.9.0 版本开始可用。
如果您正在使用我们的某个打包工具插件,可以使用 bundleSizeOptimizations
配置选项来 tree shake 可选代码:
// For example, the @sentry/webpack-plugin passed to the webpack config
sentryPlugin({
// other config
bundleSizeOptimizations: {
excludeDebugStatements: true,
excludePerformanceMonitoring: true,
},
});
更多详情,请参阅您所使用的具体打包工具插件的文档:
如果您希望 tree shake 可选代码,可以通过替换 Sentry SDK 中的各种标志来从构建输出中移除这些代码。请注意,如果您已经通过 Sentry 打包工具插件配置了 tree shaking,则无需手动执行此操作——插件会为您处理。
以下标志可用:
__SENTRY_DEBUG__
将此标志替换为 false
将 tree shake 与调试日志相关的所有 SDK 代码。
__SENTRY_TRACING__
将此标志替换为 false
将 tree shake 与跟踪相关的所有 SDK 代码。
当您使用任何与跟踪相关的 SDK 功能(例如,Sentry.startTransaction()
)时,不要将 __SENTRY_TRACING__
替换为 false
。此标志旨在与像 @sentry/next
或 @sentry/sveltekit
这样的包结合使用,这些包会自动包含跟踪功能。
要在 webpack 打包中 tree shake Sentry 调试代码,我们建议使用 webpack 的 DefinePlugin:
webpack.config.js
const webpack = require("webpack");
module.exports = {
// ... other options
plugins: [
new webpack.DefinePlugin({
__SENTRY_DEBUG__: false,
__SENTRY_TRACING__: false,
}),
// ... other plugins
],
};
如果您正在使用 rollup.js
,我们建议使用 Rollup 的 replace
插件:
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({
__SENTRY_DEBUG__: false,
__SENTRY_TRACING__: false,
}),
// ... other plugins (best placed after)
],
};
默认情况下,Sentry SDK 会设置一组 默认集成,以扩展您的 SDK 功能。您还可以在 SDK 配置中添加 其他 或 自定义 集成。 如果您不希望在配置中包含默认集成,可以使用 Sentry.initWithoutDefaultIntegrations()
并手动传递您想要的集成:
const Sentry = require("@sentry/node");
// This will not add _any_ integrations by default!
Sentry.initWithoutDefaultIntegrations({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [
Sentry.httpIntegration(),
Sentry.consoleIntegration(),
Sentry.dedupeIntegration(),
Sentry.inboundFiltersIntegration(),
],
});
如果您只想使用 Sentry 进行错误监控,而不关心任何性能监控功能,可以使用以下初始化方法,以确保任何与性能相关的集成不会包含在您的打包中,并且可以通过打包工具 tree shake 掉:
const Sentry = require("@sentry/node");
// This will not add _any_ integrations by default!
Sentry.initWithoutDefaultIntegrations({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [
// Adds all default integrations
// except the ones that are only relevant for performance
...Sentry.getDefaultIntegrationsWithoutPerformance(),
],
});