22 * Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
33 */
44
5- import assert from 'assert ' ;
5+ import { builtinModules } from 'module ' ;
66
77import deepMerge from 'deepmerge' ;
88
99import {
1010 makeBrowserBuildPlugin ,
11+ makeCommonJSPlugin ,
1112 makeIsDebugBuildPlugin ,
1213 makeLicensePlugin ,
1314 makeNodeResolvePlugin ,
@@ -17,10 +18,10 @@ import {
1718 makeTerserPlugin ,
1819 makeTSPlugin ,
1920} from './plugins/index.js' ;
20- import { getLastElement , insertAt } from './utils.js ' ;
21+ import { mergePlugins } from './utils' ;
2122
2223export function makeBaseBundleConfig ( options ) {
23- const { input , isAddOn , jsVersion, licenseTitle, outputFileBase } = options ;
24+ const { bundleType , input , jsVersion, licenseTitle, outputFileBase } = options ;
2425
2526 const nodeResolvePlugin = makeNodeResolvePlugin ( ) ;
2627 const sucrasePlugin = makeSucrasePlugin ( ) ;
@@ -30,13 +31,19 @@ export function makeBaseBundleConfig(options) {
3031 const licensePlugin = makeLicensePlugin ( licenseTitle ) ;
3132 const tsPlugin = makeTSPlugin ( jsVersion . toLowerCase ( ) ) ;
3233
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+
3339 // used by `@sentry/browser`, `@sentry/tracing`, and `@sentry/vue` (bundles which are a full SDK in and of themselves)
3440 const standAloneBundleConfig = {
3541 output : {
3642 format : 'iife' ,
3743 name : 'Sentry' ,
3844 } ,
3945 context : 'window' ,
46+ plugins : [ markAsBrowserBuildPlugin ] ,
4047 } ;
4148
4249 // 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) {
6976 // code to add after the CJS wrapper
7077 footer : '}(window));' ,
7178 } ,
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 ,
7290 } ;
7391
7492 // used by all bundles
@@ -83,19 +101,21 @@ export function makeBaseBundleConfig(options) {
83101 } ,
84102 plugins :
85103 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 ] ,
95106 treeshake : 'smallest' ,
96107 } ;
97108
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+ } ) ;
99119}
100120
101121/**
@@ -108,52 +128,45 @@ export function makeBaseBundleConfig(options) {
108128 * @returns An array of versions of that config
109129 */
110130export function makeBundleConfigVariants ( baseConfig ) {
111- const { plugins : baseConfigPlugins } = baseConfig ;
112131 const includeDebuggingPlugin = makeIsDebugBuildPlugin ( true ) ;
113132 const stripDebuggingPlugin = makeIsDebugBuildPlugin ( false ) ;
114133 const terserPlugin = makeTerserPlugin ( ) ;
115134
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-
122135 // The additional options to use for each variant we're going to create
123136 const variantSpecificConfigs = [
124137 {
125138 output : {
126139 file : `${ baseConfig . output . file } .js` ,
127140 } ,
128- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin ) ,
141+ plugins : [ includeDebuggingPlugin ] ,
129142 } ,
130143 // This variant isn't particularly helpful for an SDK user, as it strips logging while making no other minification
131144 // changes, so by default we don't create it. It is however very useful when debugging rollup's treeshaking, so it's
132145 // left here for that purpose.
133146 // {
134147 // output: { file: `${baseConfig.output.file}.no-debug.js`,
135148 // },
136- // plugins: insertAt(plugins, -2, stripDebuggingPlugin) ,
149+ // plugins: [ stripDebuggingPlugin] ,
137150 // },
138151 {
139152 output : {
140153 file : `${ baseConfig . output . file } .min.js` ,
141154 } ,
142- plugins : insertAt ( baseConfigPlugins , - 2 , stripDebuggingPlugin , terserPlugin ) ,
155+ plugins : [ stripDebuggingPlugin , terserPlugin ] ,
143156 } ,
144157 {
145158 output : {
146159 file : `${ baseConfig . output . file } .debug.min.js` ,
147160 } ,
148- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin , terserPlugin ) ,
161+ plugins : [ terserPlugin ] ,
149162 } ,
150163 ] ;
151164
152165 return variantSpecificConfigs . map ( variant =>
153166 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 ) ,
157170 } ) ,
158171 ) ;
159172}
0 commit comments