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

refactor(core): Make load plugins modular, prepare for TS #34813

Merged
merged 6 commits into from
Feb 15, 2022

Conversation

tyhopp
Copy link
Contributor

@tyhopp tyhopp commented Feb 15, 2022

Description

This refactors parts of our core Gatsby bootstrap load plugins logic to prepare for local plugin TS support in #34776.

Put up against master since tests are not passing yet in the TS feature branch, and we want to make sure this does not change any existing behavior.

What this PR does:

  • Refactor Gatsby core's load plugin logic into separate modules
  • Break loadConfigAndPlugins into two functions so we can compile local plugin gatsby files after loadConfig and before loadPlugins
  • Adjusts reporter to reflect separate load config and load plugins steps
  • Extract local plugin check logic into separate function for use in bootstrap initialize file

After merging, we can:

const compiledDirectory = `${program.directory}/${COMPILED_CACHE_DIR}`

// Compile root gatsby files
await fs.ensureDir(compiledDirectory)
await compileGatsbyFiles(program.directory)

// Load config
const { config } = await loadConfig({
  siteDirectory,
  processFlags: true,
})

// Compile local plugin gatsby files if there are any, also add this to the worker child `loadConfigAndPlugins` function (?)
await compileLocalPluginGatsbyFiles(config) // To be created, will loop over `config.plugins` and use `checkLocalPlugin`

// Load plugins
const flattenedPlugins = await loadPlugins(config, siteDirectory)

// Carry on with initialization

Documentation

No docs change needed, internal

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Feb 15, 2022
@tyhopp tyhopp added topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Feb 15, 2022
@tyhopp tyhopp marked this pull request as draft February 15, 2022 02:59
@tyhopp tyhopp force-pushed the ty/refactor-local-plugin-modules branch from ecc45f6 to 9e187e9 Compare February 15, 2022 04:03
@tyhopp tyhopp changed the title refactor(core): Make load-plugins more modular and async refactor(core): Make load plugins modular, prepare for TS Feb 15, 2022
@tyhopp tyhopp force-pushed the ty/refactor-local-plugin-modules branch 3 times, most recently from ee33e48 to ae796a0 Compare February 15, 2022 06:58
@tyhopp tyhopp force-pushed the ty/refactor-local-plugin-modules branch from ae796a0 to a6f5d38 Compare February 15, 2022 07:26
@tyhopp tyhopp marked this pull request as ready for review February 15, 2022 08:50
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't do a full review yet but this caught my eye :)

packages/gatsby/src/bootstrap/load-config/index.ts Outdated Show resolved Hide resolved
packages/gatsby/src/bootstrap/load-config/index.ts Outdated Show resolved Hide resolved
@tyhopp tyhopp marked this pull request as draft February 15, 2022 09:54
@@ -1,29 +1,23 @@
import reporter from "gatsby-cli/lib/reporter"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was renamed from load-config-and-plugins to load-config/index.ts and now exports loadConfig

@@ -1,105 +1,23 @@
import _ from "lodash"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only functions were extracted out of this file and put into their own files

@@ -0,0 +1,154 @@
import { slash } from "gatsby-core-utils"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file used to be load-plugins/load and was imported elsewhere like import { loadPlugins as loadPluginsInternal } from "./load". Name is was changed to be explicit

@@ -0,0 +1,90 @@
import { IPluginInfo, PluginRef } from "./types"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function used to be nested in loadPlugins and was moved to own file

const pluginName = isString(plugin) ? plugin : plugin.resolve

// Handle local plugins
const { validLocalPlugin, localPluginPath = `` } = checkLocalPlugin(
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is the same except the check logic for local plugins was moved to a util (checkLocalPlugin) so it can be re-used in the TS feature later

@@ -0,0 +1,41 @@
import { PluginRef } from "../types"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has the same logic as that that existed before but flattened with early returns instead of nested conditionals

@@ -0,0 +1,17 @@
import fs from "fs"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, createFileContentHash was extracted out and pasted in own file

@@ -0,0 +1,19 @@
import { createNodeId } from "../../../utils/create-node-id"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, createPluginId was extracted out and pasted in own file

@@ -0,0 +1,42 @@
import { IPluginInfo } from "../types"
Copy link
Contributor Author

@tyhopp tyhopp Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, flattenPlugins was extracted out and pasted in own file

@@ -0,0 +1,10 @@
import { ExportType, ICurrentAPIs } from "../validate"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, getAPI was extracted out and pasted in own file

@@ -0,0 +1,39 @@
import { silent as resolveFromSilent } from "resolve-from"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, cloud related functions were extracted out and pasted in this file

@@ -0,0 +1,35 @@
import { ISiteConfig, IRawSiteConfig } from "../types"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change, normalize functions were extracted out and pasted in this file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants