Skip to content

Commit 995928b

Browse files
authored
ref(build): Programmatically generate base CDN rollup config (#4650)
As part of the new build process, this continues the work centralizing the rollup config for our CDN bundles. More specifically, this creates a function to generate the base configs for `@sentry/tracing`, `@sentry/vue`, and `@sentry/wasm`, and changes each of their respective rollup configs to use it. As expected with a pure refactor, there are no bundle changes. Not included in this PR are `@sentry/browser` and `@sentry/integrations`. Because their respective rollup configs are more complicated, they'll be handled in future PRs. As a result, code which will, once those are handled, replace much of the code in the repo-level `rollup.config.js` has instead been added, so as to not break their current build processes. Note that the added dev dependency doesn't actually change `yarn.lock`, because `deepmerge` is already included, as a transitive dependency.
1 parent 4df9f7f commit 995928b

File tree

5 files changed

+104
-97
lines changed

5 files changed

+104
-97
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@types/sinon": "^7.0.11",
6464
"chai": "^4.1.2",
6565
"codecov": "^3.6.5",
66+
"deepmerge": "^4.2.2",
6667
"eslint": "7.32.0",
6768
"jest": "^24.9.0",
6869
"karma-browserstack-launcher": "^1.5.1",

packages/tracing/rollup.config.js

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,29 @@
1-
import {
2-
baseBundleConfig,
3-
makeLicensePlugin,
4-
markAsBrowserBuild,
5-
nodeResolvePlugin,
6-
terserPlugin,
7-
typescriptPluginES5,
8-
} from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeLicensePlugin, terserPlugin } from '../../rollup.config';
92

103
const licensePlugin = makeLicensePlugin('@sentry/tracing & @sentry/browser');
114

12-
const plugins = [
13-
typescriptPluginES5,
14-
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
15-
markAsBrowserBuild,
16-
nodeResolvePlugin,
17-
licensePlugin,
18-
];
19-
20-
const bundleConfig = {
21-
...baseBundleConfig,
22-
input: 'src/index.ts',
23-
output: {
24-
...baseBundleConfig.output,
25-
format: 'iife',
26-
name: 'Sentry',
27-
},
28-
context: 'window',
29-
plugins,
30-
};
5+
const baseBundleConfig = makeBaseBundleConfig({
6+
input: 'src/index.bundle.ts',
7+
isAddOn: false,
8+
outputFileBase: 'build/bundle.tracing',
9+
});
3110

3211
export default [
3312
// ES5 Browser Tracing Bundle
3413
{
35-
...bundleConfig,
36-
input: 'src/index.bundle.ts',
14+
...baseBundleConfig,
3715
output: {
38-
...bundleConfig.output,
39-
file: 'build/bundle.tracing.js',
16+
...baseBundleConfig.output,
17+
file: `${baseBundleConfig.output.file}.js`,
4018
},
41-
plugins: bundleConfig.plugins,
19+
plugins: [...baseBundleConfig.plugins, licensePlugin],
4220
},
4321
{
44-
...bundleConfig,
45-
input: 'src/index.bundle.ts',
22+
...baseBundleConfig,
4623
output: {
47-
...bundleConfig.output,
48-
file: 'build/bundle.tracing.min.js',
24+
...baseBundleConfig.output,
25+
file: `${baseBundleConfig.output.file}.min.js`,
4926
},
50-
// Uglify has to be at the end of compilation, BUT before the license banner
51-
plugins: bundleConfig.plugins.slice(0, -1).concat(terserPlugin).concat(bundleConfig.plugins.slice(-1)),
27+
plugins: [...baseBundleConfig.plugins, terserPlugin, licensePlugin],
5228
},
5329
];

packages/vue/rollup.config.js

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,28 @@
1-
import {
2-
baseBundleConfig,
3-
makeLicensePlugin,
4-
markAsBrowserBuild,
5-
nodeResolvePlugin,
6-
terserPlugin,
7-
typescriptPluginES5,
8-
} from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeLicensePlugin, terserPlugin } from '../../rollup.config';
92

103
const licensePlugin = makeLicensePlugin();
114

12-
const plugins = [
13-
typescriptPluginES5,
14-
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
15-
markAsBrowserBuild,
16-
nodeResolvePlugin,
17-
licensePlugin,
18-
];
19-
20-
const bundleConfig = {
21-
...baseBundleConfig,
22-
input: 'src/index.ts',
23-
output: {
24-
...baseBundleConfig.output,
25-
format: 'iife',
26-
name: 'Sentry',
27-
},
28-
context: 'window',
29-
plugins,
30-
};
5+
const baseBundleConfig = makeBaseBundleConfig({
6+
input: 'src/index.bundle.ts',
7+
isAddOn: false,
8+
outputFileBase: 'build/bundle.vue',
9+
});
3110

