Skip to content

Commit c51ce58

Browse files
committed
add function for creating minification config variants
1 parent 691b6e4 commit c51ce58

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

rollup.config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*
99
*/
1010

11+
import assert from 'assert';
12+
1113
import deepMerge from 'deepmerge';
1214
import license from 'rollup-plugin-license';
1315
import resolve from '@rollup/plugin-node-resolve';
@@ -190,3 +192,46 @@ export function makeBaseBundleConfig(options) {
190192

191193
return deepMerge(sharedBundleConfig, isAddOn ? addOnBundleConfig : standAloneBundleConfig);
192194
}
195+
196+
export function makeMinificationVariants(existingConfigs) {
197+
const newConfigs = [];
198+
199+
// ensure we've got an array of configs rather than a single config
200+
existingConfigs = Array.isArray(existingConfigs) ? existingConfigs : [existingConfigs];
201+
202+
existingConfigs.forEach(existingConfig => {
203+
const { plugins } = existingConfig;
204+
205+
// The license plugin has to be last, so it ends up after terser. Otherwise, terser will remove the license banner.
206+
assert(
207+
getLastElement(plugins).name === 'rollup-plugin-license',
208+
`Last plugin in given options should be \`rollup-plugin-license\`. Found ${getLastElement(plugins).name}`,
209+
);
210+
211+
const minificationVariants = [
212+
{
213+
output: {
214+
file: `${existingConfig.output.file}.js`,
215+
},
216+
plugins,
217+
},
218+
{
219+
output: {
220+
file: `${existingConfig.output.file}.min.js`,
221+
},
222+
plugins: insertAt(plugins, -2, terserPlugin),
223+
},
224+
];
225+
226+
minificationVariants.forEach(variant => {
227+
const mergedConfig = deepMerge(existingConfig, variant, {
228+
// this makes it so that instead of concatenating the `plugin` properties of the two objects, the first value is
229+
// just overwritten by the second value
230+
arrayMerge: (first, second) => second,
231+
});
232+
newConfigs.push(mergedConfig);
233+
});
234+
});
235+
236+
return newConfigs;
237+
}

0 commit comments

Comments
 (0)