-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat(gatsby-source-wordpress): allow path to js file for beforeChangeNode option #32901
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
Merged
TylerBarnes
merged 4 commits into
master
from
feat/wp-allow-fn-path-for-beforechangenode-option
Aug 26, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5bd9fde
Allow path to js file for beforeChangeNode option
TylerBarnes 64a9230
ensure Joi doesn't fail the build for inline beforeChangeNode fn's
TylerBarnes 4607a8f
Create gatsby-version.ts
TylerBarnes ea6352e
Panic on Gatsby v4+ if beforeChangeNode is a function.
TylerBarnes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| exports.createSchemaCustomization = ({ actions }) => { | ||
| const { createTypes } = actions | ||
| const typeDefs = ` | ||
| type WpPage { | ||
| beforeChangeNodeTest: String | ||
| } | ||
| type WpPost { | ||
| beforeChangeNodeTest: String | ||
| } | ||
| ` | ||
| createTypes(typeDefs) | ||
| } |
5 changes: 5 additions & 0 deletions
5
integration-tests/gatsby-source-wordpress/src/before-change-page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module.exports = ({ remoteNode }) => { | ||
| remoteNode.beforeChangeNodeTest = `TEST-${remoteNode.id}` | ||
|
|
||
| return remoteNode | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import path from "path" | ||
| import { formatLogMessage } from "~/utils/format-log-message" | ||
| import isInteger from "lodash/isInteger" | ||
| import { IPluginOptions } from "~/models/gatsby-api" | ||
| import { GatsbyNodeApiHelpers } from "~/utils/gatsby-types" | ||
| import { usingGatsbyV4OrGreater } from "~/utils/gatsby-version" | ||
| interface IProcessorOptions { | ||
| userPluginOptions: IPluginOptions | ||
| helpers: GatsbyNodeApiHelpers | ||
|
|
@@ -46,6 +48,60 @@ const optionsProcessors: Array<IOptionsProcessor> = [ | |
|
|
||
| delete userPluginOptions.schema.queryDepth | ||
|
|
||
| return userPluginOptions | ||
| }, | ||
| }, | ||
| { | ||
| name: `Require beforeChangeNode type setting functions by absolute or relative path`, | ||
| test: ({ userPluginOptions }: IProcessorOptions): boolean => | ||
| !!userPluginOptions?.type, | ||
| processor: ({ | ||
| helpers, | ||
| userPluginOptions, | ||
| }: IProcessorOptions): IPluginOptions => { | ||
| const gatsbyStore = helpers.store.getState() | ||
| const typeSettings = Object.entries(userPluginOptions.type) | ||
|
|
||
| typeSettings.forEach(([typeName, settings]) => { | ||
| const beforeChangeNodePath = settings?.beforeChangeNode | ||
|
|
||
| if ( | ||
| usingGatsbyV4OrGreater && | ||
| typeof beforeChangeNodePath === `function` | ||
| ) { | ||
| helpers.reporter.panic( | ||
| `Since Gatsby v4+ you cannot use the ${typeName}.beforeChangeNode option as a function. Please make the option a relative or absolute path to a JS file where the beforeChangeNode fn is the default export.` | ||
| ) | ||
| } | ||
|
|
||
| if (!beforeChangeNodePath || typeof beforeChangeNodePath !== `string`) { | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| const absoluteRequirePath: string | undefined = path.isAbsolute( | ||
| beforeChangeNodePath | ||
| ) | ||
| ? beforeChangeNodePath | ||
| : require.resolve( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wardpeet you mentioned gatsby-fs. Is that available now or will this need to be updated in the future? |
||
| path.join(gatsbyStore.program.directory, beforeChangeNodePath) | ||
| ) | ||
|
|
||
| const beforeChangeNodeFn = require(absoluteRequirePath) | ||
|
|
||
| if (beforeChangeNodeFn) { | ||
| userPluginOptions.type[typeName].beforeChangeNode = | ||
| beforeChangeNodeFn | ||
| } | ||
| } catch (e) { | ||
| helpers.reporter.panic( | ||
| formatLogMessage( | ||
| `beforeChangeNode type setting for ${typeName} threw error:\n${e.message}` | ||
| ) | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| return userPluginOptions | ||
| }, | ||
| }, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import semver from "semver" | ||
| const gatsbyVersion = require(`gatsby/package.json`)?.version | ||
|
|
||
| // gt = greater than | ||
| export const usingGatsbyV4OrGreater = semver.gt(gatsbyVersion, `4.0.0`) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.