diff --git a/packages/gatsby-plugin-sharp/src/gatsby-node.js b/packages/gatsby-plugin-sharp/src/gatsby-node.js index fa175f47cf250..1f474d9f54f2a 100644 --- a/packages/gatsby-plugin-sharp/src/gatsby-node.js +++ b/packages/gatsby-plugin-sharp/src/gatsby-node.js @@ -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 } @@ -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) + } } } diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 5dac0d79685e0..4ed2e2211463b 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -350,7 +350,7 @@ export interface GatsbyNode< callback: PluginCallback ): void | Promise - /** 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, @@ -370,8 +370,24 @@ export interface GatsbyNode< options: PluginOptions, callback: PluginCallback ): void | Promise + + /** + * 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 | Promise - /** 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, diff --git a/packages/gatsby/src/services/initialize.ts b/packages/gatsby/src/services/initialize.ts index 0609b1055b943..89acd0eed7fbd 100644 --- a/packages/gatsby/src/services/initialize.ts +++ b/packages/gatsby/src/services/initialize.ts @@ -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() diff --git a/packages/gatsby/src/utils/worker/child/load-config-and-plugins.ts b/packages/gatsby/src/utils/worker/child/load-config-and-plugins.ts index 83caed951629b..ba849f5c15819 100644 --- a/packages/gatsby/src/utils/worker/child/load-config-and-plugins.ts +++ b/packages/gatsby/src/utils/worker/child/load-config-and-plugins.ts @@ -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`) + } } diff --git a/patches/v4/3-onplugininit.patch b/patches/v4/3-onplugininit.patch new file mode 100644 index 0000000000000..bd0f36b956ffc --- /dev/null +++ b/patches/v4/3-onplugininit.patch @@ -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 | Promise + +- /** 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 | Promise + +- /** 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.