22 * Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
33 */
44
5- import assert from 'assert' ;
6-
75import deepMerge from 'deepmerge' ;
86
97import {
@@ -17,7 +15,7 @@ import {
1715 makeTerserPlugin ,
1816 makeTSPlugin ,
1917} from './plugins/index.js' ;
20- import { getLastElement , insertAt } from './utils.js ' ;
18+ import { mergePlugins } from './utils' ;
2119
2220export function makeBaseBundleConfig ( options ) {
2321 const { input, isAddOn, jsVersion, licenseTitle, outputFileBase } = options ;
@@ -95,7 +93,10 @@ export function makeBaseBundleConfig(options) {
9593 treeshake : 'smallest' ,
9694 } ;
9795
98- return deepMerge ( sharedBundleConfig , isAddOn ? addOnBundleConfig : standAloneBundleConfig ) ;
96+ return deepMerge ( sharedBundleConfig , isAddOn ? addOnBundleConfig : standAloneBundleConfig , {
97+ // Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
98+ customMerge : key => ( key === 'plugins' ? mergePlugins : undefined ) ,
99+ } ) ;
99100}
100101
101102/**
@@ -108,52 +109,45 @@ export function makeBaseBundleConfig(options) {
108109 * @returns An array of versions of that config
109110 */
110111export function makeBundleConfigVariants ( baseConfig ) {
111- const { plugins : baseConfigPlugins } = baseConfig ;
112112 const includeDebuggingPlugin = makeIsDebugBuildPlugin ( true ) ;
113113 const stripDebuggingPlugin = makeIsDebugBuildPlugin ( false ) ;
114114 const terserPlugin = makeTerserPlugin ( ) ;
115115
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-
122116 // The additional options to use for each variant we're going to create
123117 const variantSpecificConfigs = [
124118 {
125119 output : {
126120 file : `${ baseConfig . output . file } .js` ,
127121 } ,
128- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin ) ,
122+ plugins : [ includeDebuggingPlugin ] ,
129123 } ,
130124 // This variant isn't particularly helpful for an SDK user, as it strips logging while making no other minification
131125 // changes, so by default we don't create it. It is however very useful when debugging rollup's treeshaking, so it's
132126 // left here for that purpose.
133127 // {
134128 // output: { file: `${baseConfig.output.file}.no-debug.js`,
135129 // },
136- // plugins: insertAt(plugins, -2, stripDebuggingPlugin) ,
130+ // plugins: [ stripDebuggingPlugin] ,
137131 // },
138132 {
139133 output : {
140134 file : `${ baseConfig . output . file } .min.js` ,
141135 } ,
142- plugins : insertAt ( baseConfigPlugins , - 2 , stripDebuggingPlugin , terserPlugin ) ,
136+ plugins : [ stripDebuggingPlugin , terserPlugin ] ,
143137 } ,
144138 {
145139 output : {
146140 file : `${ baseConfig . output . file } .debug.min.js` ,
147141 } ,
148- plugins : insertAt ( baseConfigPlugins , - 2 , includeDebuggingPlugin , terserPlugin ) ,
142+ plugins : [ terserPlugin ] ,
149143 } ,
150144 ] ;
151145
152146 return variantSpecificConfigs . map ( variant =>
153147 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 ,
148+ // Merge the plugin arrays and make sure the end result is in the correct order. Everything else can use the
149+ // default merge strategy.
150+ customMerge : key => ( key === 'plugins' ? mergePlugins : undefined ) ,
157151 } ) ,
158152 ) ;
159153}
0 commit comments