@@ -9,13 +9,14 @@ import { releaseManagementPlugin } from "./plugins/release-management";
99import { telemetryPlugin } from "./plugins/telemetry" ;
1010import { createLogger } from "./sentry/logger" ;
1111import { allowedToSendTelemetry , createSentryInstance } from "./sentry/telemetry" ;
12- import { Options } from "./types" ;
12+ import { Options , SentrySDKBuildFlags } from "./types" ;
1313import {
1414 generateGlobalInjectorCode ,
1515 generateModuleMetadataInjectorCode ,
1616 getDependencies ,
1717 getPackageJson ,
1818 parseMajorVersion ,
19+ replaceBooleanFlagsInCode ,
1920 stringToUUID ,
2021 stripQueryAndHashFromPath ,
2122} from "./utils" ;
@@ -27,6 +28,7 @@ interface SentryUnpluginFactoryOptions {
2728 moduleMetadataInjectionPlugin ?: ( injectionCode : string ) => UnpluginOptions ;
2829 debugIdInjectionPlugin : ( ) => UnpluginOptions ;
2930 debugIdUploadPlugin : ( upload : ( buildArtifacts : string [ ] ) => Promise < void > ) => UnpluginOptions ;
31+ bundleSizeOptimizationsPlugin : ( buildFlags : SentrySDKBuildFlags ) => UnpluginOptions ;
3032}
3133
3234/**
@@ -61,6 +63,7 @@ export function sentryUnpluginFactory({
6163 moduleMetadataInjectionPlugin,
6264 debugIdInjectionPlugin,
6365 debugIdUploadPlugin,
66+ bundleSizeOptimizationsPlugin,
6467} : SentryUnpluginFactoryOptions ) {
6568 return createUnplugin < Options , true > ( ( userOptions , unpluginMetaContext ) => {
6669 const logger = createLogger ( {
@@ -161,6 +164,31 @@ export function sentryUnpluginFactory({
161164 } )
162165 ) ;
163166
167+ if ( options . bundleSizeOptimizations ) {
168+ const { bundleSizeOptimizations } = options ;
169+ const replacementValues : SentrySDKBuildFlags = { } ;
170+
171+ if ( bundleSizeOptimizations . excludeDebugStatements ) {
172+ replacementValues [ "__SENTRY_DEBUG__" ] = false ;
173+ }
174+ if ( bundleSizeOptimizations . excludePerformanceMonitoring ) {
175+ replacementValues [ "__SENTRY_TRACE__" ] = false ;
176+ }
177+ if ( bundleSizeOptimizations . excludeReplayCanvas ) {
178+ replacementValues [ "__RRWEB_EXCLUDE_CANVAS__" ] = true ;
179+ }
180+ if ( bundleSizeOptimizations . excludeReplayIframe ) {
181+ replacementValues [ "__RRWEB_EXCLUDE_IFRAME__" ] = true ;
182+ }
183+ if ( bundleSizeOptimizations . excludeReplayShadowDom ) {
184+ replacementValues [ "__RRWEB_EXCLUDE_SHADOW_DOM__" ] = true ;
185+ }
186+
187+ if ( Object . keys ( replacementValues ) . length > 0 ) {
188+ plugins . push ( bundleSizeOptimizationsPlugin ( replacementValues ) ) ;
189+ }
190+ }
191+
164192 if ( ! options . release . inject ) {
165193 logger . debug (
166194 "Release injection disabled via `release.inject` option. Will not inject release."
@@ -371,6 +399,14 @@ export function createRollupReleaseInjectionHooks(injectionCode: string) {
371399 } ;
372400}
373401
402+ export function createRollupBundleSizeOptimizationHooks ( replacementValues : SentrySDKBuildFlags ) {
403+ return {
404+ transform ( code : string ) {
405+ return replaceBooleanFlagsInCode ( code , replacementValues ) ;
406+ } ,
407+ } ;
408+ }
409+
374410// We need to be careful not to inject the snippet before any `"use strict";`s.
375411// As an additional complication `"use strict";`s may come after any number of comments.
376412const COMMENT_USE_STRICT_REGEX =
@@ -475,6 +511,6 @@ export function getDebugIdSnippet(debugId: string): string {
475511 return `;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${ debugId } ",e._sentryDebugIdIdentifier="sentry-dbid-${ debugId } ")}catch(e){}}();` ;
476512}
477513
478- export { stringToUUID } from "./utils" ;
514+ export { stringToUUID , replaceBooleanFlagsInCode } from "./utils" ;
479515
480- export type { Options } from "./types" ;
516+ export type { Options , SentrySDKBuildFlags } from "./types" ;
0 commit comments