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

feat(gatsby-plugin-page-creator): Fix gatsby plugin page creator v4 #33120

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/gatsby-plugin-page-creator/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
3 changes: 2 additions & 1 deletion packages/gatsby/src/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export async function bootstrap(

if (process.env.GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING) {
const directory = slash(context.store.getState().program.directory)
const program = context.store.getState().program
DanielSLew marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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