-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
bundleHelpers.js
172 lines (149 loc) · 5.98 KB
/
bundleHelpers.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
*/
import { builtinModules } from 'module';
import deepMerge from 'deepmerge';
import {
makeBrowserBuildPlugin,
makeCommonJSPlugin,
makeIsDebugBuildPlugin,
makeLicensePlugin,
makeNodeResolvePlugin,
makeCleanupPlugin,
makeSucrasePlugin,
makeTerserPlugin,
makeTSPlugin,
} from './plugins/index.js';
import { mergePlugins } from './utils';
const BUNDLE_VARIANTS = ['.js', '.min.js', '.debug.min.js'];
export function makeBaseBundleConfig(options) {
const { bundleType, entrypoints, jsVersion, licenseTitle, outputFileBase, packageSpecificConfig } = options;
const nodeResolvePlugin = makeNodeResolvePlugin();
const sucrasePlugin = makeSucrasePlugin();
const cleanupPlugin = makeCleanupPlugin();
const markAsBrowserBuildPlugin = makeBrowserBuildPlugin(true);
const licensePlugin = makeLicensePlugin(licenseTitle);
const tsPlugin = makeTSPlugin(jsVersion.toLowerCase());
// The `commonjs` plugin is the `esModuleInterop` of the bundling world. When used with `transformMixedEsModules`, it
// will include all dependencies, imported or required, in the final bundle. (Without it, CJS modules aren't included
// at all, and without `transformMixedEsModules`, they're only included if they're imported, not if they're required.)
const commonJSPlugin = makeCommonJSPlugin({ transformMixedEsModules: true });
// used by `@sentry/browser`, `@sentry/tracing`, and `@sentry/vue` (bundles which are a full SDK in and of themselves)
const standAloneBundleConfig = {
output: {
format: 'iife',
name: 'Sentry',
},
context: 'window',
plugins: [markAsBrowserBuildPlugin],
};
// used by `@sentry/integrations` and `@sentry/wasm` (bundles which need to be combined with a stand-alone SDK bundle)
const addOnBundleConfig = {
// These output settings are designed to mimic an IIFE. We don't use Rollup's `iife` format because we don't want to
// attach this code to a new global variable, but rather inject it into the existing SDK's `Integrations` object.
output: {
format: 'cjs',
// code to add before the CJS wrapper
banner: '(function (__window) {',
// code to add just inside the CJS wrapper, before any of the wrapped code
intro: 'var exports = {};',
// code to add after all of the wrapped code, but still inside the CJS wrapper
outro: () =>
[
'',
" // Add this module's exports to the global `Sentry.Integrations`",
' __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];',
' }',
' }',
].join('\n'),
// code to add after the CJS wrapper
footer: '}(window));',
},
plugins: [markAsBrowserBuildPlugin],
};
// used by `@sentry/serverless`, when creating the lambda layer
const nodeBundleConfig = {
output: {
format: 'cjs',
},
plugins: [commonJSPlugin],
// Don't bundle any of Node's core modules
external: builtinModules,
};
// used by all bundles
const sharedBundleConfig = {
input: entrypoints,
output: {
// a file extension will be added to this base value when we specify either a minified or non-minified build
entryFileNames: outputFileBase,
dir: 'build',
sourcemap: true,
strict: false,
esModule: false,
},
plugins:
jsVersion === 'es5'
? [tsPlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin]
: [sucrasePlugin, nodeResolvePlugin, cleanupPlugin, licensePlugin],
treeshake: 'smallest',
};
const bundleTypeConfigMap = {
standalone: standAloneBundleConfig,
addon: addOnBundleConfig,
node: nodeBundleConfig,
};
return deepMerge.all([sharedBundleConfig, bundleTypeConfigMap[bundleType], packageSpecificConfig || {}], {
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
});
}
/**
* Takes the CDN rollup config for a given package and produces three versions of it:
* - non-minified, including debug logging,
* - minified, including debug logging,
* - minified, with debug logging stripped
*
* @param baseConfig The rollup config shared by the entire package
* @returns An array of versions of that config
*/
export function makeBundleConfigVariants(baseConfig, options = {}) {
const { variants = BUNDLE_VARIANTS } = options;
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
const terserPlugin = makeTerserPlugin();
// The additional options to use for each variant we're going to create.
const variantSpecificConfigMap = {
'.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`,
},
plugins: [includeDebuggingPlugin],
},
'.min.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
},
plugins: [stripDebuggingPlugin, terserPlugin],
},
'.debug.min.js': {
output: {
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`,
},
plugins: [includeDebuggingPlugin, terserPlugin],
},
};
return variants.map(variant => {
if (!BUNDLE_VARIANTS.includes(variant)) {
throw new Error(`Unknown bundle variant requested: ${variant}`);
}
return deepMerge(baseConfig, variantSpecificConfigMap[variant], {
// Merge the plugin arrays and make sure the end result is in the correct order. Everything else can use the
// default merge strategy.
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
});
});
}