3211
export default [
33-
// ES5 Browser Tracing Bundle
3412
{
35-
...bundleConfig,
36-
input: 'src/index.bundle.ts',
13+
...baseBundleConfig,
3714
output: {
38-
...bundleConfig.output,
39-
file: 'build/bundle.vue.js',
15+
...baseBundleConfig.output,
16+
file: `${baseBundleConfig.output.file}.js`,
4017
},
41-
plugins: bundleConfig.plugins,
18+
plugins: [...baseBundleConfig.plugins, licensePlugin],
4219
},
4320
{
44-
...bundleConfig,
45-
input: 'src/index.bundle.ts',
21+
...baseBundleConfig,
4622
output: {
47-
...bundleConfig.output,
48-
file: 'build/bundle.vue.min.js',
23+
...baseBundleConfig.output,
24+
file: `${baseBundleConfig.output.file}.min.js`,
4925
},
50-
// Uglify has to be at the end of compilation, BUT before the license banner
51-
plugins: bundleConfig.plugins.slice(0, -1).concat(terserPlugin).concat(bundleConfig.plugins.slice(-1)),
26+
plugins: [...baseBundleConfig.plugins, terserPlugin, licensePlugin],
5227
},
5328
];

packages/wasm/rollup.config.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
1-
import {
2-
addOnBundleConfig,
3-
baseBundleConfig,
4-
markAsBrowserBuild,
5-
nodeResolvePlugin,
6-
terserPlugin,
7-
typescriptPluginES5,
8-
} from '../../rollup.config';
1+
import { makeBaseBundleConfig, terserPlugin } from '../../rollup.config';
92

10-
const plugins = [
11-
typescriptPluginES5,
12-
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
13-
markAsBrowserBuild,
14-
nodeResolvePlugin,
15-
];
3+
const baseBundleConfig = makeBaseBundleConfig({
4+
input: 'src/index.ts',
5+
isAddOn: true,
6+
outputFileBase: 'build/wasm',
7+
});
168

179
function loadAllIntegrations() {
1810
const builds = [];
1911
[
2012
{
2113
extension: '.js',
22-
plugins,
14+
plugins: baseBundleConfig.plugins,
2315
},
2416
{
2517
extension: '.min.js',
26-
plugins: [...plugins, terserPlugin],
18+
plugins: [...baseBundleConfig.plugins, terserPlugin],
2719
},
2820
].forEach(build => {
2921
builds.push({
3022
...baseBundleConfig,
31-
input: `src/index.ts`,
3223
output: {
3324
...baseBundleConfig.output,
34-
...addOnBundleConfig.output,
35-
file: `build/wasm${build.extension}`,
25+
file: `${baseBundleConfig.output.file}${build.extension}`,
3626
},
3727
plugins: build.plugins,
3828
});

rollup.config.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/**
2-
* Shared config used by individual packages' Rollup configs
2+
* Shared config used by individual packages' Rollup configs. Items here come in three flavors:
3+
* - stand-alone: used by `@sentry/browser`, `@sentry/tracing`, and `@sentry/vue` (bundles which are a full SDK in
4+
* and of themselves)
5+
* - add-on: used by `@sentry/integrations` and `@sentry/wasm` (bundles which need to be combined with a stand-alone
6+
* SDK bundle)
7+
* - shared: used by both types of bundles
8+
*
39
*/
410

11+
import deepMerge from 'deepmerge';
512
import license from 'rollup-plugin-license';
613
import resolve from '@rollup/plugin-node-resolve';
714
import replace from '@rollup/plugin-replace';
@@ -115,3 +122,61 @@ export const addOnBundleConfig = {
115122
footer: '}(window));',
116123
},
117124
};
125+
126+
export function makeBaseBundleConfig(options) {
127+
const { input, isAddOn, outputFileBase } = options;
128+
129+
const standAloneBundleConfig = {
130+
output: {
131+
format: 'iife',
132+
name: 'Sentry',
133+
},
134+
context: 'window',
135+
};
136+
137+
const addOnBundleConfig = {
138+
// These output settings are designed to mimic an IIFE. We don't use Rollup's `iife` format because we don't want to
139+
// attach this code to a new global variable, but rather inject it into the existing SDK's `Integrations` object.
140+
output: {
141+
format: 'cjs',
142+
143+
// code to add before the CJS wrapper
144+
banner: '(function (__window) {',
145+
146+
// code to add just inside the CJS wrapper, before any of the wrapped code
147+
intro: 'var exports = {};',
148+
149+
// code to add after all of the wrapped code, but still inside the CJS wrapper
150+
outro: () =>
151+
[
152+
'',
153+
" // Add this module's exports to the global `Sentry.Integrations`",
154+
' __window.Sentry = __window.Sentry || {};',
155+
' __window.Sentry.Integrations = __window.Sentry.Integrations || {};',
156+
' for (var key in exports) {',
157+
' if (Object.prototype.hasOwnProperty.call(exports, key)) {',
158+
' __window.Sentry.Integrations[key] = exports[key];',
159+
' }',
160+
' }',
161+
].join('\n'),
162+
163+
// code to add after the CJS wrapper
164+
footer: '}(window));',
165+
},
166+
};
167+
168+
const sharedBundleConfig = {
169+
input,
170+
output: {
171+
// a file extension will be added to this base value when we specify either a minified or non-minified build
172+
file: outputFileBase,
173+
sourcemap: true,
174+
strict: false,
175+
esModule: false,
176+
},
177+
plugins: [typescriptPluginES5, markAsBrowserBuild, nodeResolvePlugin],
178+
treeshake: 'smallest',
179+
};
180+
181+
return deepMerge(sharedBundleConfig, isAddOn ? addOnBundleConfig : standAloneBundleConfig);
182+
}

0 commit comments

Comments
 (0)