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

fix(gatsby): Add TS type/v4 patches for unstable_onPluginInit #33062

Merged
merged 6 commits into from
Sep 6, 2021
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
23 changes: 17 additions & 6 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ const path = require(`path`)
let coreSupportsOnPluginInit
try {
const { isGatsbyNodeLifecycleSupported } = require(`gatsby-plugin-utils`)
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(
`unstable_onPluginInit`
)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(`onPluginInit`)
} else {
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(
`unstable_onPluginInit`
)
}
} catch (e) {
coreSupportsOnPluginInit = false
}
Expand Down Expand Up @@ -127,9 +131,16 @@ exports.onPostBootstrap = async ({ reporter, cache, store }) => {

if (coreSupportsOnPluginInit) {
// to properly initialize plugin in worker (`onPreBootstrap` won't run in workers)
exports.unstable_onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
exports.onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
}
} else {
exports.unstable_onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
}
}
}

Expand Down
20 changes: 18 additions & 2 deletions packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export interface GatsbyNode<
callback: PluginCallback<void>
): void | Promise<void>

/** Called at the end of the bootstrap process after all other extension APIs have been called. */
/** Called at the end of the bootstrap process after all other extension APIs have been called. If you indend to use this API in a plugin, use "unstable_onPluginInit" instead. */
onPreBootstrap?(
args: ParentSpanPluginArgs,
options: PluginOptions,
Expand All @@ -370,8 +370,24 @@ export interface GatsbyNode<
options: PluginOptions,
callback: PluginCallback<void>
): void | Promise<void>

/**
* Lifecycle executed in each process (one time per process). Used to store actions, etc. for later use. Plugins should use this over other APIs like "onPreBootstrap" or "onPreInit" since onPluginInit will run in main process + all workers to support Parallel Query Running.
* @gatsbyVersion 3.9.0
* @example
* let createJobV2
* exports.unstable_onPluginInit = ({ actions }) => {
* // Store job creation action to use it later
* createJobV2 = actions.createJobV2
* }
*/
unstable_onPluginInit?(
args: ParentSpanPluginArgs,
options: PluginOptions,
callback: PluginCallback<void>
): void | Promise<void>

/** The first API called during Gatsby execution, runs as soon as plugins are loaded, before cache initialization and bootstrap preparation. */
/** The first API called during Gatsby execution, runs as soon as plugins are loaded, before cache initialization and bootstrap preparation. If you indend to use this API in a plugin, use "unstable_onPluginInit" instead. */
onPreInit?(
args: ParentSpanPluginArgs,
options: PluginOptions,
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/services/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ export async function initialize({
await fs.ensureDir(`${publicDirectory}/static`)

// Init plugins once cache is initialized
await apiRunnerNode(`unstable_onPluginInit`)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
await apiRunnerNode(`onPluginInit`)
} else {
await apiRunnerNode(`unstable_onPluginInit`)
}

activity.end()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ export async function loadConfigAndPlugins(
await internalLoadConfigAndPlugins(...args)

// Cache is already initialized
await apiRunnerNode(`unstable_onPluginInit`)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
await apiRunnerNode(`onPluginInit`)
} else {
await apiRunnerNode(`unstable_onPluginInit`)
}
}
69 changes: 69 additions & 0 deletions patches/v4/3-onplugininit.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts
index 4ed2e22114..df803ef8b4 100644
--- a/packages/gatsby/index.d.ts
+++ b/packages/gatsby/index.d.ts
@@ -350,7 +350,7 @@ export interface GatsbyNode<
callback: PluginCallback<void>
): void | Promise<void>

- /** Called at the end of the bootstrap process after all other extension APIs have been called. If you indend to use this API in a plugin, use "unstable_onPluginInit" instead. */
+ /** Called at the end of the bootstrap process after all other extension APIs have been called. If you indend to use this API in a plugin, use "onPluginInit" instead. */
onPreBootstrap?(
args: ParentSpanPluginArgs,
options: PluginOptions,
@@ -376,18 +376,18 @@ export interface GatsbyNode<
* @gatsbyVersion 3.9.0
* @example
* let createJobV2
- * exports.unstable_onPluginInit = ({ actions }) => {
+ * exports.onPluginInit = ({ actions }) => {
* // Store job creation action to use it later
* createJobV2 = actions.createJobV2
* }
*/
- unstable_onPluginInit?(
+ onPluginInit?(
args: ParentSpanPluginArgs,
options: PluginOptions,
callback: PluginCallback<void>
): void | Promise<void>

- /** The first API called during Gatsby execution, runs as soon as plugins are loaded, before cache initialization and bootstrap preparation. If you indend to use this API in a plugin, use "unstable_onPluginInit" instead. */
+ /** The first API called during Gatsby execution, runs as soon as plugins are loaded, before cache initialization and bootstrap preparation. If you indend to use this API in a plugin, use "onPluginInit" instead. */
onPreInit?(
args: ParentSpanPluginArgs,
options: PluginOptions,
diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js
index c0b7735a76..593059e8ce 100644
--- a/packages/gatsby/scripts/__tests__/api.js
+++ b/packages/gatsby/scripts/__tests__/api.js
@@ -57,7 +57,7 @@ it("generates the expected api output", done => {
"resolvableExtensions": Object {},
"setFieldsOnGraphQLNodeType": Object {},
"sourceNodes": Object {},
- "unstable_onPluginInit": Object {
+ "onPluginInit": Object {
"version": "3.9.0",
},
"unstable_shouldOnCreateNode": Object {
diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts
index 793386f614..06b2259350 100644
--- a/packages/gatsby/src/utils/api-node-docs.ts
+++ b/packages/gatsby/src/utils/api-node-docs.ts
@@ -415,14 +415,13 @@ export const onPreInit = true
*
* @example
* let createJobV2
- * exports.unstable_onPluginInit = ({ actions }) => {
+ * exports.onPluginInit = ({ actions }) => {
* // store job creation action to use it later
* createJobV2 = actions.createJobV2
* }
* @gatsbyVersion 3.9.0
*/
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export const unstable_onPluginInit = true
+export const onPluginInit = true

/**
* Called once Gatsby has initialized itself and is ready to bootstrap your site.