Skip to content

ref(build): Centralize identical CDN rollup config #4637

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 11 commits into from
Feb 28, 2022
Merged
78 changes: 19 additions & 59 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import license from 'rollup-plugin-license';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';

const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
.trim();
import {
baseBundleConfig,
makeLicensePlugin,
markAsBrowserBuild,
nodeResolvePlugin,
paths,
typescriptPluginES5,
} from '../../rollup.config';

const licensePlugin = makeLicensePlugin();

const terserInstance = terser({
compress: {
Expand Down Expand Up @@ -37,60 +41,23 @@ const terserInstance = terser({
},
});

const paths = {
'@sentry/utils': ['../utils/src'],
'@sentry/core': ['../core/src'],
'@sentry/hub': ['../hub/src'],
'@sentry/types': ['../types/src'],
'@sentry/minimal': ['../minimal/src'],
};

const plugins = [
typescript({
tsconfig: 'tsconfig.esm.json',
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
paths,
baseUrl: '.',
},
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
replace({
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
// syntax error)
preventAssignment: true,
// the replacements to make
values: {
__SENTRY_BROWSER_BUNDLE__: true,
},
}),
resolve({
mainFields: ['module'],
}),
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',
sourcemap: true,
strict: false,
esModule: false,
},
context: 'window',
plugins: [
...plugins,
license({
sourcemap: true,
banner: `/*! @sentry/browser <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
}),
],
treeshake: 'smallest',
plugins: [...plugins, licensePlugin],
};

export default [
Expand All @@ -109,10 +76,7 @@ export default [
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)),
plugins: bundleConfig.plugins.slice(0, -1).concat(terserInstance).concat(bundleConfig.plugins.slice(-1)),
},
// ------------------
// ES6 Browser Bundle
Expand Down Expand Up @@ -159,11 +123,7 @@ export default [
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
...plugins
.slice(1)
.slice(0, -1)
.concat(terserInstance)
.concat(bundleConfig.plugins.slice(-1)),
...plugins.slice(1).slice(0, -1).concat(terserInstance).concat(bundleConfig.plugins.slice(-1)),
],
},
// ------------------
Expand Down
70 changes: 15 additions & 55 deletions packages/integrations/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as fs from 'fs';

import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';

import {
addOnBundleConfig,
baseBundleConfig,
markAsBrowserBuild,
nodeResolvePlugin,
typescriptPluginES5,
} from '../../rollup.config';

const terserInstance = terser({
mangle: {
Expand All @@ -21,52 +26,13 @@ const terserInstance = terser({
});

const plugins = [
typescript({
tsconfig: 'tsconfig.esm.json',
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
paths: {
'@sentry/utils': ['../utils/src'],
'@sentry/core': ['../core/src'],
'@sentry/hub': ['../hub/src'],
'@sentry/types': ['../types/src'],
'@sentry/minimal': ['../minimal/src'],
},
baseUrl: '.',
},
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
replace({
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
// syntax error)
preventAssignment: true,
// the replacements to make
values: {
__SENTRY_BROWSER_BUNDLE__: true,
},
}),
resolve({
mainFields: ['module'],
}),
typescriptPluginES5,
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
markAsBrowserBuild,
nodeResolvePlugin,
commonjs(),
];

function mergeIntoSentry() {
return `
__window.Sentry = __window.Sentry || {};
__window.Sentry.Integrations = __window.Sentry.Integrations || {};
for (var key in exports) {
if (Object.prototype.hasOwnProperty.call(exports, key)) {
__window.Sentry.Integrations[key] = exports[key];
}
}
`;
}

function allIntegrations() {
return fs.readdirSync('./src').filter(file => file != 'index.ts');
}
Expand All @@ -85,20 +51,14 @@ function loadAllIntegrations() {
].forEach(build => {
builds.push(
...allIntegrations().map(file => ({
...baseBundleConfig,
input: `src/${file}`,
output: {
banner: '(function (__window) {',
intro: 'var exports = {};',
outro: mergeIntoSentry(),
footer: '}(window));',
...baseBundleConfig.output,
...addOnBundleConfig.output,
file: `build/${file.replace('.ts', build.extension)}`,
format: 'cjs',
sourcemap: true,
strict: false,
esModule: false,
},
plugins: build.plugins,
treeshake: 'smallest',
})),
);
});
Expand Down
91 changes: 18 additions & 73 deletions packages/tracing/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,32 @@
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import license from 'rollup-plugin-license';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import {
baseBundleConfig,
makeLicensePlugin,
markAsBrowserBuild,
nodeResolvePlugin,
terserPlugin,
typescriptPluginES5,
} from '../../rollup.config';

const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
.trim();

const terserInstance = terser({
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 sake, as they are all used in the frames manipulation process.
reserved: ['captureException', 'captureMessage', 'sentryWrapped'],
properties: {
regex: /^_[^_]/,
},
},
output: {
comments: false,
},
});

const paths = {
'@sentry/utils': ['../utils/src'],
'@sentry/core': ['../core/src'],
'@sentry/hub': ['../hub/src'],
'@sentry/types': ['../types/src'],
'@sentry/minimal': ['../minimal/src'],
'@sentry/browser': ['../browser/src'],
};
const licensePlugin = makeLicensePlugin('@sentry/tracing & @sentry/browser');

const plugins = [
typescript({
tsconfig: 'tsconfig.esm.json',
tsconfigOverride: {
compilerOptions: {
declaration: false,
declarationMap: false,
paths,
baseUrl: '.',
},
},
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
}),
replace({
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
// syntax error)
preventAssignment: true,
// the replacements to make
values: {
__SENTRY_BROWSER_BUNDLE__: true,
},
}),
resolve({
mainFields: ['module'],
}),
typescriptPluginES5,
// replace `__SENTRY_BROWSER_BUNDLE__` with `true` to enable treeshaking of non-browser code
markAsBrowserBuild,
nodeResolvePlugin,
licensePlugin,
];

const bundleConfig = {
...baseBundleConfig,
input: 'src/index.ts',
output: {
...baseBundleConfig.output,
format: 'iife',
name: 'Sentry',
sourcemap: true,
strict: false,
esModule: false,
},
context: 'window',
plugins: [
...plugins,
license({
sourcemap: true,
banner: `/*! @sentry/tracing & @sentry/browser <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
}),
],
treeshake: 'smallest',
plugins,
};

export default [
Expand All @@ -100,9 +48,6 @@ export default [
file: 'build/bundle.tracing.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)),
plugins: bundleConfig.plugins.slice(0, -1).concat(terserPlugin).concat(bundleConfig.plugins.slice(-1)),
},
];
Loading