Skip to content

Commit af7081c

Browse files
authored
fet(build): Create debug versions of minified bundles (#4699)
This adds to our base CDN rollup config the ability to create bundles which are minified but nonetheless contain debug logging. Notable changes: - Setting the constant (`__SENTRY_DEBUG__`) which controls whether or not to include logging is now done by a separate rollup plugin rather than by terser (which was setting the now-obsolete `__SENTRY_NO_DEBUG__` flag). This allows rollup to do the treeshaking, which is good because it lets us create a _non_-minified no-debug bundle. Though such a bundle doesn't make a lot of sense to publish (why strip the logging if you’re not also going to minify the code?), it _is_ very helpful for us, because it allows us to see what's being treeshaken in the context of human-readable code. - The scope of the helper function `makeMinificationVariants` has broadened to include the creation of all bundle variants*. As a result, it no longer takes an array of configs (which it might have gotten had there ever been a `makeJSVersionVariants` or `makeDebugVariants` whose results could have been passed to `makeMinificationVariants`) but instead takes a single base config which it then spins out into all necessary flavors. This broadening of scope is also now reflected in variable names and the function name itself. * This is not quite true, in that it's still not handling the ES5/ES6 question. I chose not to incorporate that because as soon as we go ES6-only in v7, it will no longer be an issue for any package except perhaps the browser package.
1 parent 1124961 commit af7081c

File tree

6 files changed

+77
-52
lines changed

6 files changed

+77
-52
lines changed

packages/browser/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseBundleConfig, makeMinificationVariants } from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeConfigVariants } from '../../rollup.config';
22

33
const builds = [];
44

@@ -11,7 +11,7 @@ const builds = [];
1111
outputFileBase: `build/bundle${jsVersion === 'es6' ? '.es6' : ''}`,
1212
});
1313

14-
builds.push(...makeMinificationVariants(baseBundleConfig));
14+
builds.push(...makeConfigVariants(baseBundleConfig));
1515
});
1616

1717
export default builds;

packages/integrations/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22

33
import commonjs from '@rollup/plugin-commonjs';
44

5-
import { insertAt, makeBaseBundleConfig, makeMinificationVariants } from '../../rollup.config';
5+
import { insertAt, makeBaseBundleConfig, makeConfigVariants } from '../../rollup.config';
66

77
const builds = [];
88

@@ -20,7 +20,7 @@ integrationSourceFiles.forEach(file => {
2020
// TODO We only need `commonjs` for localforage (used in the offline plugin). Once that's fixed, this can come out.
2121
baseBundleConfig.plugins = insertAt(baseBundleConfig.plugins, -2, commonjs());
2222

23-
builds.push(...makeMinificationVariants(baseBundleConfig));
23+
builds.push(...makeConfigVariants(baseBundleConfig));
2424
});
2525

2626
export default builds;

packages/tracing/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseBundleConfig, makeMinificationVariants } from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeConfigVariants } from '../../rollup.config';
22

33
const builds = [];
44

@@ -11,7 +11,7 @@ const builds = [];
1111
outputFileBase: `build/bundle.tracing${jsVersion === 'es6' ? '.es6' : ''}`,
1212
});
1313

14-
builds.push(...makeMinificationVariants(baseBundleConfig));
14+
builds.push(...makeConfigVariants(baseBundleConfig));
1515
});
1616

1717
export default builds;

packages/vue/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseBundleConfig, makeMinificationVariants } from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeConfigVariants } from '../../rollup.config';
22

