diff --git a/benchmarks/gabe-csv-markdown/gatsby-node.js b/benchmarks/gabe-csv-markdown/gatsby-node.js index c0af0bd8c9340..5d55d27d36546 100644 --- a/benchmarks/gabe-csv-markdown/gatsby-node.js +++ b/benchmarks/gabe-csv-markdown/gatsby-node.js @@ -42,7 +42,7 @@ exports.createPages = async ({ graphql, actions }) => { }) } -exports.unstable_shouldOnCreateNode = ({ node }) => +exports.shouldOnCreateNode = ({ node }) => node.internal.type === `GendataCsv` // Not sure if there is a better way than to create a proxy node for markdown to pick up diff --git a/benchmarks/gabe-fs-text/gatsby-node.js b/benchmarks/gabe-fs-text/gatsby-node.js index b082889ee68fb..ac38ea998177c 100644 --- a/benchmarks/gabe-fs-text/gatsby-node.js +++ b/benchmarks/gabe-fs-text/gatsby-node.js @@ -40,7 +40,7 @@ exports.createPages = async ({ graphql, actions }) => { }) } -exports.unstable_shouldOnCreateNode = ({ node }) => +exports.shouldOnCreateNode = ({ node }) => node.internal.type === "File" exports.onCreateNode = ({ node, actions, createNodeId }) => { diff --git a/packages/gatsby-plugin-mdx/src/gatsby-node.ts b/packages/gatsby-plugin-mdx/src/gatsby-node.ts index b045594bb2bc4..8622567a1392b 100644 --- a/packages/gatsby-plugin-mdx/src/gatsby-node.ts +++ b/packages/gatsby-plugin-mdx/src/gatsby-node.ts @@ -282,11 +282,13 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] } // eslint-disable-next-line @typescript-eslint/naming-convention -export const unstable_shouldOnCreateNode: GatsbyNode["unstable_shouldOnCreateNode"] = - ({ node }: { node: FileSystemNode }, pluginOptions) => { - const { extensions } = defaultOptions(pluginOptions) - return node.internal.type === `File` && extensions.includes(node.ext) - } +export const shouldOnCreateNode: GatsbyNode["shouldOnCreateNode"] = ( + { node }: { node: FileSystemNode }, + pluginOptions +) => { + const { extensions } = defaultOptions(pluginOptions) + return node.internal.type === `File` && extensions.includes(node.ext) +} /** * Create Mdx nodes from MDX files. diff --git a/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js index 818a444444a12..cd12905deb2e2 100644 --- a/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js @@ -1,5 +1,5 @@ const path = require(`path`) -const { onCreateNode } = require(`../gatsby-node`) +const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`) jest.mock(`asciidoctor`, () => () => { return { @@ -47,35 +47,50 @@ describe(`gatsby-transformer-asciidoc`, () => { createContentDigest = jest.fn(() => `digest`) }) - it(`should do nothing when extension is not whitelisted`, async () => { + it(`should do nothing when extension is not allowed`, async () => { node.extension = `foo` - await onCreateNode( - { node, actions, loadNodeContent, createNodeId, createContentDigest }, - {} - ) + const shouldCreateNode = shouldOnCreateNode({ node }, {}) + + if (shouldCreateNode) { + await onCreateNode( + { node, actions, loadNodeContent, createNodeId, createContentDigest }, + {} + ) + } expect(actions.createNode).not.toHaveBeenCalled() }) it(`should enhance available extension`, async () => { node.extension = `ad` - await onCreateNode( - { node, actions, loadNodeContent, createNodeId, createContentDigest }, + const shouldCreateNode = shouldOnCreateNode( + { node }, { fileExtensions: [`ad`] } ) + + if (shouldCreateNode) { + await onCreateNode( + { node, actions, loadNodeContent, createNodeId, createContentDigest }, + { fileExtensions: [`ad`] } + ) + } expect(actions.createNode).toHaveBeenCalled() }) it(`should create node based on loaded asciidoc file`, async () => { - await onCreateNode( - { - node, - actions, - loadNodeContent, - createNodeId, - createContentDigest, - }, - {} - ) + const shouldCreateNode = shouldOnCreateNode({ node }, {}) + + if (shouldCreateNode) { + await onCreateNode( + { + node, + actions, + loadNodeContent, + createNodeId, + createContentDigest, + }, + {} + ) + } expect(actions.createNode).toHaveBeenCalledWith({ author: null, children: [], diff --git a/packages/gatsby-transformer-asciidoc/src/gatsby-node.js b/packages/gatsby-transformer-asciidoc/src/gatsby-node.js index 1395eeb1cabbf..b9eb4161fbff5 100644 --- a/packages/gatsby-transformer-asciidoc/src/gatsby-node.js +++ b/packages/gatsby-transformer-asciidoc/src/gatsby-node.js @@ -1,7 +1,7 @@ const asciidoc = require(`asciidoctor`)() const _ = require(`lodash`) -function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { +function shouldOnCreateNode({ node }, pluginOptions = {}) { const extensionsConfig = pluginOptions.fileExtensions // make extensions configurable and use adoc and asciidoc as default @@ -23,10 +23,6 @@ async function onCreateNode( }, pluginOptions ) { - if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { - return - } - // register custom converter if given if (pluginOptions.converterFactory) { asciidoc.ConverterFactory.register( @@ -140,5 +136,5 @@ const extractPageAttributes = allAttributes => return pageAttributes }, {}) -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-csv/src/gatsby-node.js b/packages/gatsby-transformer-csv/src/gatsby-node.js index 92390d737dc2a..ff3d48040be83 100644 --- a/packages/gatsby-transformer-csv/src/gatsby-node.js +++ b/packages/gatsby-transformer-csv/src/gatsby-node.js @@ -8,7 +8,7 @@ const convertToJson = (data, options) => .fromString(data) .then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`)) -function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { +function shouldOnCreateNode({ node }, pluginOptions = {}) { const { extension } = node const { extensions } = pluginOptions @@ -19,10 +19,6 @@ async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, pluginOptions ) { - if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { - return - } - const { createNode, createParentChildLink } = actions // Destructure out our custom options @@ -84,5 +80,5 @@ async function onCreateNode( return } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-documentationjs/src/gatsby-node.js b/packages/gatsby-transformer-documentationjs/src/gatsby-node.js index 0a0197a60aebe..74dbebb5802f1 100644 --- a/packages/gatsby-transformer-documentationjs/src/gatsby-node.js +++ b/packages/gatsby-transformer-documentationjs/src/gatsby-node.js @@ -185,7 +185,7 @@ exports.createResolvers = ({ createResolvers }) => { }) } -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { return ( node.internal.type === `File` && (node.internal.mediaType === `application/javascript` || @@ -195,17 +195,13 @@ function unstable_shouldOnCreateNode({ node }) { ) } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode /** * Implement the onCreateNode API to create documentation.js nodes * @param {Object} super this is a super param */ exports.onCreateNode = async ({ node, actions, ...helpers }) => { - if (!unstable_shouldOnCreateNode({ node })) { - return null - } - const { createNodeId, createContentDigest } = helpers const { createNode, createParentChildLink } = actions diff --git a/packages/gatsby-transformer-excel/src/gatsby-node.js b/packages/gatsby-transformer-excel/src/gatsby-node.js index d9c118541ad28..7360c1d5a08f3 100644 --- a/packages/gatsby-transformer-excel/src/gatsby-node.js +++ b/packages/gatsby-transformer-excel/src/gatsby-node.js @@ -28,7 +28,7 @@ const extensions = [ `numbers`, ] -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { return extensions.includes((node.extension || ``).toLowerCase()) } @@ -36,10 +36,6 @@ async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, options = {} ) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions // accept *all* options to pass to the sheet_to_json function @@ -108,5 +104,5 @@ async function onCreateNode( return } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-hjson/src/gatsby-node.js b/packages/gatsby-transformer-hjson/src/gatsby-node.js index d47928241a9d9..e6b4661eb209e 100644 --- a/packages/gatsby-transformer-hjson/src/gatsby-node.js +++ b/packages/gatsby-transformer-hjson/src/gatsby-node.js @@ -2,7 +2,7 @@ const _ = require(`lodash`) const path = require(`path`) const HJSON = require(`hjson`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // We only care about HJSON content. // NOTE the mime package does not recognize HJSON yet // See RFC https://hjson.org/rfc.html#rfc.section.1.3 @@ -19,10 +19,6 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions function transformObject(obj, id, type) { @@ -63,5 +59,5 @@ async function onCreateNode({ } } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-javascript-frontmatter/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-javascript-frontmatter/src/__tests__/gatsby-node.js index ba93be93aa1a2..794a7d9ac1125 100644 --- a/packages/gatsby-transformer-javascript-frontmatter/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-javascript-frontmatter/src/__tests__/gatsby-node.js @@ -1,4 +1,4 @@ -const { onCreateNode } = require(`../gatsby-node`) +const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`) describe(`gatsby-transformer-javascript-frontmatter`, () => { describe(`onCreateNode`, () => { @@ -43,39 +43,61 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => { `( `should loadNodeContent if file has extension $extension`, async ({ extension }) => { - await onCreateNode({ + const shouldCreateNode = shouldOnCreateNode({ node: { ...node, extension, }, - actions, - loadNodeContent, - createContentDigest, }) + + if (shouldCreateNode) { + await onCreateNode({ + node: { + ...node, + extension, + }, + actions, + loadNodeContent, + createContentDigest, + }) + } expect(loadNodeContent).toBeCalled() } ) it(`should not loadNodeContent for not javascript file`, async () => { - await onCreateNode({ + const shouldCreateNode = shouldOnCreateNode({ node: { ...node, extension: `csv`, }, - actions, - loadNodeContent, - createContentDigest, }) + + if (shouldCreateNode) { + await onCreateNode({ + node: { + ...node, + extension: `csv`, + }, + actions, + loadNodeContent, + createContentDigest, + }) + } expect(loadNodeContent).not.toBeCalled() }) it(`should load frontmatter data with exported object`, async () => { - await onCreateNode({ - node, - actions, - loadNodeContent, - createContentDigest, - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + loadNodeContent, + createContentDigest, + }) + } expect(actions.createNode).toBeCalled() expect(actions.createNode.mock.calls[0]).toMatchSnapshot() }) @@ -91,12 +113,16 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => { description: "Things about the choropleth.", } `) - await onCreateNode({ - node, - actions, - loadNodeContent, - createContentDigest, - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + loadNodeContent, + createContentDigest, + }) + } expect(actions.createNode).toBeCalled() expect(actions.createNode.mock.calls[0]).toMatchSnapshot() }) @@ -104,12 +130,16 @@ describe(`gatsby-transformer-javascript-frontmatter`, () => { it(`should pass fileAbsolutePath to node if file type is "File"`, async () => { node.internal.type = `File` node.absolutePath = `bar` - await onCreateNode({ - node, - actions, - loadNodeContent, - createContentDigest, - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + loadNodeContent, + createContentDigest, + }) + } expect(actions.createNode.mock.calls[0][0].fileAbsolutePath).toEqual( node.absolutePath ) diff --git a/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js b/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js index dccdacc888936..ff00553e59eb0 100644 --- a/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js +++ b/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js @@ -4,7 +4,7 @@ const traverse = require(`@babel/traverse`).default const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`] -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // This only processes JavaScript and TypeScript files. return fileExtsToProcess.includes(node.extension) } @@ -15,10 +15,6 @@ async function onCreateNode({ loadNodeContent, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions const code = await loadNodeContent(node) @@ -142,5 +138,5 @@ async function onCreateNode({ } } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js b/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js index b6476c3cd0b43..8a39fe84ce512 100644 --- a/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js +++ b/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js @@ -2,23 +2,18 @@ const _ = require(`lodash`) const babylon = require(`@babel/parser`) const traverse = require(`@babel/traverse`).default -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // This only processes JavaScript files. return node.internal.mediaType === `application/javascript` } async function onCreateNode({ node, - getNode, actions, loadNodeContent, createNodeId, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions const code = await loadNodeContent(node) @@ -155,5 +150,5 @@ async function onCreateNode({ } } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-json/src/gatsby-node.js b/packages/gatsby-transformer-json/src/gatsby-node.js index dee95b20753d8..0be6206ce8ad8 100644 --- a/packages/gatsby-transformer-json/src/gatsby-node.js +++ b/packages/gatsby-transformer-json/src/gatsby-node.js @@ -1,7 +1,7 @@ const _ = require(`lodash`) const path = require(`path`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // We only care about JSON content. return node.internal.mediaType === `application/json` } @@ -10,10 +10,6 @@ async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, pluginOptions ) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - function getType({ node, object, isArray }) { if (pluginOptions && _.isFunction(pluginOptions.typeName)) { return pluginOptions.typeName({ node, object, isArray }) @@ -78,5 +74,5 @@ async function onCreateNode( } } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-pdf/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-pdf/src/__tests__/gatsby-node.js index 5fc2fd620fa2a..4f8e0147438e7 100644 --- a/packages/gatsby-transformer-pdf/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-pdf/src/__tests__/gatsby-node.js @@ -1,5 +1,5 @@ const path = require(`path`) -const { onCreateNode } = require(`../gatsby-node`) +const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`) describe(`gatsby-transformer-pdf`, () => { let node @@ -25,24 +25,34 @@ describe(`gatsby-transformer-pdf`, () => { it(`should do nothing if file extension is not pdf`, async () => { node.extension = `js` - await onCreateNode({ - node, - actions, - loadNodeContent, - createNodeId, - createContentDigest, - }) + + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + loadNodeContent, + createNodeId, + createContentDigest, + }) + } + expect(createNodeId).not.toHaveBeenCalled() }) it(`should create node base on pdf data`, async () => { - await onCreateNode({ - node, - actions, - loadNodeContent, - createNodeId, - createContentDigest, - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + loadNodeContent, + createNodeId, + createContentDigest, + }) + } expect(actions.createNode).toHaveBeenCalledWith({ children: [], content: expect.any(String), diff --git a/packages/gatsby-transformer-pdf/src/gatsby-node.js b/packages/gatsby-transformer-pdf/src/gatsby-node.js index b4cd70010c69c..ffbdb62180347 100644 --- a/packages/gatsby-transformer-pdf/src/gatsby-node.js +++ b/packages/gatsby-transformer-pdf/src/gatsby-node.js @@ -2,7 +2,7 @@ const Promise = require(`bluebird`) const PDFParser = require(`pdf2json`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // Filter out non-pdf content return node.extension === `pdf` } @@ -23,14 +23,9 @@ const convertToJson = path => async function onCreateNode({ node, actions, - loadNodeContent, createNodeId, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions const parsedContent = await convertToJson(node.absolutePath) @@ -51,5 +46,5 @@ async function onCreateNode({ createParentChildLink({ parent: node, child: pdfNode }) } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-react-docgen/src/gatsby-node.js b/packages/gatsby-transformer-react-docgen/src/gatsby-node.js index c522a84d3679a..0049b50da4976 100644 --- a/packages/gatsby-transformer-react-docgen/src/gatsby-node.js +++ b/packages/gatsby-transformer-react-docgen/src/gatsby-node.js @@ -1,7 +1,4 @@ -const { - onCreateNode, - unstable_shouldOnCreateNode, -} = require(`./on-node-create`) -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).default diff --git a/packages/gatsby-transformer-react-docgen/src/on-node-create.js b/packages/gatsby-transformer-react-docgen/src/on-node-create.js index edd6b8c54461f..cb561e7864cc4 100644 --- a/packages/gatsby-transformer-react-docgen/src/on-node-create.js +++ b/packages/gatsby-transformer-react-docgen/src/on-node-create.js @@ -88,7 +88,7 @@ function createPropNodes( return node } -export function unstable_shouldOnCreateNode({ node }) { +export function shouldOnCreateNode({ node }) { return canParse(node) } diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index 9c1ef41caee4b..2a40b7ab837e5 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -1,9 +1,6 @@ -const { - onCreateNode, - unstable_shouldOnCreateNode, -} = require(`./on-node-create`) +const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) exports.onCreateNode = onCreateNode -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.createSchemaCustomization = require(`./create-schema-customization`) exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index faa91945fb742..e8a19be0a51bc 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -1,7 +1,7 @@ const grayMatter = require(`gray-matter`) const _ = require(`lodash`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { return ( node.internal.mediaType === `text/markdown` || node.internal.mediaType === `text/x-markdown` @@ -19,11 +19,6 @@ module.exports.onCreateNode = async function onCreateNode( }, pluginOptions ) { - // We only care about markdown content. - if (!unstable_shouldOnCreateNode({ node })) { - return {} - } - const { createNode, createParentChildLink } = actions const content = await loadNodeContent(node) @@ -81,4 +76,4 @@ module.exports.onCreateNode = async function onCreateNode( } } -module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +module.exports.shouldOnCreateNode = shouldOnCreateNode diff --git a/packages/gatsby-transformer-screenshot/src/gatsby-node.js b/packages/gatsby-transformer-screenshot/src/gatsby-node.js index b894a0d62b3bb..80700644cb67c 100644 --- a/packages/gatsby-transformer-screenshot/src/gatsby-node.js +++ b/packages/gatsby-transformer-screenshot/src/gatsby-node.js @@ -82,7 +82,7 @@ exports.onPreBootstrap = ( }) } -function unstable_shouldOnCreateNode({ node }, pluginOptions) { +function shouldOnCreateNode({ node }, pluginOptions) { /* * Check if node is of a type we care about, and has a url field * (originally only checked sites.yml, hence including by default) @@ -91,16 +91,17 @@ function unstable_shouldOnCreateNode({ node }, pluginOptions) { return validNodeTypes.includes(node.internal.type) && node.url } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - -exports.onCreateNode = async ( - { node, actions, store, cache, createNodeId, createContentDigest, getCache }, - pluginOptions -) => { - if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { - return - } +exports.shouldOnCreateNode = shouldOnCreateNode +exports.onCreateNode = async ({ + node, + actions, + store, + cache, + createNodeId, + createContentDigest, + getCache, +}) => { const { createNode, createParentChildLink } = actions try { diff --git a/packages/gatsby-transformer-sharp/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-sharp/src/__tests__/gatsby-node.js index 02a09a24337ab..be86bc1462db5 100644 --- a/packages/gatsby-transformer-sharp/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/__tests__/gatsby-node.js @@ -1,4 +1,4 @@ -const { onCreateNode } = require(`../gatsby-node`) +const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`) describe(`Process image nodes correctly`, () => { it(`correctly creates an ImageSharp node from a file image node`, async () => { @@ -17,16 +17,20 @@ describe(`Process image nodes correctly`, () => { const createNodeId = jest.fn() createNodeId.mockReturnValue(`uuid-from-gatsby`) - await onCreateNode({ - node, - actions, - createNodeId, - }).then(() => { - expect(createNode.mock.calls).toMatchSnapshot() - expect(createParentChildLink.mock.calls).toMatchSnapshot() - expect(createNode).toHaveBeenCalledTimes(1) - expect(createParentChildLink).toHaveBeenCalledTimes(1) - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + createNodeId, + }) + } + + expect(createNode.mock.calls).toMatchSnapshot() + expect(createParentChildLink.mock.calls).toMatchSnapshot() + expect(createNode).toHaveBeenCalledTimes(1) + expect(createParentChildLink).toHaveBeenCalledTimes(1) }) it(`doesn't create an ImageSharp node for a .gif file`, async () => { const node = { @@ -44,14 +48,18 @@ describe(`Process image nodes correctly`, () => { const createNodeId = jest.fn() createNodeId.mockReturnValue(`uuid-from-gatsby`) - await onCreateNode({ - node, - actions, - createNodeId, - }).then(() => { - expect(createNode).toHaveBeenCalledTimes(0) - expect(createParentChildLink).toHaveBeenCalledTimes(0) - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + createNodeId, + }) + } + + expect(createNode).toHaveBeenCalledTimes(0) + expect(createParentChildLink).toHaveBeenCalledTimes(0) }) it(`doesn't create an ImageSharp node if parent is not a File`, async () => { @@ -70,13 +78,17 @@ describe(`Process image nodes correctly`, () => { const createNodeId = jest.fn() createNodeId.mockReturnValue(`uuid-from-gatsby`) - await onCreateNode({ - node, - actions, - createNodeId, - }).then(() => { - expect(createNode).toHaveBeenCalledTimes(0) - expect(createParentChildLink).toHaveBeenCalledTimes(0) - }) + const shouldCreateNode = shouldOnCreateNode({ node }) + + if (shouldCreateNode) { + await onCreateNode({ + node, + actions, + createNodeId, + }) + } + + expect(createNode).toHaveBeenCalledTimes(0) + expect(createParentChildLink).toHaveBeenCalledTimes(0) }) }) diff --git a/packages/gatsby-transformer-sharp/src/gatsby-node.js b/packages/gatsby-transformer-sharp/src/gatsby-node.js index 0b32f5ab6b302..fdf47546e5d12 100644 --- a/packages/gatsby-transformer-sharp/src/gatsby-node.js +++ b/packages/gatsby-transformer-sharp/src/gatsby-node.js @@ -1,7 +1,4 @@ -const { - onCreateNode, - unstable_shouldOnCreateNode, -} = require(`./on-node-create`) +const { onCreateNode, shouldOnCreateNode } = require(`./on-node-create`) const { ERROR_MAP } = require(`./error-utils`) exports.onPreInit = ({ reporter }) => { @@ -10,6 +7,6 @@ exports.onPreInit = ({ reporter }) => { } } exports.onCreateNode = onCreateNode -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.createSchemaCustomization = require(`./customize-schema`) exports.createResolvers = require(`./create-resolvers`) diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index a08644866a325..8c89741243c76 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -1,20 +1,16 @@ const { supportedExtensions } = require(`./supported-extensions`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { return node.internal.type === `File` && !!supportedExtensions[node.extension] } -module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +module.exports.shouldOnCreateNode = shouldOnCreateNode module.exports.onCreateNode = async function onCreateNode({ node, actions, createNodeId, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions const imageNode = { diff --git a/packages/gatsby-transformer-toml/src/gatsby-node.js b/packages/gatsby-transformer-toml/src/gatsby-node.js index df34d54c430ac..5651d98fbfc41 100644 --- a/packages/gatsby-transformer-toml/src/gatsby-node.js +++ b/packages/gatsby-transformer-toml/src/gatsby-node.js @@ -1,7 +1,7 @@ const toml = require(`toml`) const _ = require(`lodash`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // Filter out non-toml content // Currently TOML files are considered null in 'mime-db' // Hence the extension test instead of mediaType test @@ -15,10 +15,6 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions // Load TOML contents @@ -51,5 +47,5 @@ async function onCreateNode({ return } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-xml/src/gatsby-node.js b/packages/gatsby-transformer-xml/src/gatsby-node.js index 392a48563a20b..01f04b2cabffd 100644 --- a/packages/gatsby-transformer-xml/src/gatsby-node.js +++ b/packages/gatsby-transformer-xml/src/gatsby-node.js @@ -1,7 +1,7 @@ const parseXml = require(`xml-parser`) const _ = require(`lodash`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { // We only care about XML content. return [`application/xml`, `text/xml`].includes(node.internal.mediaType) } @@ -13,10 +13,6 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - const { createNode, createParentChildLink } = actions const rawXml = await loadNodeContent(node) @@ -47,5 +43,5 @@ async function onCreateNode({ return } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-yaml/src/gatsby-node.js b/packages/gatsby-transformer-yaml/src/gatsby-node.js index 2d9ca9064e9dc..c210445b7a458 100644 --- a/packages/gatsby-transformer-yaml/src/gatsby-node.js +++ b/packages/gatsby-transformer-yaml/src/gatsby-node.js @@ -2,7 +2,7 @@ const jsYaml = require(`js-yaml`) const _ = require(`lodash`) const path = require(`path`) -function unstable_shouldOnCreateNode({ node }) { +function shouldOnCreateNode({ node }) { return node.internal.mediaType === `text/yaml` } @@ -10,10 +10,6 @@ async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, pluginOptions ) { - if (!unstable_shouldOnCreateNode({ node })) { - return - } - function getType({ node, object, isArray }) { if (pluginOptions && _.isFunction(pluginOptions.typeName)) { return pluginOptions.typeName({ node, object, isArray }) @@ -68,5 +64,5 @@ async function onCreateNode( } } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.shouldOnCreateNode = shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index c3dea75e6b707..4cdffd10f1534 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -452,9 +452,9 @@ export interface GatsbyNode< * * @gatsbyVersion 2.24.80 * @example - * exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' + * exports.shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' */ - unstable_shouldOnCreateNode?( + shouldOnCreateNode?( args: { node: TNode }, options: PluginOptions ): boolean diff --git a/packages/gatsby/scripts/__tests__/api.js b/packages/gatsby/scripts/__tests__/api.js index d81e2a2c780aa..beb4a4671e966 100644 --- a/packages/gatsby/scripts/__tests__/api.js +++ b/packages/gatsby/scripts/__tests__/api.js @@ -65,10 +65,10 @@ it("generates the expected api output", done => { "preprocessSource": Object {}, "resolvableExtensions": Object {}, "setFieldsOnGraphQLNodeType": Object {}, - "sourceNodes": Object {}, - "unstable_shouldOnCreateNode": Object { - "version": "2.24.80", + "shouldOnCreateNode": Object { + "version": "5.0.0", }, + "sourceNodes": Object {}, }, "ssr": Object { "onPreRenderHTML": Object {}, diff --git a/packages/gatsby/src/utils/api-node-docs.ts b/packages/gatsby/src/utils/api-node-docs.ts index 171e20c686835..9d833527cd686 100644 --- a/packages/gatsby/src/utils/api-node-docs.ts +++ b/packages/gatsby/src/utils/api-node-docs.ts @@ -157,12 +157,12 @@ export const onCreateNode = true * then Gatsby will not schedule the `onCreateNode` callback for this node for this plugin. * Note: this API does not receive the regular `api` that other callbacks get as first arg. * - * @gatsbyVersion 2.24.80 + * @gatsbyVersion 5.0.0 * @example - * exports.unstable_shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' + * exports.shouldOnCreateNode = ({node}, pluginOptions) => node.internal.type === 'Image' */ // eslint-disable-next-line @typescript-eslint/naming-convention -export const unstable_shouldOnCreateNode = true +export const shouldOnCreateNode = true /** * Called when a new page is created. This extension API is useful diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 3b3b7250ec2d8..f78512253e541 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -623,8 +623,8 @@ function apiRunnerNode(api, args = {}, { pluginSource, activity } = {}) { // TODO: rethink createNode API to handle this better if ( api === `onCreateNode` && - gatsbyNode?.unstable_shouldOnCreateNode && // Don't bail if this api is not exported - !gatsbyNode.unstable_shouldOnCreateNode( + gatsbyNode?.shouldOnCreateNode && // Don't bail if this api is not exported + !gatsbyNode.shouldOnCreateNode( { node: args.node }, plugin.pluginOptions )