-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbundlePlugins.mjs
155 lines (141 loc) · 6.1 KB
/
bundlePlugins.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* CommonJS plugin docs: https://github.com/rollup/plugins/tree/master/packages/commonjs
* License plugin docs: https://github.com/mjeanroy/rollup-plugin-license
* Replace plugin docs: https://github.com/rollup/plugins/tree/master/packages/replace
* Resolve plugin docs: https://github.com/rollup/plugins/tree/master/packages/node-resolve
* Terser plugin docs: https://github.com/TrySound/rollup-plugin-terser#options
* Terser docs: https://github.com/terser/terser#api-reference
* Typescript plugin docs: https://github.com/rollup/plugins/tree/master/packages/typescript/#readme
*/
import * as childProcess from 'child_process';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
import license from 'rollup-plugin-license';
/**
* Create a plugin to add an identification banner to the top of stand-alone bundles.
*
* @param title The title to use for the SDK, if not the package name
* @returns An instance of the `rollup-plugin-license` plugin
*/
export function makeLicensePlugin(title) {
const commitHash = childProcess.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim();
const plugin = license({
banner: {
content: `/*! <%= data.title %> <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
data: { title },
},
});
// give it a nicer name for later, when we'll need to sort the plugins
plugin.name = 'license';
return plugin;
}
/**
* Create a plugin to set the value of the `__SENTRY_DEBUG__` magic string.
*
* @param includeDebugging Whether or not the resulting build should include log statements
* @returns An instance of the `@rollup/plugin-replace` plugin to do the replacement of the magic string with `true` or
* 'false`
*/
export function makeIsDebugBuildPlugin(includeDebugging) {
return replace({
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
// `__SENTRY_DEBUG__`, but if we don't give it a value, it will spam with warnings.)
preventAssignment: true,
values: {
// Flags in current package
__DEBUG_BUILD__: includeDebugging,
// Flags in built monorepo dependencies, from which the bundle pulls
__SENTRY_DEBUG__: includeDebugging,
},
});
}
export function makeSetSDKSourcePlugin(sdkSource) {
return replace({
preventAssignment: false,
delimiters: ['', ''],
values: {
'/* __SENTRY_SDK_SOURCE__ */': `return ${JSON.stringify(sdkSource)};`,
},
});
}
/**
* Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string.
*
* @param isBrowserBuild Whether or not the resulting build will be run in the browser
* @returns An instance of the `replace` plugin to do the replacement of the magic string with `true` or 'false`
*/
export function makeBrowserBuildPlugin(isBrowserBuild) {
return replace({
// TODO This will be the default in the next version of the `replace` plugin
preventAssignment: true,
values: {
__SENTRY_BROWSER_BUNDLE__: isBrowserBuild,
},
});
}
// `terser` options reference: https://github.com/terser/terser#api-reference
// `rollup-plugin-terser` options reference: https://github.com/TrySound/rollup-plugin-terser#options
/**
* Create a plugin to perform minification using `terser`.
*
* @returns An instance of the `terser` plugin
*/
export function makeTerserPlugin() {
return terser({
mangle: {
// `captureException` and `captureMessage` are public API methods and they don't need to be listed here, as the
// mangler won't touch user-facing things, but `sentryWrapped` is not user-facing, and would be mangled during
// minification. (We need it in its original form to correctly detect our internal frames for stripping.) All three
// are all listed here just for the clarity's sake, as they are all used in the frames manipulation process.
reserved: ['captureException', 'captureMessage', 'sentryWrapped'],
properties: {
// allow mangling of private field names...
regex: /^_[^_]/,
reserved: [
// ...except for `_experiments`, which we want to remain usable from the outside
'_experiments',
// We want to keep some replay fields unmangled to enable integration tests to access them
'_replay',
'_canvas',
// We also can't mangle rrweb private fields when bundling rrweb in the replay CDN bundles
'_cssText',
// We want to keep the _integrations variable unmangled to send all installed integrations from replay
'_integrations',
// _meta is used to store metadata of replay network events
'_meta',
// We store SDK metadata in the options
'_metadata',
// Object we inject debug IDs into with bundler plugins
'_sentryDebugIds',
// These are used by instrument.ts in utils for identifying HTML elements & events
'_sentryCaptured',
'_sentryId',
// Keeps the frozen DSC on a Sentry Span
'_frozenDsc',
// These are used to keep span & scope relationships
'_sentryRootSpan',
'_sentryChildSpans',
'_sentrySpan',
// require-in-the-middle calls `Module._resolveFilename`. We cannot mangle this (AWS lambda layer bundle).
'_resolveFilename',
// Set on e.g. the shim feedbackIntegration to be able to detect it
'_isShim',
// This is used in metadata integration
'_sentryModuleMetadata',
],
},
},
output: {
comments: false,
},
});
}
// We don't pass these plugins any options which need to be calculated or changed by us, so no need to wrap them in
// another factory function, as they are themselves already factory functions.
export function makeNodeResolvePlugin() {
return nodeResolve();
}
export { commonjs as makeCommonJSPlugin };