From 6e133cbb79450103506cb61bf3a02a0f671cc5b2 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 09:38:48 +0200 Subject: [PATCH 1/6] v3 changes --- packages/gatsby/index.d.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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, From fb77d424876c9fc1db2f169e6c35ae8c50f87f1f Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 09:42:05 +0200 Subject: [PATCH 2/6] v4 patch --- packages/gatsby/index.d.ts | 8 +++--- patches/v4/3-onplugininit-types.patch | 35 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 patches/v4/3-onplugininit-types.patch diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 4ed2e2211463b..df803ef8b43b8 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/patches/v4/3-onplugininit-types.patch b/patches/v4/3-onplugininit-types.patch new file mode 100644 index 0000000000000..679f16a7fca88 --- /dev/null +++ b/patches/v4/3-onplugininit-types.patch @@ -0,0 +1,35 @@ +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, From 75c2273717030343a8229ff568049eca3288d739 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 09:43:01 +0200 Subject: [PATCH 3/6] oops --- packages/gatsby/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index df803ef8b43b8..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. If you indend to use this API in a plugin, use "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 "unstable_onPluginInit" instead. */ onPreBootstrap?( args: ParentSpanPluginArgs, options: PluginOptions, @@ -376,18 +376,18 @@ export interface GatsbyNode< * @gatsbyVersion 3.9.0 * @example * let createJobV2 - * exports.onPluginInit = ({ actions }) => { + * exports.unstable_onPluginInit = ({ actions }) => { * // Store job creation action to use it later * createJobV2 = actions.createJobV2 * } */ - onPluginInit?( + 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. If you indend to use this API in a plugin, use "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 "unstable_onPluginInit" instead. */ onPreInit?( args: ParentSpanPluginArgs, options: PluginOptions, From 937ab07a26f2a2152fc830bf25761e817925f187 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 09:54:11 +0200 Subject: [PATCH 4/6] remove old diff --- patches/v4/3-onplugininit-types.patch | 35 --------------------------- 1 file changed, 35 deletions(-) delete mode 100644 patches/v4/3-onplugininit-types.patch diff --git a/patches/v4/3-onplugininit-types.patch b/patches/v4/3-onplugininit-types.patch deleted file mode 100644 index 679f16a7fca88..0000000000000 --- a/patches/v4/3-onplugininit-types.patch +++ /dev/null @@ -1,35 +0,0 @@ -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, From 823515c95086caac8ca1fd4fdde0bec3f1863cf3 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 09:54:47 +0200 Subject: [PATCH 5/6] new patch file --- patches/v4/3-onplugininit.patch | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 patches/v4/3-onplugininit.patch 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. From 76bca09dba5834fe0bb4a49b0b771092d538213e Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 6 Sep 2021 10:01:18 +0200 Subject: [PATCH 6/6] cflags for files --- .../gatsby-plugin-sharp/src/gatsby-node.js | 23 ++++++++++++++----- packages/gatsby/src/services/initialize.ts | 6 ++++- .../worker/child/load-config-and-plugins.ts | 6 ++++- 3 files changed, 27 insertions(+), 8 deletions(-) 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/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`) + } }