UNPKG

1.88 kBJavaScriptView Raw
1"use strict";
2
3const _ = require(`lodash`);
4/**
5 * Defines how a theme object is merged with the user's config
6 */
7
8
9module.exports = (a, b) => {
10 // a and b are gatsby configs, If they have keys, that means there are values to merge
11 const allGatsbyConfigKeysWithAValue = _.uniq(Object.keys(a).concat(Object.keys(b))); // reduce the array of mergable keys into a single gatsby config object
12
13
14 const mergedConfig = allGatsbyConfigKeysWithAValue.reduce((config, gatsbyConfigKey) => {
15 // choose a merge function for the config key if there's one defined,
16 // otherwise use the default value merge function
17 const mergeFn = howToMerge[gatsbyConfigKey] || howToMerge.byDefault;
18 return Object.assign({}, config, {
19 [gatsbyConfigKey]: mergeFn(a[gatsbyConfigKey], b[gatsbyConfigKey])
20 });
21 }, {}); // return the fully merged config
22
23 return mergedConfig;
24};
25/**
26 * Normalize plugin spec before comparing so
27 * - `gatsby-plugin-name`
28 * - { resolve: `gatsby-plugin-name` }
29 * - { resolve: `gatsby-plugin-name`, options: {} }
30 * are all considered equal
31 */
32
33
34const normalizePluginEntry = entry => _.isString(entry) ? {
35 resolve: entry,
36 options: {}
37} : _.isObject(entry) ? Object.assign({
38 options: {}
39}, entry) : entry;
40
41const howToMerge = {
42 /**
43 * pick a truthy value by default.
44 * This makes sure that if a single value is defined, that one it used.
45 * We prefer the "right" value, because the user's config will be "on the right"
46 */
47 byDefault: (a, b) => b || a,
48 siteMetadata: (objA, objB) => _.merge({}, objA, objB),
49 // plugins are concatenated and uniq'd, so we don't get two of the same plugin value
50 plugins: (a = [], b = []) => _.uniqWith(a.concat(b), (a, b) => _.isEqual(normalizePluginEntry(a), normalizePluginEntry(b))),
51 mapping: (objA, objB) => _.merge({}, objA, objB)
52};
53//# sourceMappingURL=merge-gatsby-config.js.map
\No newline at end of file