-
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.
refactor(gatsby): single function to load config and plugins
- Loading branch information
Showing
2 changed files
with
64 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import reporter from "gatsby-cli/lib/reporter" | ||
|
||
import { IProgram } from "../commands/types" | ||
import { IFlattenedPlugin } from "./load-plugins/types" | ||
|
||
import { preferDefault } from "../bootstrap/prefer-default" | ||
import { getConfigFile } from "../bootstrap/get-config-file" | ||
import { loadPlugins } from "../bootstrap/load-plugins" | ||
import { internalActions } from "../redux/actions" | ||
import loadThemes from "../bootstrap/load-themes" | ||
import { store } from "../redux" | ||
|
||
export async function loadConfigAndPlugins({ | ||
siteDirectory, | ||
}: { | ||
siteDirectory: string | ||
}): Promise<{ | ||
config: any | ||
flattenedPlugins: Array<IFlattenedPlugin> | ||
}> { | ||
// Try opening the site's gatsby-config.js file. | ||
const { configModule, configFilePath } = await getConfigFile( | ||
siteDirectory, | ||
`gatsby-config` | ||
) | ||
let config = preferDefault(configModule) | ||
|
||
// The root config cannot be exported as a function, only theme configs | ||
if (typeof config === `function`) { | ||
reporter.panic({ | ||
id: `10126`, | ||
context: { | ||
configName: `gatsby-config`, | ||
siteDirectory, | ||
}, | ||
}) | ||
} | ||
|
||
// theme gatsby configs can be functions or objects | ||
if (config) { | ||
const plugins = await loadThemes(config, { | ||
configFilePath, | ||
rootDir: siteDirectory, | ||
}) | ||
config = plugins.config | ||
} | ||
|
||
store.dispatch(internalActions.setSiteConfig(config)) | ||
|
||
const flattenedPlugins = await loadPlugins(config, siteDirectory) | ||
|
||
return { config, flattenedPlugins } | ||
} |
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