Skip to content

Commit 8901eb2

Browse files
authored
fix(gatsby): Add TS type/v4 patches for unstable_onPluginInit (#33062)
1 parent 48cff06 commit 8901eb2

File tree

5 files changed

+114
-10
lines changed

5 files changed

+114
-10
lines changed

packages/gatsby-plugin-sharp/src/gatsby-node.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ const path = require(`path`)
1414
let coreSupportsOnPluginInit
1515
try {
1616
const { isGatsbyNodeLifecycleSupported } = require(`gatsby-plugin-utils`)
17-
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(
18-
`unstable_onPluginInit`
19-
)
17+
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
18+
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(`onPluginInit`)
19+
} else {
20+
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(
21+
`unstable_onPluginInit`
22+
)
23+
}
2024
} catch (e) {
2125
coreSupportsOnPluginInit = false
2226
}
@@ -127,9 +131,16 @@ exports.onPostBootstrap = async ({ reporter, cache, store }) => {
127131

128132
if (coreSupportsOnPluginInit) {
129133
// to properly initialize plugin in worker (`onPreBootstrap` won't run in workers)
130-
exports.unstable_onPluginInit = async ({ actions }, pluginOptions) => {
131-
setActions(actions)
132-
setPluginOptions(pluginOptions)
134+
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
135+
exports.onPluginInit = async ({ actions }, pluginOptions) => {
136+
setActions(actions)
137+
setPluginOptions(pluginOptions)
138+
}
139+
} else {
140+
exports.unstable_onPluginInit = async ({ actions }, pluginOptions) => {
141+
setActions(actions)
142+
setPluginOptions(pluginOptions)
143+
}
133144
}
134145
}
135146

packages/gatsby/index.d.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export interface GatsbyNode<
350350
callback: PluginCallback<void>
351351
): void | Promise<void>
352352

353-
/** Called at the end of the bootstrap process after all other extension APIs have been called. */
353+
/** 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. */
354354
onPreBootstrap?(
355355
args: ParentSpanPluginArgs,
356356
options: PluginOptions,
@@ -370,8 +370,24 @@ export interface GatsbyNode<
370370
options: PluginOptions,
371371
callback: PluginCallback<void>
372372
): void | Promise<void>
373+
374+
/**
375+
* 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.
376+
* @gatsbyVersion 3.9.0
377+
* @example
378+
* let createJobV2
379+
* exports.unstable_onPluginInit = ({ actions }) => {
380+
* // Store job creation action to use it later
381+
* createJobV2 = actions.createJobV2
382+
* }
383+
*/
384+
unstable_onPluginInit?(
385+
args: ParentSpanPluginArgs,
386+
options: PluginOptions,
387+
callback: PluginCallback<void>
388+
): void | Promise<void>
373389

374-
/** The first API called during Gatsby execution, runs as soon as plugins are loaded, before cache initialization and bootstrap preparation. */
390+
/** 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. */
375391
onPreInit?(
376392
args: ParentSpanPluginArgs,
377393
options: PluginOptions,

packages/gatsby/src/services/initialize.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,11 @@ export async function initialize({
426426
await fs.ensureDir(`${publicDirectory}/static`)
427427

428428
// Init plugins once cache is initialized
429-
await apiRunnerNode(`unstable_onPluginInit`)
429+
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
430+
await apiRunnerNode(`onPluginInit`)
431+
} else {
432+
await apiRunnerNode(`unstable_onPluginInit`)
433+
}
430434

431435
activity.end()
432436

packages/gatsby/src/utils/worker/child/load-config-and-plugins.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ export async function loadConfigAndPlugins(
1717
await internalLoadConfigAndPlugins(...args)
1818

1919
// Cache is already initialized
20-
await apiRunnerNode(`unstable_onPluginInit`)
20+
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
21+
await apiRunnerNode(`onPluginInit`)
22+
} else {
23+
await apiRunnerNode(`unstable_onPluginInit`)
24+
}
2125
}

patches/v4/3-onplugininit.patch

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts
2+
index 4ed2e22114..df803ef8b4 100644
3+
--- a/packages/gatsby/index.d.ts
4+
+++ b/packages/gatsby/index.d.ts
5+
@@ -350,7 +350,7 @@ export interface GatsbyNode<
6+
callback: PluginCallback<void>
7+
): void | Promise<void>
8+
9+
- /** 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. */
10+
+ /** 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. */
11+
onPreBootstrap?(
12+
args: ParentSpanPluginArgs,
13+
options: PluginOptions,
14+
@@ -376,18 +376,18 @@ export interface GatsbyNode<
15+
* @gatsbyVersion 3.9.0
16+
* @example
17+
* let createJobV2
18+
- * exports.unstable_onPluginInit = ({ actions }) => {
19+
+ * exports.onPluginInit = ({ actions }) => {
20+
* // Store job creation action to use it later
21+
* createJobV2 = actions.createJobV2
22+
* }
23+
*/
24+
- unstable_onPluginInit?(
25+
+ onPluginInit?(
26+
args: ParentSpanPluginArgs,
27+
options: PluginOptions,
28+
callback: PluginCallback<void>
29+
): void | Promise<void>
30+
31+
- /** 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. */
32+
+ /** 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. */
33+
onPreInit?(
34+
args: ParentSpanPluginArgs,
35+
options: PluginOptions,
36+
diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js
37+
index c0b7735a76..593059e8ce 100644
38+
--- a/packages/gatsby/scripts/__tests__/api.js
39+
+++ b/packages/gatsby/scripts/__tests__/api.js
40+
@@ -57,7 +57,7 @@ it("generates the expected api output", done => {
41+
"resolvableExtensions": Object {},
42+
"setFieldsOnGraphQLNodeType": Object {},
43+
"sourceNodes": Object {},
44+
- "unstable_onPluginInit": Object {
45+
+ "onPluginInit": Object {
46+
"version": "3.9.0",
47+
},
48+
"unstable_shouldOnCreateNode": Object {
49+
diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts
50+
index 793386f614..06b2259350 100644
51+
--- a/packages/gatsby/src/utils/api-node-docs.ts
52+
+++ b/packages/gatsby/src/utils/api-node-docs.ts
53+
@@ -415,14 +415,13 @@ export const onPreInit = true
54+
*
55+
* @example
56+
* let createJobV2
57+
- * exports.unstable_onPluginInit = ({ actions }) => {
58+
+ * exports.onPluginInit = ({ actions }) => {
59+
* // store job creation action to use it later
60+
* createJobV2 = actions.createJobV2
61+
* }
62+
* @gatsbyVersion 3.9.0
63+
*/
64+
-// eslint-disable-next-line @typescript-eslint/naming-convention
65+
-export const unstable_onPluginInit = true
66+
+export const onPluginInit = true
67+
68+
/**
69+
* Called once Gatsby has initialized itself and is ready to bootstrap your site.

0 commit comments

Comments
 (0)