Skip to content

Commit

Permalink
feat(gatsby-plugin-page-creator): Fix gatsby plugin page creator v4 (#…
Browse files Browse the repository at this point in the history
…33120)

* use onPluginInit in page creator and set entire program in workers

* add gatsby-plugin-utils

* remove logs

* Update packages/gatsby/src/bootstrap/index.ts

Co-authored-by: Ward Peeters <ward@coding-tech.com>

* fix snapshot

Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
DanielSLew and wardpeet committed Sep 10, 2021
1 parent 3401149 commit 4837b72
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-page-creator/package.json
Expand Up @@ -30,6 +30,7 @@
"chokidar": "^3.5.2",
"fs-exists-cached": "^1.0.0",
"gatsby-core-utils": "^2.14.0-next.2",
"gatsby-plugin-utils": "^1.14.0-next.2",
"gatsby-page-utils": "^1.14.0-next.2",
"gatsby-telemetry": "^2.14.0-next.2",
"globby": "^11.0.4",
Expand Down
24 changes: 23 additions & 1 deletion packages/gatsby-plugin-page-creator/src/gatsby-node.ts
Expand Up @@ -23,6 +23,19 @@ import { derivePath } from "./derive-path"
import { validatePathQuery } from "./validate-path-query"
import { CODES, ERROR_MAP, prefixId } from "./error-utils"

let coreSupportsOnPluginInit: `unstable` | `stable` | undefined

try {
const { isGatsbyNodeLifecycleSupported } = require(`gatsby-plugin-utils`)
if (isGatsbyNodeLifecycleSupported(`onPluginInit`)) {
coreSupportsOnPluginInit = `stable`
} else if (isGatsbyNodeLifecycleSupported(`unstable_onPluginInit`)) {
coreSupportsOnPluginInit = `unstable`
}
} catch (e) {
console.error(`Could not check if Gatsby supports onPluginInit lifecycle`)
}

interface IOptions extends PluginOptions {
path: string
pathCheck?: boolean
Expand Down Expand Up @@ -221,7 +234,7 @@ export function setFieldsOnGraphQLNodeType(
}
}

export async function onPreInit(
async function initializePlugin(
{ reporter }: ParentSpanPluginArgs,
{ path: pagesPath }: IOptions
): Promise<void> {
Expand Down Expand Up @@ -265,3 +278,12 @@ export async function onPreInit(
})
}
}

if (coreSupportsOnPluginInit === `stable`) {
// need to conditionally export otherwise it throws an error for older versions
exports.onPluginInit = initializePlugin
} else if (coreSupportsOnPluginInit === `unstable`) {
exports.unstable_onPluginInit = initializePlugin
} else {
exports.onPreInit = initializePlugin
}
5 changes: 3 additions & 2 deletions packages/gatsby/src/bootstrap/index.ts
Expand Up @@ -50,9 +50,10 @@ export async function bootstrap(
const workerPool = context.workerPool

if (process.env.GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING) {
const directory = slash(context.store.getState().program.directory)
const program = context.store.getState().program
const directory = slash(program.directory)

workerPool.all.loadConfigAndPlugins({ siteDirectory: directory })
workerPool.all.loadConfigAndPlugins({ siteDirectory: directory, program })
}

await customizeSchema(context)
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/bootstrap/load-config-and-plugins.ts
Expand Up @@ -11,13 +11,15 @@ import loadThemes from "../bootstrap/load-themes"
import { store } from "../redux"
import handleFlags from "../utils/handle-flags"
import availableFlags from "../utils/flags"
import { IProgram } from "../commands/types"

export async function loadConfigAndPlugins({
siteDirectory,
processFlags = false,
}: {
siteDirectory: string
processFlags?: boolean
program?: IProgram
}): Promise<{
config: any
flattenedPlugins: Array<IFlattenedPlugin>
Expand Down
Expand Up @@ -110,7 +110,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/dev-404-page/src/pages",
Expand All @@ -131,7 +131,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/load-babel-config/src/pages",
Expand All @@ -152,7 +152,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/internal-data-bridge/src/pages",
Expand All @@ -173,7 +173,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/prod-404/src/pages",
Expand All @@ -194,7 +194,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/src/pages",
Expand All @@ -215,7 +215,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/bundle-optimisations/src/pages",
Expand All @@ -236,7 +236,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/functions/src/pages",
Expand Down Expand Up @@ -292,7 +292,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "/src/pages",
Expand Down Expand Up @@ -429,7 +429,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/dev-404-page/src/pages",
Expand All @@ -450,7 +450,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/load-babel-config/src/pages",
Expand All @@ -471,7 +471,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/internal-data-bridge/src/pages",
Expand All @@ -492,7 +492,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/prod-404/src/pages",
Expand All @@ -513,7 +513,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/src/pages",
Expand All @@ -534,7 +534,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/bundle-optimisations/src/pages",
Expand All @@ -555,7 +555,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/functions/src/pages",
Expand All @@ -576,7 +576,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "__TEST__/src/pages",
Expand Down Expand Up @@ -632,7 +632,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "/src/pages",
Expand Down Expand Up @@ -757,7 +757,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"ignore": Array [
Expand All @@ -780,7 +780,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/dev-404-page/src/pages",
Expand All @@ -801,7 +801,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/load-babel-config/src/pages",
Expand All @@ -822,7 +822,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/internal-data-bridge/src/pages",
Expand All @@ -843,7 +843,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/prod-404/src/pages",
Expand All @@ -864,7 +864,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/src/pages",
Expand All @@ -885,7 +885,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/bundle-optimisations/src/pages",
Expand All @@ -906,7 +906,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby/src/internal-plugins/functions/src/pages",
Expand All @@ -927,7 +927,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "<PROJECT_ROOT>/packages/gatsby-plugin-page-creator/src/pages",
Expand Down Expand Up @@ -983,7 +983,7 @@ Array [
"nodeAPIs": Array [
"createPagesStatefully",
"setFieldsOnGraphQLNodeType",
"onPreInit",
"unstable_onPluginInit",
],
"pluginOptions": Object {
"path": "/src/pages",
Expand Down
Expand Up @@ -5,12 +5,12 @@ import apiRunnerNode from "../../api-runner-node"
export async function loadConfigAndPlugins(
...args: Parameters<typeof internalLoadConfigAndPlugins>
): Promise<void> {
const [{ siteDirectory }] = args
const [{ siteDirectory, program }] = args

store.dispatch({
type: `SET_PROGRAM`,
payload: {
...store.getState().program,
...program,
directory: siteDirectory,
},
})
Expand Down

0 comments on commit 4837b72

Please sign in to comment.