From f8c7f265458c4d183d51f0e55368fdbd320e696e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 14 Dec 2021 00:46:20 +0100 Subject: [PATCH 1/3] build: configure static globals to control bundle/debug code --- packages/browser/rollup.config.js | 8 ++++++++ packages/utils/src/env.ts | 31 +++++++++++++++++++++++++++++++ packages/utils/src/index.ts | 1 + packages/utils/src/node.ts | 9 ++++++++- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/utils/src/env.ts diff --git a/packages/browser/rollup.config.js b/packages/browser/rollup.config.js index 4de85f669aab..e5fe476cd22c 100644 --- a/packages/browser/rollup.config.js +++ b/packages/browser/rollup.config.js @@ -9,6 +9,14 @@ const commitHash = require('child_process') .trim(); const terserInstance = terser({ + compress: { + // Tell env.ts that we're building a browser bundle and that we do not + // want to have unnecessary debug functionality. + global_defs: { + __SENTRY_BROWSER_BUNDLE__: true, + __SENTRY_NDEBUG__: true, + }, + }, mangle: { // captureExceptions and captureMessage are public API methods and they don't need to be listed here // as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version. diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts new file mode 100644 index 000000000000..c6031a1a7015 --- /dev/null +++ b/packages/utils/src/env.ts @@ -0,0 +1,31 @@ +/** + * This module mostly exists for optimizations in the build process + * through rollup and terser. We define some global constants which + * are normally undefined. However terser overrides these with global + * definitions which can be evaluated by the static analyzer when + * creating a bundle. + * + * In turn the `isDebugBuild` and `isBrowserBundle` functions are pure + * and can help us remove unused code from the bundles. + */ + +declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined; +declare const __SENTRY_NDEBUG__: boolean | undefined; + +/** + * Figures out if we're building with debug functionality. + * + * @returns true if this is a debug build + */ +export function isDebugBuild(): boolean { + return !__SENTRY_NDEBUG__; +} + +/** + * Figures out if we're building a browser bundle. + * + * @returns true if this is a browser bundle build. + */ +export function isBrowserBundle(): boolean { + return !!__SENTRY_BROWSER_BUNDLE__; +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 3c5a3e61a480..ca92d86df6f0 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -17,3 +17,4 @@ export * from './string'; export * from './supports'; export * from './syncpromise'; export * from './time'; +export * from './env'; diff --git a/packages/utils/src/node.ts b/packages/utils/src/node.ts index 18d29fc3144f..33bece5ef62f 100644 --- a/packages/utils/src/node.ts +++ b/packages/utils/src/node.ts @@ -3,13 +3,20 @@ * you must either a) use `console.log` rather than the logger, or b) put your function elsewhere. */ +import { isBrowserBundle } from './env'; + /** * Checks whether we're in the Node.js or Browser environment * * @returns Answer to given question */ export function isNodeEnv(): boolean { - return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'; + // explicitly check for browser bundles as those can be optimized statically + // by terser/rollup. + return ( + !isBrowserBundle() && + Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]' + ); } /** From 4977ed402c8237fb6ad1c63eb39eeb7b26b5ebca Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 14 Dec 2021 01:01:02 +0100 Subject: [PATCH 2/3] fix: make typescript happy --- packages/utils/src/env.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index c6031a1a7015..256d8f510da7 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -18,7 +18,7 @@ declare const __SENTRY_NDEBUG__: boolean | undefined; * @returns true if this is a debug build */ export function isDebugBuild(): boolean { - return !__SENTRY_NDEBUG__; + return typeof __SENTRY_NDEBUG__ !== 'undefined' && !__SENTRY_BROWSER_BUNDLE__; } /** @@ -27,5 +27,5 @@ export function isDebugBuild(): boolean { * @returns true if this is a browser bundle build. */ export function isBrowserBundle(): boolean { - return !!__SENTRY_BROWSER_BUNDLE__; + return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__; } From 53d5aacfcf0e8b620e5e8a9f251e385cfee85fd1 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 14 Dec 2021 11:13:57 +0100 Subject: [PATCH 3/3] ref: NDBEUG -> NO_DEBUG --- packages/browser/rollup.config.js | 2 +- packages/utils/src/env.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/browser/rollup.config.js b/packages/browser/rollup.config.js index e5fe476cd22c..93478a9fd5db 100644 --- a/packages/browser/rollup.config.js +++ b/packages/browser/rollup.config.js @@ -14,7 +14,7 @@ const terserInstance = terser({ // want to have unnecessary debug functionality. global_defs: { __SENTRY_BROWSER_BUNDLE__: true, - __SENTRY_NDEBUG__: true, + __SENTRY_NO_DEBUG__: true, }, }, mangle: { diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index 256d8f510da7..44a0ea4fc98c 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -10,7 +10,7 @@ */ declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined; -declare const __SENTRY_NDEBUG__: boolean | undefined; +declare const __SENTRY_NO_DEBUG__: boolean | undefined; /** * Figures out if we're building with debug functionality. @@ -18,7 +18,7 @@ declare const __SENTRY_NDEBUG__: boolean | undefined; * @returns true if this is a debug build */ export function isDebugBuild(): boolean { - return typeof __SENTRY_NDEBUG__ !== 'undefined' && !__SENTRY_BROWSER_BUNDLE__; + return typeof __SENTRY_NO_DEBUG__ !== 'undefined' && !__SENTRY_BROWSER_BUNDLE__; } /**