Skip to content

Commit f8c7f26

Browse files
committed
build: configure static globals to control bundle/debug code
1 parent 5216184 commit f8c7f26

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

Diff for: packages/browser/rollup.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const commitHash = require('child_process')
99
.trim();
1010

1111
const terserInstance = terser({
12+
compress: {
13+
// Tell env.ts that we're building a browser bundle and that we do not
14+
// want to have unnecessary debug functionality.
15+
global_defs: {
16+
__SENTRY_BROWSER_BUNDLE__: true,
17+
__SENTRY_NDEBUG__: true,
18+
},
19+
},
1220
mangle: {
1321
// captureExceptions and captureMessage are public API methods and they don't need to be listed here
1422
// as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version.

Diff for: packages/utils/src/env.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This module mostly exists for optimizations in the build process
3+
* through rollup and terser. We define some global constants which
4+
* are normally undefined. However terser overrides these with global
5+
* definitions which can be evaluated by the static analyzer when
6+
* creating a bundle.
7+
*
8+
* In turn the `isDebugBuild` and `isBrowserBundle` functions are pure
9+
* and can help us remove unused code from the bundles.
10+
*/
11+
12+
declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined;
13+
declare const __SENTRY_NDEBUG__: boolean | undefined;
14+
15+
/**
16+
* Figures out if we're building with debug functionality.
17+
*
18+
* @returns true if this is a debug build
19+
*/
20+
export function isDebugBuild(): boolean {
21+
return !__SENTRY_NDEBUG__;
22+
}
23+
24+
/**
25+
* Figures out if we're building a browser bundle.
26+
*
27+
* @returns true if this is a browser bundle build.
28+
*/
29+
export function isBrowserBundle(): boolean {
30+
return !!__SENTRY_BROWSER_BUNDLE__;
31+
}

Diff for: packages/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export * from './string';
1717
export * from './supports';
1818
export * from './syncpromise';
1919
export * from './time';
20+
export * from './env';

Diff for: packages/utils/src/node.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
* you must either a) use `console.log` rather than the logger, or b) put your function elsewhere.
44
*/
55

6+
import { isBrowserBundle } from './env';
7+
68
/**
79
* Checks whether we're in the Node.js or Browser environment
810
*
911
* @returns Answer to given question
1012
*/
1113
export function isNodeEnv(): boolean {
12-
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
14+
// explicitly check for browser bundles as those can be optimized statically
15+
// by terser/rollup.
16+
return (
17+
!isBrowserBundle() &&
18+
Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'
19+
);
1320
}
1421

1522
/**

0 commit comments

Comments
 (0)