-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Try to make the merge code more approachable for beginners, etc. * Add test for duplicate plugin values
- Loading branch information
1 parent
73e8c76
commit aba5452
Showing
3 changed files
with
74 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,39 @@ | ||
const _ = require("lodash") | ||
const _ = require(`lodash`) | ||
/** | ||
* Defines how a theme object is merged with the user's config | ||
*/ | ||
module.exports = (a, b) => | ||
_.uniq(Object.keys(a).concat(Object.keys(b))).reduce((acc, key) => { | ||
const mergeFn = mergeAlgo[key] | ||
acc[key] = mergeFn ? mergeFn(a[key], b[key]) : b[key] || a[key] | ||
return acc | ||
}, {}) | ||
module.exports = (a, b) => { | ||
// a and b are gatsby configs, If they have keys, that means there are values to merge | ||
const allGatsbyConfigKeysWithAValue = _.uniq( | ||
Object.keys(a).concat(Object.keys(b)) | ||
) | ||
|
||
const mergeAlgo = { | ||
siteMetadata: (a, b) => _.merge({}, a, b), | ||
plugins: (a = [], b = []) => a.concat(b), | ||
mapping: (a, b) => _.merge({}, a, b), | ||
// reduce the array of mergable keys into a single gatsby config object | ||
const mergedConfig = allGatsbyConfigKeysWithAValue.reduce( | ||
(config, gatsbyConfigKey) => { | ||
// choose a merge function for the config key if there's one defined, | ||
// otherwise use the default value merge function | ||
const mergeFn = howToMerge[gatsbyConfigKey] || howToMerge.byDefault | ||
return { | ||
...config, | ||
[gatsbyConfigKey]: mergeFn(a[gatsbyConfigKey], b[gatsbyConfigKey]), | ||
} | ||
}, | ||
{} | ||
) | ||
|
||
// return the fully merged config | ||
return mergedConfig | ||
} | ||
const howToMerge = { | ||
/** | ||
* pick a truthy value by default. | ||
* This makes sure that if a single value is defined, that one it used. | ||
* We prefer the "right" value, because the user's config will be "on the right" | ||
*/ | ||
byDefault: (a, b) => b || a, | ||
siteMetadata: (objA, objB) => _.merge({}, objA, objB), | ||
// plugins are concatenated and uniq'd, so we don't get two of the same plugin value | ||
plugins: (a = [], b = []) => _.uniqWith(a.concat(b), _.isEqual), | ||
mapping: (objA, objB) => _.merge({}, objA, objB), | ||
} |