UNPKG

1.83 kBJavaScriptView Raw
1/**
2 * Created by pgotthardt on 07/12/15.
3 */
4var _ = require('lodash');
5
6/**
7 * Creates configuration variants.
8 *
9 * @param {Object} [baseConfig={}] The base configuration
10 * @param {Object} variants The variants
11 * @param {Function} [configCallback] A callback executed for each configuration to
12 * transform the variant into a webpack configuration
13 * @returns {*|Array}
14 */
15module.exports = function createVariants(baseConfig, variants, configCallback) {
16 if(arguments.length < 3) {
17 if(arguments.length === 2) {
18 if(_.isFunction(variants)) {
19 // createVariants(variants: Object, configCallback: Function)
20 configCallback = variants;
21 variants = baseConfig;
22 baseConfig = {};
23 }
24 // createVariants(baseConfig: Object, variants: Object)
25 // => don't do anything
26 } else {
27 // createVariants(variants: Object)
28 variants = baseConfig;
29 baseConfig = {};
30 }
31 }
32
33 // Okay, so this looks a little bit messy but it really does make some sense.
34 // Essentially, for each base configuration, we want to create every
35 // possible combination of the configuration variants specified above.
36 var transforms = _.map(_.keys(variants), function(key) {
37 return function(config) {
38 return variants[key].map(function(value) {
39 var result = _.assign({}, config);
40 result[key] = value;
41 return result;
42 });
43 };
44 }),
45 configs = _.reduce(transforms, function(options, transform) {
46 return _.flatten(_.map(options, transform));
47 }, [baseConfig]);
48
49
50 return configCallback && _.map(configs, configCallback) || configs;
51};