1 | 'use strict'
|
2 |
|
3 | const deepmerge = require('deepmerge')
|
4 | const isPlainObj = require('is-plain-obj')
|
5 |
|
6 | const { groupBy } = require('./utils/group')
|
7 | const { removeUndefined } = require('./utils/remove_falsy')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const mergeConfigs = function (configs) {
|
14 | const cleanedConfigs = configs.map(removeUndefinedProps)
|
15 | return deepmerge.all(cleanedConfigs, { arrayMerge })
|
16 | }
|
17 |
|
18 | const removeUndefinedProps = function ({ build = {}, ...config }) {
|
19 | return removeUndefined({ ...config, build: removeUndefined(build) })
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const arrayMerge = function (arrayA, arrayB) {
|
26 | if (isPluginsProperty(arrayA) && isPluginsProperty(arrayB)) {
|
27 | return mergePlugins(arrayA, arrayB)
|
28 | }
|
29 |
|
30 | return arrayB
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 | const isPluginsProperty = function (array) {
|
36 | return Array.isArray(array) && array.every(isPluginObject)
|
37 | }
|
38 |
|
39 | const isPluginObject = function (object) {
|
40 | return isPlainObj(object) && typeof object.package === 'string'
|
41 | }
|
42 |
|
43 |
|
44 | const mergePlugins = function (pluginsA, pluginsB) {
|
45 | const plugins = [...pluginsA, ...pluginsB]
|
46 | return groupBy(plugins, 'package').map(mergePluginConfigs)
|
47 | }
|
48 |
|
49 | const mergePluginConfigs = function (plugins) {
|
50 | return plugins.reduce(mergePluginsPair, {})
|
51 | }
|
52 |
|
53 | const mergePluginsPair = function (pluginA, pluginB) {
|
54 | return deepmerge(pluginA, pluginB, { arrayMerge })
|
55 | }
|
56 |
|
57 | module.exports = { mergeConfigs }
|