Skip to content

Commit 66c2d32

Browse files
committed
move iife-mimicking code from add-on bundles' config into central config
1 parent 2b2167d commit 66c2d32

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

packages/integrations/rollup.config.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import typescript from 'rollup-plugin-typescript2';
55
import resolve from '@rollup/plugin-node-resolve';
66
import commonjs from '@rollup/plugin-commonjs';
77

8-
import { baseBundleConfig, markAsBrowserBuild, paths } from '../../rollup.config';
8+
import { addOnBundleConfig, baseBundleConfig, markAsBrowserBuild, paths } from '../../rollup.config';
99

1010
const terserInstance = terser({
1111
mangle: {
@@ -42,18 +42,6 @@ const plugins = [
4242
commonjs(),
4343
];
4444

45-
function mergeIntoSentry() {
46-
return `
47-
__window.Sentry = __window.Sentry || {};
48-
__window.Sentry.Integrations = __window.Sentry.Integrations || {};
49-
for (var key in exports) {
50-
if (Object.prototype.hasOwnProperty.call(exports, key)) {
51-
__window.Sentry.Integrations[key] = exports[key];
52-
}
53-
}
54-
`;
55-
}
56-
5745
function allIntegrations() {
5846
return fs.readdirSync('./src').filter(file => file != 'index.ts');
5947
}
@@ -75,13 +63,9 @@ function loadAllIntegrations() {
7563
...baseBundleConfig,
7664
input: `src/${file}`,
7765
output: {
78-
banner: '(function (__window) {',
79-
intro: 'var exports = {};',
80-
outro: mergeIntoSentry(),
81-
footer: '}(window));',
8266
...baseBundleConfig.output,
67+
...addOnBundleConfig.output,
8368
file: `build/${file.replace('.ts', build.extension)}`,
84-
format: 'cjs',
8569
},
8670
plugins: build.plugins,
8771
})),

packages/wasm/rollup.config.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typescript from 'rollup-plugin-typescript2';
22
import resolve from '@rollup/plugin-node-resolve';
33

4-
import { baseBundleConfig, paths, markAsBrowserBuild, terserPlugin } from '../../rollup.config';
4+
import { addOnBundleConfig, baseBundleConfig, paths, markAsBrowserBuild, terserPlugin } from '../../rollup.config';
55

66
const plugins = [
77
typescript({
@@ -23,18 +23,6 @@ const plugins = [
2323
}),
2424
];
2525

26-
function mergeIntoSentry() {
27-
return `
28-
__window.Sentry = __window.Sentry || {};
29-
__window.Sentry.Integrations = __window.Sentry.Integrations || {};
30-
for (var key in exports) {
31-
if (Object.prototype.hasOwnProperty.call(exports, key)) {
32-
__window.Sentry.Integrations[key] = exports[key];
33-
}
34-
}
35-
`;
36-
}
37-
3826
function loadAllIntegrations() {
3927
const builds = [];
4028
[
@@ -51,13 +39,9 @@ function loadAllIntegrations() {
5139
...baseBundleConfig,
5240
input: `src/index.ts`,
5341
output: {
54-
banner: '(function (__window) {',
55-
intro: 'var exports = {};',
56-
outro: mergeIntoSentry(),
57-
footer: '}(window));',
5842
...baseBundleConfig.output,
43+
...addOnBundleConfig.output,
5944
file: `build/wasm${build.extension}`,
60-
format: 'cjs',
6145
},
6246
plugins: build.plugins,
6347
});

rollup.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,34 @@ export const baseBundleConfig = {
6767
},
6868
treeshake: 'smallest',
6969
};
70+
71+
export const addOnBundleConfig = {
72+
// These output settings are designed to mimic an IIFE. We don't use Rollup's `iife` format because we don't want to
73+
// attach this code to a new global variable, but rather inject it into the existing SDK's `Integrations` object.
74+
output: {
75+
format: 'cjs',
76+
77+
// code to add before the CJS wrapper
78+
banner: '(function (__window) {',
79+
80+
// code to add just inside the CJS wrapper, before any of the wrapped code
81+
intro: 'var exports = {};',
82+
83+
// code to add after all of the wrapped code, but still inside the CJS wrapper
84+
outro: () =>
85+
[
86+
'',
87+
" // Add this module's exports to the global `Sentry.Integrations`",
88+
' __window.Sentry = __window.Sentry || {};',
89+
' __window.Sentry.Integrations = __window.Sentry.Integrations || {};',
90+
' for (var key in exports) {',
91+
' if (Object.prototype.hasOwnProperty.call(exports, key)) {',
92+
' __window.Sentry.Integrations[key] = exports[key];',
93+
' }',
94+
' }',
95+
].join('\n'),
96+
97+
// code to add after the CJS wrapper
98+
footer: '}(window));',
99+
},
100+
};

0 commit comments

Comments
 (0)