Skip to content

ref(build): Use rollup config function for @sentry/browser #4668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 29 additions & 122 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,130 +1,37 @@
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import { makeBaseBundleConfig, makeLicensePlugin, terserPlugin } from '../../rollup.config';

import {
baseBundleConfig,
makeLicensePlugin,
markAsBrowserBuild,
nodeResolvePlugin,
paths,
typescriptPluginES5,
} from '../../rollup.config';
const builds = [];

const licensePlugin = makeLicensePlugin();

const terserInstance = terser({
compress: {
// Tell env.ts that we're building a browser bundle and that we do not
// want to have unnecessary debug functionality.
global_defs: {
__SENTRY_NO_DEBUG__: false,
},
},
mangle: {
// captureExceptions and captureMessage are public API methods and they don't need to be listed here
// as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version.
// We need those full names to correctly detect our internal frames for stripping.
// I listed all of them here just for the clarity's sake, as they are all used in the frame-manipulation process.
reserved: ['captureException', 'captureMessage', 'sentryWrapped'],
properties: {
regex: /^_[^_]/,
// This exclusion prevents most of the occurrences of the bug linked below:
// https://github.com/getsentry/sentry-javascript/issues/2622
// The bug is caused by multiple SDK instances, where one is minified and one is using non-mangled code.
// Unfortunatelly we cannot fix it reliably (thus `typeof` check in the `InboundFilters` code),
// as we cannot force people using multiple instances in their apps to sync SDK versions.
reserved: ['_mergeOptions'],
},
},
output: {
comments: false,
},
});

const plugins = [
typescriptPluginES5,
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
markAsBrowserBuild,
nodeResolvePlugin,
];

const bundleConfig = {
...baseBundleConfig,
input: 'src/index.ts',
output: {
...baseBundleConfig.output,
format: 'iife',
name: 'Sentry',
},
context: 'window',
plugins: [...plugins, licensePlugin],
};
['es5', 'es6'].forEach(jsVersion => {
const baseBundleConfig = makeBaseBundleConfig({
input: 'src/index.ts',
isAddOn: false,
jsVersion,
outputFileBase: `build/bundle${jsVersion === 'es6' ? '.es6' : ''}`,
});

export default [
// ES5 Browser Bundle
{
...bundleConfig,
output: {
...bundleConfig.output,
file: 'build/bundle.js',
},
},
{
...bundleConfig,
output: {
...bundleConfig.output,
file: 'build/bundle.min.js',
},
// Uglify has to be at the end of compilation, BUT before the license banner
plugins: bundleConfig.plugins.slice(0, -1).concat(terserInstance).concat(bundleConfig.plugins.slice(-1)),
},
// ------------------
// ES6 Browser Bundle
{
...bundleConfig,
output: {
...bundleConfig.output,
file: 'build/bundle.es6.js',
},
plugins: [
typescript({
tsconfig: 'tsconfig.esm.json',
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
paths,
baseUrl: '.',
target: 'es6',
},
builds.push(
...[
{
...baseBundleConfig,
output: {
...baseBundleConfig.output,
file: `${baseBundleConfig.output.file}.js`,
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
...plugins.slice(1).concat(bundleConfig.plugins.slice(-1)),
],
},
{
...bundleConfig,
output: {
...bundleConfig.output,
file: 'build/bundle.es6.min.js',
},
plugins: [
typescript({
tsconfig: 'tsconfig.esm.json',
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
paths,
baseUrl: '.',
target: 'es6',
},
plugins: [...baseBundleConfig.plugins, licensePlugin],
},
{
...baseBundleConfig,
output: {
...baseBundleConfig.output,
file: `${baseBundleConfig.output.file}.min.js`,
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
...plugins.slice(1).slice(0, -1).concat(terserInstance).concat(bundleConfig.plugins.slice(-1)),
plugins: [...baseBundleConfig.plugins, terserPlugin, licensePlugin],
},
],
},
// ------------------
];
);
});

export default builds;
7 changes: 7 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export function makeLicensePlugin(title) {
}

export const terserPlugin = terser({
compress: {
// Tell env.ts that we're building a browser bundle and that we do not
// want to have unnecessary debug functionality.
global_defs: {
__SENTRY_NO_DEBUG__: false,
},
},
mangle: {
// captureExceptions and captureMessage are public API methods and they don't need to be listed here
// as mangler doesn't touch user-facing thing, however sentryWrapped is not, and it would be mangled into a minified version.
Expand Down