Skip to content

build: configure static globals to control bundle/debug code #4273

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 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_NO_DEBUG__: 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.
Expand Down
31 changes: 31 additions & 0 deletions packages/utils/src/env.ts
Original file line number Diff line number Diff line change
@@ -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_NO_DEBUG__: boolean | undefined;

/**
* Figures out if we're building with debug functionality.
*
* @returns true if this is a debug build
*/
export function isDebugBuild(): boolean {
return typeof __SENTRY_NO_DEBUG__ !== 'undefined' && !__SENTRY_BROWSER_BUNDLE__;
}

/**
* Figures out if we're building a browser bundle.
*
* @returns true if this is a browser bundle build.
*/
export function isBrowserBundle(): boolean {
return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__;
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './string';
export * from './supports';
export * from './syncpromise';
export * from './time';
export * from './env';
9 changes: 8 additions & 1 deletion packages/utils/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
);
}

/**
Expand Down