Skip to content

Commit 9d7a3f2

Browse files
committed
feat(replay): Allow to treeshake rrweb features
1 parent d46bc08 commit 9d7a3f2

File tree

6 files changed

+285
-5
lines changed

6 files changed

+285
-5
lines changed

.size-limit.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ module.exports = [
77
gzip: true,
88
limit: '90 KB',
99
},
10+
{
11+
name: '@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped)',
12+
path: 'packages/browser/build/npm/esm/index.js',
13+
import: '{ init, Replay, BrowserTracing }',
14+
gzip: true,
15+
limit: '90 KB',
16+
modifyWebpackConfig: function (config) {
17+
const webpack = require('webpack');
18+
config.plugins.push(
19+
new webpack.DefinePlugin({
20+
__SENTRY_DEBUG__: false,
21+
__RRWEB_EXCLUDE_CANVAS__: true,
22+
__RRWEB_EXCLUDE_SHADOW_DOM__: true,
23+
__RRWEB_EXCLUDE_IFRAME__: true,
24+
}),
25+
);
26+
return config;
27+
},
28+
},
1029
{
1130
name: '@sentry/browser (incl. Tracing) - Webpack (gzipped)',
1231
path: 'packages/browser/build/npm/esm/index.js',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"@rollup/plugin-sucrase": "^4.0.3",
8585
"@rollup/plugin-typescript": "^8.3.1",
8686
"@size-limit/preset-small-lib": "~9.0.0",
87+
"@size-limit/webpack": "~9.0.0",
8788
"@strictsoftware/typedoc-plugin-monorepo": "^0.3.1",
8889
"@types/chai": "^4.1.3",
8990
"@types/jest": "^27.4.1",

rollup/bundleHelpers.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
makeBrowserBuildPlugin,
1111
makeCommonJSPlugin,
1212
makeIsDebugBuildPlugin,
13+
makeRrwebBuildPlugin,
1314
makeLicensePlugin,
1415
makeNodeResolvePlugin,
1516
makeCleanupPlugin,
@@ -34,6 +35,11 @@ export function makeBaseBundleConfig(options) {
3435
const markAsBrowserBuildPlugin = makeBrowserBuildPlugin(true);
3536
const licensePlugin = makeLicensePlugin(licenseTitle);
3637
const tsPlugin = makeTSPlugin('es5');
38+
const rrwebBuildPlugin = makeRrwebBuildPlugin({
39+
excludeCanvas: false,
40+
excludeIframe: false,
41+
excludeShadowDom: false,
42+
});
3743

3844
// The `commonjs` plugin is the `esModuleInterop` of the bundling world. When used with `transformMixedEsModules`, it
3945
// will include all dependencies, imported or required, in the final bundle. (Without it, CJS modules aren't included
@@ -51,7 +57,7 @@ export function makeBaseBundleConfig(options) {
5157
},
5258
},
5359
context: 'window',
54-
plugins: [markAsBrowserBuildPlugin],
60+
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
5561
};
5662

5763
// used by `@sentry/integrations` and `@sentry/wasm` (bundles which need to be combined with a stand-alone SDK bundle)
@@ -84,7 +90,7 @@ export function makeBaseBundleConfig(options) {
8490
// code to add after the CJS wrapper
8591
footer: '}(window));',
8692
},
87-
plugins: [markAsBrowserBuildPlugin],
93+
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
8894
};
8995

9096
// used by `@sentry/serverless`, when creating the lambda layer

rollup/npmHelpers.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
makeNodeResolvePlugin,
1313
makeCleanupPlugin,
1414
makeSucrasePlugin,
15+
makeRrwebBuildPlugin,
1516
makeDebugBuildStatementReplacePlugin,
1617
makeSetSDKSourcePlugin,
1718
} from './plugins/index.js';
@@ -34,6 +35,11 @@ export function makeBaseNPMConfig(options = {}) {
3435
const cleanupPlugin = makeCleanupPlugin();
3536
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
3637
const setSdkSourcePlugin = makeSetSDKSourcePlugin('npm');
38+
const rrwebBuildPlugin = makeRrwebBuildPlugin({
39+
excludeCanvas: undefined,
40+
excludeShadowDom: undefined,
41+
excludeIframe: undefined,
42+
});
3743

3844
const defaultBaseConfig = {
3945
input: entrypoints,
@@ -84,7 +90,14 @@ export function makeBaseNPMConfig(options = {}) {
8490
interop: esModuleInterop ? 'auto' : 'esModule',
8591
},
8692

87-
plugins: [nodeResolvePlugin, setSdkSourcePlugin, sucrasePlugin, debugBuildStatementReplacePlugin, cleanupPlugin],
93+
plugins: [
94+
nodeResolvePlugin,
95+
setSdkSourcePlugin,
96+
sucrasePlugin,
97+
debugBuildStatementReplacePlugin,
98+
rrwebBuildPlugin,
99+
cleanupPlugin,
100+
],
88101

89102
// don't include imported modules from outside the package in the final output
90103
external: [

rollup/plugins/npmPlugins.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,33 @@ export function makeDebugBuildStatementReplacePlugin() {
105105
});
106106
}
107107

108+
/**
109+
* Creates a plugin to replace build flags of rrweb with either a constant (if passed true/false) or with a safe statement that:
110+
* a) evaluates to `true`
111+
* b) can easily be modified by our users' bundlers to evaluate to false, facilitating the treeshaking of logger code.
112+
*
113+
* When `undefined` is passed,
114+
* end users can define e.g. `__SENTRY_EXCLUDE_CANVAS__` in their bundler to shake out canvas specific rrweb code.
115+
*/
116+
export function makeRrwebBuildPlugin({ excludeCanvas, excludeShadowDom, excludeIframe } = {}) {
117+
const values = {};
118+
119+
if (typeof excludeCanvas === 'boolean') {
120+
values['__RRWEB_EXCLUDE_CANVAS__'] = excludeCanvas;
121+
}
122+
123+
if (typeof excludeShadowDom === 'boolean') {
124+
values['__RRWEB_EXCLUDE_SHADOW_DOM__'] = excludeShadowDom;
125+
}
126+
127+
if (typeof excludeIframe === 'boolean') {
128+
values['__RRWEB_EXCLUDE_IFRAME__'] = excludeIframe;
129+
}
130+
131+
return replace({
132+
preventAssignment: true,
133+
values,
134+
});
135+
}
136+
108137
export { makeExtractPolyfillsPlugin } from './extractPolyfillsPlugin.js';

0 commit comments

Comments
 (0)