2
2
* Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
3
3
*/
4
4
5
- import assert from 'assert ' ;
5
+ import { builtinModules } from 'module ' ;
6
6
7
7
import deepMerge from 'deepmerge' ;
8
8
9
9
import {
10
10
makeBrowserBuildPlugin ,
11
+ makeCommonJSPlugin ,
11
12
makeIsDebugBuildPlugin ,
12
13
makeLicensePlugin ,
13
14
makeNodeResolvePlugin ,
@@ -17,10 +18,10 @@ import {
17
18
makeTerserPlugin ,
18
19
makeTSPlugin ,
19
20
} from './plugins/index.js' ;
20
- import { getLastElement , insertAt } from './utils.js ' ;
21
+ import { mergePlugins } from './utils' ;
21
22
22
23
export function makeBaseBundleConfig ( options ) {
23
- const { input , isAddOn , jsVersion, licenseTitle, outputFileBase } = options ;
24
+ const { bundleType , input , jsVersion, licenseTitle, outputFileBase } = options ;
24
25
25
26
const nodeResolvePlugin = makeNodeResolvePlugin ( ) ;
26
27
const sucrasePlugin = makeSucrasePlugin ( ) ;
@@ -30,13 +31,19 @@ export function makeBaseBundleConfig(options) {
30
31
const licensePlugin = makeLicensePlugin ( licenseTitle ) ;
31
32
const tsPlugin = makeTSPlugin ( jsVersion . toLowerCase ( ) ) ;
32
33
34
+ // The `commonjs` plugin is the `esModuleInterop` of the bundling world. When used with `transformMixedEsModules`, it
35
+ // will include all dependencies, imported or required, in the final bundle. (Without it, CJS modules aren't included
36
+ // at all, and without `transformMixedEsModules`, they're only included if they're imported, not if they're required.)
37
+ const commonJSPlugin = makeCommonJSPlugin ( { transformMixedEsModules : true } ) ;
38
+
33
39
// used by `@sentry/browser`, `@sentry/tracing`, and `@sentry/vue` (bundles which are a full SDK in and of themselves)
34
40
const standAloneBundleConfig = {
35
41
output : {
36
42
format : 'iife' ,
37
43
name : 'Sentry' ,
38
44
} ,
39
45
context : 'window' ,
46
+ plugins : [ markAsBrowserBuildPlugin ] ,
40
47
} ;
41
48
42
49
// used by `@sentry/integrations` and `@sentry/wasm` (bundles which need to be combined with a stand-alone SDK bundle)
@@ -69,6 +76,17 @@ export function makeBaseBundleConfig(options) {
69
76
// code to add after the CJS wrapper
70
77
footer : '}(window));' ,
71
78
} ,
79
+ plugins : [ markAsBrowserBuildPlugin ] ,
80
+ } ;
81
+
82
+ // used by `@sentry/serverless`, when creating the lambda layer
83
+ const nodeBundleConfig = {
84
+ output : {
85
+ format : 'cjs' ,
86
+ } ,
87
+ plugins : [ commonJSPlugin ] ,
88
+ // Don't bundle any of Node's core modules
89
+ external : builtinModules ,
72
90
} ;
73
91
74
92
// used by all bundles
@@ -83,19 +101,21 @@ export function makeBaseBundleConfig(options) {
83
101
} ,
84
102
plugins :
85
103
jsVersion === 'es5'
86
- ? [ tsPlugin , markAsBrowserBuildPlugin , nodeResolvePlugin , licensePlugin ]
87
- : [
88
- sucrasePlugin ,
89
- removeBlankLinesPlugin ,
90
- removeESLintCommentsPlugin ,
91
- markAsBrowserBuildPlugin ,
92
- nodeResolvePlugin ,
93
- licensePlugin ,
94
- ] ,
104
+ ? [ tsPlugin , nodeResolvePlugin , licensePlugin ]
105
+ : [ sucrasePlugin , removeBlankLinesPlugin , removeESLintCommentsPlugin , nodeResolvePlugin , licensePlugin ] ,
95
106
treeshake : 'smallest' ,
96
107
} ;
97
108
98
- return deepMerge ( sharedBundleConfig , isAddOn ? addOnBundleConfig : standAloneBundleConfig ) ;
109
+ const bundleTypeConfigMap = {
110
+ standalone : standAloneBundleConfig ,
111
+ addon : addOnBundleConfig ,
112
+ node : nodeBundleConfig ,
113
+ } ;
114
+
115
+ return deepMerge ( sharedBundleConfig , bundleTypeConfigMap [ bundleType ] , {
116
+ // Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
117
+ customMerge : key => ( key === 'plugins' ? mergePlugins : undefined ) ,
118
+ } ) ;
99
119
}
100
120
101
121
/**
@@ -108,52 +128,45 @@ export function makeBaseBundleConfig(options) {
108
128
* @returns An array of versions of that config
109
129
*/
110
130
export function makeBundleConfigVariants ( baseConfig ) {
111
- const { plugins : baseConfigPlugins } = baseConfig ;
112
131
const includeDebuggingPlugin = makeIsDebugBuildPlugin ( true ) ;
113
132
const stripDebuggingPlugin = makeIsDebugBuildPlugin ( false ) ;
114
133
const terserPlugin = makeTerserPlugin ( ) ;
115
134
116
- // The license plugin has to be last, so it ends up after terser. Otherwise, terser will remove the license banner.
117
- assert (
118
- getLastElement ( baseConfigPlugins ) . name === 'rollup-plugin-license' ,
119
- `Last plugin in given options should be \`rollup-plugin-license\`. Found ${ getLastElement ( baseConfigPlugins ) . name } ` ,
120
- ) ;
121
-
122
135
// The additional options to use for each variant we're going to create
123
136
const variantSpecificConfigs = [
124
137
{
125
138
output : {
126
139
file : `${ baseConfig . output . file } .js` ,
127
140
} ,
128
- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin ) ,
141
+ plugins : [ includeDebuggingPlugin ] ,
129
142
} ,
130
143
// This variant isn't particularly helpful for an SDK user, as it strips logging while making no other minification
131
144
// changes, so by default we don't create it. It is however very useful when debugging rollup's treeshaking, so it's
132
145
// left here for that purpose.
133
146
// {
134
147
// output: { file: `${baseConfig.output.file}.no-debug.js`,
135
148
// },
136
- // plugins: insertAt(plugins, -2, stripDebuggingPlugin) ,
149
+ // plugins: [ stripDebuggingPlugin] ,
137
150
// },
138
151
{
139
152
output : {
140
153
file : `${ baseConfig . output . file } .min.js` ,
141
154
} ,
142
- plugins : insertAt ( baseConfigPlugins , - 2 , stripDebuggingPlugin , terserPlugin ) ,
155
+ plugins : [ stripDebuggingPlugin , terserPlugin ] ,
143
156
} ,
144
157
{
145
158
output : {
146
159
file : `${ baseConfig . output . file } .debug.min.js` ,
147
160
} ,
148
- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin , terserPlugin ) ,
161
+ plugins : [ terserPlugin ] ,
149
162
} ,
150
163
] ;
151
164
152
165
return variantSpecificConfigs . map ( variant =>
153
166
deepMerge ( baseConfig , variant , {
154
- // this makes it so that instead of concatenating the `plugin` properties of the two objects, the first value is
155
- // just overwritten by the second value
156
- arrayMerge : ( first , second ) => second ,
167
+ // Merge the plugin arrays and make sure the end result is in the correct order. Everything else can use the
168
+ // default merge strategy.
169
+ customMerge : key => ( key === 'plugins' ? mergePlugins : undefined ) ,
157
170
} ) ,
158
171
) ;
159
172
}
0 commit comments