Skip to content

Commit 7f7fe69

Browse files
committed
fix(debugId): Add guards for injected code to avoid errors
1 parent c15f9df commit 7f7fe69

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

packages/bundler-plugin-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export function createComponentNameAnnotateHooks(ignoredComponents?: string[]):
431431
}
432432

433433
export function getDebugIdSnippet(debugId: string): string {
434-
return `;{try{(function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}");})();}catch(e){}};`;
434+
return `;{try{(function(){var e=("undefined"!=typeof window&&window)||("undefined"!=typeof global&&global)||("undefined"!=typeof globalThis&&globalThis)||("undefined"!=typeof self&&self)||{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="${debugId}",e._sentryDebugIdIdentifier="sentry-dbid-${debugId}");})();}catch(e){}};`;
435435
}
436436

437437
export type { Logger } from "./logger";

packages/bundler-plugin-core/src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export function generateGlobalInjectorCode({
314314
}): string {
315315
// The code below is mostly ternary operators because it saves bundle size.
316316
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
317-
let code = `!function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};`;
317+
let code = `!function(){try{var e=("undefined"!=typeof window&&window)||("undefined"!=typeof global&&global)||("undefined"!=typeof globalThis&&globalThis)||("undefined"!=typeof self&&self)||{};`;
318318

319319
code += `e.SENTRY_RELEASE={id:${JSON.stringify(release)}};`;
320320

@@ -324,7 +324,7 @@ export function generateGlobalInjectorCode({
324324
code += `e.SENTRY_BUILD_INFO=${JSON.stringify(buildInfo)};`;
325325
}
326326

327-
code += "}();";
327+
code += "}catch(e){}}();";
328328

329329
return code;
330330
}
@@ -334,9 +334,10 @@ export function generateModuleMetadataInjectorCode(metadata: any): string {
334334
// The code below is mostly ternary operators because it saves bundle size.
335335
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
336336
// We are merging the metadata objects in case modules are bundled twice with the plugin
337-
return `!function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],${JSON.stringify(
337+
// Use try-catch to avoid issues when bundlers rename global variables like 'window' to 'k'
338+
return `!function(){try{var e=("undefined"!=typeof window&&window)||("undefined"!=typeof global&&global)||("undefined"!=typeof globalThis&&globalThis)||("undefined"!=typeof self&&self)||{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},e._sentryModuleMetadata[(new e.Error).stack]=function(e){for(var n=1;n<arguments.length;n++){var a=arguments[n];if(null!=a)for(var t in a)a.hasOwnProperty(t)&&(e[t]=a[t])}return e}({},e._sentryModuleMetadata[(new e.Error).stack],${JSON.stringify(
338339
metadata
339-
)})}();`;
340+
)})}catch(e){}}();`;
340341
}
341342

342343
export function getBuildInformation(): {

0 commit comments

Comments
 (0)