-
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.
This is the first step towards gatsby themes. It is low level and defines the way multiple gatsby sites compose by defining the way in which gatsby-config's compose. For those that are mathematically inclined, this defines a monoid for the gatsby-config data structure such that `(siteA <> siteB) <> siteC === siteA <> (siteB <> siteC)`. This method of composition opens the door to themes and sub-themes and allows us to get more input into how to deal with potentially conflicting artifacts (such as two singleton plugins being defined). A theme is defined as a parameterizable gatsby site. This means that gatsby-config can be a function that accepts configuration from the end user or a subtheme.
- Loading branch information
1 parent
f008878
commit 73e8c76
Showing
4 changed files
with
131 additions
and
2 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
83 changes: 83 additions & 0 deletions
83
packages/gatsby/src/utils/__tests__/merge-gatsby-config.js
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const mergeGatsbyConfig = require(`../merge-gatsby-config`) | ||
|
||
describe(`Merge gatsby config`, () => { | ||
it(`Merging empty config is an identity operation`, () => { | ||
const emptyConfig = {} | ||
const basicConfig = { | ||
plugins: [`gatsby-mdx`], | ||
} | ||
|
||
expect(mergeGatsbyConfig(basicConfig, emptyConfig)).toEqual(basicConfig) | ||
expect(mergeGatsbyConfig(emptyConfig, basicConfig)).toEqual(basicConfig) | ||
}) | ||
|
||
it(`Merging plugins concatenates them`, () => { | ||
const basicConfig = { | ||
plugins: [`gatsby-mdx`], | ||
} | ||
const morePlugins = { | ||
plugins: [`a-plugin`, `b-plugin`, { resolve: `c-plugin`, options: {} }], | ||
} | ||
expect(mergeGatsbyConfig(basicConfig, morePlugins)).toEqual({ | ||
plugins: [ | ||
`gatsby-mdx`, | ||
`a-plugin`, | ||
`b-plugin`, | ||
{ resolve: `c-plugin`, options: {} }, | ||
], | ||
}) | ||
expect(mergeGatsbyConfig(morePlugins, basicConfig)).toEqual({ | ||
plugins: [ | ||
`a-plugin`, | ||
`b-plugin`, | ||
{ resolve: `c-plugin`, options: {} }, | ||
`gatsby-mdx`, | ||
], | ||
}) | ||
}) | ||
|
||
it(`Merging siteMetadata is recursive`, () => { | ||
const a = { | ||
siteMetadata: { | ||
title: "my site", | ||
something: { else: 1 }, | ||
}, | ||
} | ||
|
||
const b = { | ||
siteMetadata: { | ||
something: { nested: 2 }, | ||
}, | ||
} | ||
|
||
expect(mergeGatsbyConfig(a, b)).toEqual({ | ||
siteMetadata: { | ||
title: "my site", | ||
something: { else: 1, nested: 2 }, | ||
}, | ||
}) | ||
}) | ||
|
||
it(`Merging proxy is overriden`, () => { | ||
const a = { | ||
proxy: { | ||
prefix: "/something-not/api", | ||
url: "http://examplesite.com/api/", | ||
}, | ||
} | ||
|
||
const b = { | ||
proxy: { | ||
prefix: "/api", | ||
url: "http://examplesite.com/api/", | ||
}, | ||
} | ||
|
||
expect(mergeGatsbyConfig(a, b)).toEqual({ | ||
proxy: { | ||
prefix: "/api", | ||
url: "http://examplesite.com/api/", | ||
}, | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 | ||
}, {}) | ||
|
||
const mergeAlgo = { | ||
siteMetadata: (a, b) => _.merge({}, a, b), | ||
plugins: (a = [], b = []) => a.concat(b), | ||
mapping: (a, b) => _.merge({}, a, b), | ||
} |