33
const baseBundleConfig = makeBaseBundleConfig({
44
input: 'src/index.bundle.ts',
@@ -8,4 +8,4 @@ const baseBundleConfig = makeBaseBundleConfig({
88
outputFileBase: 'build/bundle.vue',
99
});
1010

11-
export default makeMinificationVariants(baseBundleConfig);
11+
export default makeConfigVariants(baseBundleConfig);

packages/wasm/rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeBaseBundleConfig, makeMinificationVariants } from '../../rollup.config';
1+
import { makeBaseBundleConfig, makeConfigVariants } from '../../rollup.config';
22

33
const baseBundleConfig = makeBaseBundleConfig({
44
input: 'src/index.ts',
@@ -8,4 +8,4 @@ const baseBundleConfig = makeBaseBundleConfig({
88
outputFileBase: 'build/wasm',
99
});
1010

11-
export default makeMinificationVariants(baseBundleConfig);
11+
export default makeConfigVariants(baseBundleConfig);

rollup.config.js

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import typescript from 'rollup-plugin-typescript2';
2020
const getLastElement = array => {
2121
return array[array.length - 1];
2222
};
23-
export const insertAt = (arr, index, insertee) => {
23+
export const insertAt = (arr, index, ...insertees) => {
2424
const newArr = [...arr];
2525
// Add 1 to the array length so that the inserted element ends up in the right spot with respect to the length of the
2626
// new array (which will be one element longer), rather than that of the current array
2727
const destinationIndex = index >= 0 ? index : arr.length + 1 + index;
28-
newArr.splice(destinationIndex, 0, insertee);
28+
newArr.splice(destinationIndex, 0, ...insertees);
2929
return newArr;
3030
};
3131

@@ -46,14 +46,18 @@ function makeLicensePlugin(title) {
4646
});
4747
}
4848

49-
export const terserPlugin = terser({
50-
compress: {
51-
// Tell env.ts that we're building a browser bundle and that we do not
52-
// want to have unnecessary debug functionality.
53-
global_defs: {
54-
__SENTRY_NO_DEBUG__: false,
49+
function makeIsDebugBuildPlugin(includeDebugging) {
50+
return replace({
51+
// don't turn `const __SENTRY_DEBUG__ = false` into `const false = false`
52+
preventAssignment: true,
53+
// everywhere else, replace it with the value of `includeDebugging`
54+
values: {
55+
__SENTRY_DEBUG__: includeDebugging,
5556
},
56-
},
57+
});
58+
}
59+
60+
export const terserPlugin = terser({
5761
mangle: {
5862
// captureExceptions and captureMessage are public API methods and they don't need to be listed here
5963
// as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version.
@@ -190,45 +194,66 @@ export function makeBaseBundleConfig(options) {
190194
return deepMerge(sharedBundleConfig, isAddOn ? addOnBundleConfig : standAloneBundleConfig);
191195
}
192196

193-
export function makeMinificationVariants(existingConfigs) {
194-
const newConfigs = [];
195-
196-
// ensure we've got an array of configs rather than a single config
197-
existingConfigs = Array.isArray(existingConfigs) ? existingConfigs : [existingConfigs];
197+
/**
198+
* Takes the CDN rollup config for a given package and produces three versions of it:
199+
* - non-minified, including debug logging,
200+
* - minified, including debug logging,
201+
* - minified, with debug logging stripped
202+
*
203+
* @param baseConfig The rollup config shared by the entire package
204+
* @returns An array of versions of that config
205+
*/
206+
export function makeConfigVariants(baseConfig) {
207+
const configVariants = [];
198208

199-
existingConfigs.forEach(existingConfig => {
200-
const { plugins } = existingConfig;
209+
const { plugins } = baseConfig;
210+
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
211+
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
201212

202-
// The license plugin has to be last, so it ends up after terser. Otherwise, terser will remove the license banner.
203-
assert(
204-
getLastElement(plugins).name === 'rollup-plugin-license',
205-
`Last plugin in given options should be \`rollup-plugin-license\`. Found ${getLastElement(plugins).name}`,
206-
);
213+
// The license plugin has to be last, so it ends up after terser. Otherwise, terser will remove the license banner.
214+
assert(
215+
getLastElement(plugins).name === 'rollup-plugin-license',
216+
`Last plugin in given options should be \`rollup-plugin-license\`. Found ${getLastElement(plugins).name}`,
217+
);
207218

208-
const bundleVariants = [
209-
{
210-
output: {
211-
file: `${existingConfig.output.file}.js`,
212-
},
213-
plugins,
219+
// The additional options to use for each variant we're going to create
220+
const variantSpecificConfigs = [
221+
{
222+
output: {
223+
file: `${baseConfig.output.file}.js`,
214224
},
215-
{
216-
output: {
217-
file: `${existingConfig.output.file}.min.js`,
218-
},
219-
plugins: insertAt(plugins, -2, terserPlugin),
225+
plugins: insertAt(plugins, -2, includeDebuggingPlugin),
226+
},
227+
// This variant isn't particularly helpful for an SDK user, as it strips logging while making no other minification
228+
// changes, so by default we don't create it. It is however very useful when debugging rollup's treeshaking, so it's
229+
// left here for that purpose.
230+
// {
231+
// output: { file: `${baseConfig.output.file}.no-debug.js`,
232+
// },
233+
// plugins: insertAt(plugins, -2, stripDebuggingPlugin),
234+
// },
235+
{
236+
output: {
237+
file: `${baseConfig.output.file}.min.js`,
238+
},
239+
plugins: insertAt(plugins, -2, stripDebuggingPlugin, terserPlugin),
240+
},
241+
{
242+
output: {
243+
file: `${baseConfig.output.file}.debug.min.js`,
220244
},
221-
];
222-
223-
bundleVariants.forEach(variant => {
224-
const mergedConfig = deepMerge(existingConfig, variant, {
225-
// this makes it so that instead of concatenating the `plugin` properties of the two objects, the first value is
226-
// just overwritten by the second value
227-
arrayMerge: (first, second) => second,
228-
});
229-
newConfigs.push(mergedConfig);
245+
plugins: insertAt(plugins, -2, includeDebuggingPlugin, terserPlugin),
246+
},
247+
];
248+
249+
variantSpecificConfigs.forEach(variant => {
250+
const mergedConfig = deepMerge(baseConfig, variant, {
251+
// this makes it so that instead of concatenating the `plugin` properties of the two objects, the first value is
252+
// just overwritten by the second value
253+
arrayMerge: (first, second) => second,
230254
});
255+
configVariants.push(mergedConfig);
231256
});
232257

233-
return newConfigs;
258+
return configVariants;
234259
}

0 commit comments

Comments
 (0)