Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): migrate merge-gatsby-config to TypeScript #23789

Merged
merged 7 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/load-themes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createRequireFromPath } = require(`gatsby-core-utils`)
const path = require(`path`)
const mergeGatsbyConfig = require(`../../utils/merge-gatsby-config`)
import { mergeGatsbyConfig } from "../../utils/merge-gatsby-config"
const Promise = require(`bluebird`)
const _ = require(`lodash`)
const debug = require(`debug`)(`gatsby:load-themes`)
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface IGatsbyConfig {
developMiddleware?: any
proxy?: any
pathPrefix?: string
mapping?: Record<string, string>
}

export interface IGatsbyNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mergeGatsbyConfig = require(`../merge-gatsby-config`)
import { mergeGatsbyConfig } from "../merge-gatsby-config"

describe(`Merge gatsby config`, () => {
it(`Merging empty config is an identity operation`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
const _ = require(`lodash`)
/**
* Defines how a theme object is merged with the user's config
*/
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))
)
import _ from "lodash"
import { Express } from "express"
// TODO export it in index.d.ts
type PluginEntry =
| string
| {
resolve: string
options?: Record<string, unknown>
}

// 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]),
}
},
{}
)
interface INormalizedPluginEntry {
resolve: string
options: Record<string, unknown>
}

// return the fully merged config
return mergedConfig
interface IGatsbyConfigInput {
siteMetadata?: Record<string, unknown>
plugins?: Array<PluginEntry>
pathPrefix?: string
assetPrefix?: string
polyfill?: boolean
mapping?: Record<string, string>
proxy?: {
prefix: string
url: string
}
developMiddleware?(app: Express): void
}

type ConfigKey = keyof IGatsbyConfigInput
type Metadata = IGatsbyConfigInput["siteMetadata"]
type Mapping = IGatsbyConfigInput["mapping"]

/**
* Normalize plugin spec before comparing so
* - `gatsby-plugin-name`
* - { resolve: `gatsby-plugin-name` }
* - { resolve: `gatsby-plugin-name`, options: {} }
* are all considered equal
*/
const normalizePluginEntry = entry =>
const normalizePluginEntry = (entry: PluginEntry): INormalizedPluginEntry =>
_.isString(entry)
? { resolve: entry, options: {} }
? {
resolve: entry,
options: {},
}
: _.isObject(entry)
? { options: {}, ...entry }
: entry
Expand All @@ -46,15 +54,46 @@ const howToMerge = {
* 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),
byDefault: (a: ConfigKey, b: ConfigKey): ConfigKey => b || a,
siteMetadata: (objA: Metadata, objB: Metadata): Metadata =>
_.merge({}, objA, objB),
// plugins are concatenated and uniq'd, so we don't get two of the same plugin value
plugins: (a = [], b = []) =>
plugins: (a: PluginEntry[] = [], b: PluginEntry[] = []): PluginEntry[] =>
_.uniqWith(a.concat(b), (a, b) =>
_.isEqual(
_.pick(normalizePluginEntry(a), [`resolve`, `options`]),
_.pick(normalizePluginEntry(b), [`resolve`, `options`])
)
),
mapping: (objA, objB) => _.merge({}, objA, objB),
mapping: (objA: Mapping, objB: Mapping): Mapping => _.merge({}, objA, objB),
} as const

/**
* Defines how a theme object is merged with the user's config
*/
export const mergeGatsbyConfig = (
a: IGatsbyConfigInput,
b: IGatsbyConfigInput
): IGatsbyConfigInput => {
// 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))
) as ConfigKey[]

// 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]),
}
},
{} as IGatsbyConfigInput
)

// return the fully merged config
return mergedConfig
}