From b759483e31fc137712bf7a66b062d01284b3c00b Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Mon, 19 Oct 2020 12:20:21 +0200 Subject: [PATCH 1/2] perf: implement shouldOnCreateNode for all our plugins --- benchmarks/gabe-csv-markdown/gatsby-node.js | 31 +++--- benchmarks/markdown_id/gatsby-node.js | 23 ++-- benchmarks/markdown_slug/gatsby-node.js | 23 ++-- benchmarks/md/gatsby-node.js | 32 +++--- benchmarks/mdx/gatsby-node.js | 18 ++-- benchmarks/source-agilitycms/gatsby-node.js | 100 ++++++++++-------- benchmarks/source-cosmicjs/gatsby-node.js | 14 ++- benchmarks/source-datocms/gatsby-node.js | 8 +- benchmarks/source-drupal/gatsby-node.js | 8 +- benchmarks/source-flotiq/gatsby-node.js | 8 +- packages/gatsby-plugin-mdx/gatsby-node.js | 12 ++- .../gatsby/on-create-node.js | 35 +++--- .../src/gatsby-node.js | 21 ++-- .../gatsby-transformer-csv/src/gatsby-node.js | 20 ++-- .../src/gatsby-node.js | 26 +++-- .../src/gatsby-node.js | 40 ++++++- .../src/gatsby-node.js | 25 +++-- .../src/gatsby-node.js | 16 ++- .../src/gatsby-node.js | 13 ++- .../src/gatsby-node.js | 15 ++- .../gatsby-transformer-pdf/src/gatsby-node.js | 13 ++- .../src/__tests__/on-node-create.js | 2 +- .../src/gatsby-node.js | 7 +- .../src/on-node-create.js | 10 +- .../src/on-node-create.js | 4 +- .../src/gatsby-node.js | 20 ++-- .../src/on-node-create.js | 8 +- .../src/gatsby-node.js | 17 ++- .../gatsby-transformer-xml/src/gatsby-node.js | 14 ++- .../src/gatsby-node.js | 13 ++- 30 files changed, 386 insertions(+), 210 deletions(-) diff --git a/benchmarks/gabe-csv-markdown/gatsby-node.js b/benchmarks/gabe-csv-markdown/gatsby-node.js index e175aa95b70f4..689f0607142fd 100644 --- a/benchmarks/gabe-csv-markdown/gatsby-node.js +++ b/benchmarks/gabe-csv-markdown/gatsby-node.js @@ -42,21 +42,26 @@ exports.createPages = async ({ graphql, actions }) => { }) } +function unstable_shouldOnCreateNode({ node }) { + return node.internal.type === `GendataCsv` +} + // Not sure if there is a better way than to create a proxy node for markdown to pick up // I certainly can't get remark to to pick up csv nodes :( -exports.onCreateNode = ({ node, actions }) => { +function onCreateNode({ node, actions }) { const { createNode } = actions - if (node.internal.type === `GendataCsv`) { - createNode({ - id: `${node.id}-MarkdownProxy`, - parent: node.id, - internal: { - type: `MarkdownProxy`, - mediaType: "text/markdown", - content: node.articleContent, - contentDigest: node.articleContent, - }, - }) - } + createNode({ + id: `${node.id}-MarkdownProxy`, + parent: node.id, + internal: { + type: `MarkdownProxy`, + mediaType: "text/markdown", + content: node.articleContent, + contentDigest: node.articleContent, + }, + }) } + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.onCreateNode = onCreateNode diff --git a/benchmarks/markdown_id/gatsby-node.js b/benchmarks/markdown_id/gatsby-node.js index 40a81a43188ce..268c09adf7a38 100644 --- a/benchmarks/markdown_id/gatsby-node.js +++ b/benchmarks/markdown_id/gatsby-node.js @@ -48,15 +48,20 @@ exports.createPages = async ({ graphql, actions }) => { }) } -exports.onCreateNode = ({ node, actions, getNode }) => { +function unstable_shouldOnCreateNode({ node }) { + return node.internal.type === `MarkdownRemark` +} + +function onCreateNode({ node, actions, getNode }) { const { createNodeField } = actions - if (node.internal.type === `MarkdownRemark`) { - const value = createFilePath({ node, getNode }) - createNodeField({ - name: `slug`, - node, - value, - }) - } + const value = createFilePath({ node, getNode }) + createNodeField({ + name: `slug`, + node, + value, + }) } + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.onCreateNode = onCreateNode diff --git a/benchmarks/markdown_slug/gatsby-node.js b/benchmarks/markdown_slug/gatsby-node.js index 8c980141d3a5b..216953ccd0661 100644 --- a/benchmarks/markdown_slug/gatsby-node.js +++ b/benchmarks/markdown_slug/gatsby-node.js @@ -47,15 +47,20 @@ exports.createPages = async ({ graphql, actions }) => { }) } -exports.onCreateNode = ({ node, actions, getNode }) => { +function unstable_shouldOnCreateNode({ node }) { + return node.internal.type === `MarkdownRemark` +} + +function onCreateNode({ node, actions, getNode }) { const { createNodeField } = actions - if (node.internal.type === `MarkdownRemark`) { - const value = createFilePath({ node, getNode }) - createNodeField({ - name: `slug`, - node, - value, - }) - } + const value = createFilePath({ node, getNode }) + createNodeField({ + name: `slug`, + node, + value, + }) } + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.onCreateNode = onCreateNode diff --git a/benchmarks/md/gatsby-node.js b/benchmarks/md/gatsby-node.js index 4855458bd3b53..15efb43402ec2 100644 --- a/benchmarks/md/gatsby-node.js +++ b/benchmarks/md/gatsby-node.js @@ -1,20 +1,6 @@ const path = require("path") const { createFilePath } = require("gatsby-source-filesystem") -exports.onCreateNode = ({ node, actions, getNode }) => { - const { createNodeField } = actions - - if (node.internal.type === "MarkdownRemark") { - const value = createFilePath({ node, getNode }) - - createNodeField({ - name: "path", - node, - value, - }) - } -} - exports.createPages = async ({ graphql, actions, reporter }) => { const { createPage } = actions @@ -47,3 +33,21 @@ exports.createPages = async ({ graphql, actions, reporter }) => { }) }) } + +function unstable_shouldOnCreateNode({ node }) { + return node.internal.type === `MarkdownRemark` +} + +function onCreateNode({ node, actions, getNode }) { + const { createNodeField } = actions + + const value = createFilePath({ node, getNode }) + createNodeField({ + name: `path`, + node, + value, + }) +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode +exports.onCreateNode = onCreateNode diff --git a/benchmarks/mdx/gatsby-node.js b/benchmarks/mdx/gatsby-node.js index 1388e4c84e7e6..5b013e69efb03 100644 --- a/benchmarks/mdx/gatsby-node.js +++ b/benchmarks/mdx/gatsby-node.js @@ -1,18 +1,20 @@ const path = require("path") const { createFilePath } = require("gatsby-source-filesystem") +exports.unstable_shouldOnCreateNode = function ({ node }) { + return node.internal.type === "Mdx" +} + exports.onCreateNode = ({ node, actions, getNode }) => { const { createNodeField } = actions - if (node.internal.type === "Mdx") { - const value = createFilePath({ node, getNode }) + const value = createFilePath({ node, getNode }) - createNodeField({ - name: "path", - node, - value, - }) - } + createNodeField({ + name: "path", + node, + value, + }) } exports.createPages = async ({ graphql, actions, reporter }) => { diff --git a/benchmarks/source-agilitycms/gatsby-node.js b/benchmarks/source-agilitycms/gatsby-node.js index 86648f9fb6261..70748d6705f90 100644 --- a/benchmarks/source-agilitycms/gatsby-node.js +++ b/benchmarks/source-agilitycms/gatsby-node.js @@ -1,57 +1,69 @@ -const agility = require('./src/agility/utils') +const agility = require("./src/agility/utils") const { createRemoteFileNode } = require("gatsby-source-filesystem") //gatsy-node.js //CREATE RESOLVERS ******************************************************************************************* exports.createResolvers = (args) => { + const { + createResolvers, + getNode, + createNodeId, + createNode, + createContentDigest, + configOptions, + } = args - const { createResolvers, getNode, createNodeId, createNode, createContentDigest, configOptions } = args; + const resolvers = { + //on the 'agilityPost' node type... + agilityPost: { + //get the sitemap node that represents this item - useful for retrieving the URL for the item + sitemapNode: agility.getDynamicPageItemSitemapNode(), - const resolvers = { - //on the 'agilityPost' node type... - agilityPost: { - //get the sitemap node that represents this item - useful for retrieving the URL for the item - sitemapNode: agility.getDynamicPageItemSitemapNode(), + //[Not Implemented] + //if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query + //linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' }) + }, - //[Not Implemented] - //if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query - //linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' }) - }, + //[Not Implemented] + //if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query + // agilityImageSlider: { + // linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' }) + // } + } + createResolvers(resolvers) +} - //[Not Implemented] - //if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query - // agilityImageSlider: { - // linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' }) - // } - } - createResolvers(resolvers) +function unstable_shouldOnCreateNode({node}) { + return ( + node.properties && node.properties.referenceName.toLowerCase() === `posts` + ) } +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + exports.onCreateNode = async ({ - node, - actions: { createNode }, - store, - cache, - createNodeId, + node, + actions: { createNode }, + store, + cache, + createNodeId, }) => { - - if (node.properties - && node.properties.referenceName.toLowerCase() === `posts`) { - let field = "image" - let imageUrl = node.customFields[field] - if (imageUrl) { - let fileNode = await createRemoteFileNode({ - url: imageUrl, // string that points to the URL of the image - parentNodeId: node.id, // id of the parent node of the fileNode you are going to create - createNode, // helper function in gatsby-node to generate the node - createNodeId, // helper function in gatsby-node to generate the node id - cache, // Gatsby's cache - store, // Gatsby's redux store - }) - // if the file was created, attach the new node to the parent node - if (fileNode) { - node.customFields[`${field}LocalImg___NODE`] = fileNode.id - } - } - } -} \ No newline at end of file + if (unstable_shouldOnCreateNode({node})) { + let field = "image" + let imageUrl = node.customFields[field] + if (imageUrl) { + let fileNode = await createRemoteFileNode({ + url: imageUrl, // string that points to the URL of the image + parentNodeId: node.id, // id of the parent node of the fileNode you are going to create + createNode, // helper function in gatsby-node to generate the node + createNodeId, // helper function in gatsby-node to generate the node id + cache, // Gatsby's cache + store, // Gatsby's redux store + }) + // if the file was created, attach the new node to the parent node + if (fileNode) { + node.customFields[`${field}LocalImg___NODE`] = fileNode.id + } + } + } +} diff --git a/benchmarks/source-cosmicjs/gatsby-node.js b/benchmarks/source-cosmicjs/gatsby-node.js index 40ab25a22e39b..4912615a66686 100644 --- a/benchmarks/source-cosmicjs/gatsby-node.js +++ b/benchmarks/source-cosmicjs/gatsby-node.js @@ -1,9 +1,15 @@ const kebabCase = require(`lodash.kebabcase`) +function unstable_shouldOnCreateNode({node}) { + return node.internal.type === "node__article" +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (node.internal.type === 'node__article') { + if (unstable_shouldOnCreateNode({node})) { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } @@ -27,13 +33,13 @@ exports.createPages = async ({ actions, graphql, reporter }) => { reporter.panicOnBuild(result.errors) } - result.data.articles.edges.map(article => { + result.data.articles.edges.map((article) => { createPage({ path: article.node.slug, component: require.resolve(`./src/templates/article.js`), context: { slug: article.node.slug, - } + }, }) }) -} \ No newline at end of file +} diff --git a/benchmarks/source-datocms/gatsby-node.js b/benchmarks/source-datocms/gatsby-node.js index 75f590e92c7a0..4e53a79c06bb2 100644 --- a/benchmarks/source-datocms/gatsby-node.js +++ b/benchmarks/source-datocms/gatsby-node.js @@ -1,9 +1,15 @@ const kebabCase = require(`lodash.kebabcase`) +function unstable_shouldOnCreateNode({node}) { + return node.internal.type === `node__article` +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (node.internal.type === `node__article`) { + if (unstable_shouldOnCreateNode({node})) { createNodeField({ node, name: `slug`, value: kebabCase(node.title) }) } } diff --git a/benchmarks/source-drupal/gatsby-node.js b/benchmarks/source-drupal/gatsby-node.js index d161fa76d59a7..ca3007f4bad09 100644 --- a/benchmarks/source-drupal/gatsby-node.js +++ b/benchmarks/source-drupal/gatsby-node.js @@ -1,9 +1,15 @@ const kebabCase = require(`lodash.kebabcase`) +function unstable_shouldOnCreateNode({node}) { + return node.internal.type === `node__article` +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (node.internal.type === "node__article") { + if (unstable_shouldOnCreateNode({node})) { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } diff --git a/benchmarks/source-flotiq/gatsby-node.js b/benchmarks/source-flotiq/gatsby-node.js index 26f4f739e91fe..7b29474d0da07 100644 --- a/benchmarks/source-flotiq/gatsby-node.js +++ b/benchmarks/source-flotiq/gatsby-node.js @@ -1,9 +1,15 @@ const kebabCase = require(`lodash.kebabcase`) +function unstable_shouldOnCreateNode({node}) { + return node.internal.type === `node__article` +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (node.internal.type === "node__article") { + if (unstable_shouldOnCreateNode({node})) { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } diff --git a/packages/gatsby-plugin-mdx/gatsby-node.js b/packages/gatsby-plugin-mdx/gatsby-node.js index 0035204c58d04..247fbaf343302 100644 --- a/packages/gatsby-plugin-mdx/gatsby-node.js +++ b/packages/gatsby-plugin-mdx/gatsby-node.js @@ -4,15 +4,25 @@ const { MDX_SCOPES_LOCATION } = require(`./constants`) const defaultOptions = require(`./utils/default-options`) const fs = require(`fs`) +const { + onCreateNode, + unstable_shouldOnCreateNode +} = require(`./gatsby/on-create-node`) + /** * Create Mdx types and resolvers */ exports.sourceNodes = require(`./gatsby/source-nodes`) +/** + * Check whether the Create Mdx nodes from MDX files. + */ +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + /** * Create Mdx nodes from MDX files. */ -exports.onCreateNode = require(`./gatsby/on-create-node`) +exports.onCreateNode = onCreateNode /** * Add frontmatter as page context for MDX pages diff --git a/packages/gatsby-plugin-mdx/gatsby/on-create-node.js b/packages/gatsby-plugin-mdx/gatsby/on-create-node.js index b33d246c6f6d9..36258385bd62c 100644 --- a/packages/gatsby-plugin-mdx/gatsby/on-create-node.js +++ b/packages/gatsby-plugin-mdx/gatsby/on-create-node.js @@ -10,7 +10,26 @@ const { findImports } = require(`../utils/gen-mdx`) const contentDigest = val => createContentDigest(val) -module.exports = async ( +function unstable_shouldOnCreateNode({ node }, pluginOptions) { + const options = defaultOptions(pluginOptions) + + return _unstable_shouldOnCreateNode({ node }, options) +} + +function _unstable_shouldOnCreateNode({ node }, options) { + // options check to stop transformation of the node + if (options.shouldBlockNodeFromTransformation(node)) { + return false + } + + return node.internal.type === `File` + ? options.extensions.includes(node.ext) + : options.mediaTypes.includes(node.internal.mediaType) +} + +module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + +module.exports.onCreateNode = async ( { node, loadNodeContent, @@ -28,19 +47,7 @@ module.exports = async ( const { createNode, createParentChildLink } = actions const options = defaultOptions(pluginOptions) - // options check to stop transformation of the node - if (options.shouldBlockNodeFromTransformation(node)) { - return - } - - // if we shouldn't process this node, then return - if ( - !(node.internal.type === `File` && options.extensions.includes(node.ext)) && - !( - node.internal.type !== `File` && - options.mediaTypes.includes(node.internal.mediaType) - ) - ) { + if (!_unstable_shouldOnCreateNode({ node }, options)) { return } diff --git a/packages/gatsby-transformer-asciidoc/src/gatsby-node.js b/packages/gatsby-transformer-asciidoc/src/gatsby-node.js index 05a021f4e8a49..c1a0d8f828144 100644 --- a/packages/gatsby-transformer-asciidoc/src/gatsby-node.js +++ b/packages/gatsby-transformer-asciidoc/src/gatsby-node.js @@ -1,6 +1,16 @@ const asciidoc = require(`asciidoctor`)() const _ = require(`lodash`) +function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { + const extensionsConfig = pluginOptions.fileExtensions + + // make extensions configurable and use adoc and asciidoc as default + const supportedExtensions = + extensionsConfig instanceof Array ? extensionsConfig : [`adoc`, `asciidoc`] + + return supportedExtensions.includes(node.extension) +} + async function onCreateNode( { node, @@ -13,15 +23,7 @@ async function onCreateNode( }, pluginOptions ) { - const extensionsConfig = pluginOptions.fileExtensions - - // make extensions configurable and use adoc and asciidoc as default - const supportedExtensions = - typeof extensionsConfig !== `undefined` && extensionsConfig instanceof Array - ? extensionsConfig - : [`adoc`, `asciidoc`] - - if (!supportedExtensions.includes(node.extension)) { + if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { return } @@ -138,4 +140,5 @@ const extractPageAttributes = allAttributes => return pageAttributes }, {}) +exports.unstable_shouldOnCreateNode = unstable_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 cefed79dc5819..ca06e2dd78211 100644 --- a/packages/gatsby-transformer-csv/src/gatsby-node.js +++ b/packages/gatsby-transformer-csv/src/gatsby-node.js @@ -8,20 +8,25 @@ const convertToJson = (data, options) => .fromString(data) .then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`)) +function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { + const { extension } = node + const { extensions } = pluginOptions + + return extensions ? extensions.includes(extension) : extension === `csv` +} + async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, pluginOptions ) { + if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { + return + } + const { createNode, createParentChildLink } = actions // Destructure out our custom options - const { typeName, nodePerFile, extensions, ...options } = pluginOptions || {} - - // Filter out unwanted content - const filterExtensions = extensions ?? [`csv`] - if (!filterExtensions.includes(node.extension)) { - return - } + const { typeName, nodePerFile, ...options } = pluginOptions || {} // Load file contents const content = await loadNodeContent(node) @@ -79,4 +84,5 @@ async function onCreateNode( return } +exports.unstable_shouldOnCreateNode = unstable_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 ee4976f443fb9..1bf9f45afad61 100644 --- a/packages/gatsby-transformer-documentationjs/src/gatsby-node.js +++ b/packages/gatsby-transformer-documentationjs/src/gatsby-node.js @@ -185,25 +185,29 @@ exports.createResolvers = ({ createResolvers }) => { }) } +function unstable_shouldOnCreateNode({ node }) { + return ( + node.internal.type === `File` && + (node.internal.mediaType === `application/javascript` || + node.extension === `tsx` || + node.extension === `ts`) + ) +} + +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + /** * Implement the onCreateNode API to create documentation.js nodes * @param {Object} super this is a super param */ exports.onCreateNode = async ({ node, actions, ...helpers }) => { - const { createNodeId, createContentDigest } = helpers - const { createNode, createParentChildLink } = actions - - if ( - node.internal.type !== `File` || - !( - node.internal.mediaType === `application/javascript` || - node.extension === `tsx` || - node.extension === `ts` - ) - ) { + if (!unstable_shouldOnCreateNode({ node })) { return null } + const { createNodeId, createContentDigest } = helpers + const { createNode, createParentChildLink } = actions + let documentationJson try { documentationJson = await documentation.build(node.absolutePath, { diff --git a/packages/gatsby-transformer-excel/src/gatsby-node.js b/packages/gatsby-transformer-excel/src/gatsby-node.js index d85b7b2b91984..dfe6fbdaacd9f 100644 --- a/packages/gatsby-transformer-excel/src/gatsby-node.js +++ b/packages/gatsby-transformer-excel/src/gatsby-node.js @@ -9,17 +9,46 @@ function _loadNodeContent(fileNode, fallback) { : fallback(fileNode) } +const extensions = [ + `xls`, + `xlsx`, + `xlsm`, + `xlsb`, + `xml`, + `xlw`, + `xlc`, + `csv`, + `txt`, + `dif`, + `sylk`, + `slk`, + `prn`, + `ods`, + `fods`, + `uos`, + `dbf`, + `wks`, + `123`, + `wq1`, + `qpw`, + `htm`, + `html`, +] + +function unstable_shouldOnCreateNode({ node }) { + return extensions.includes((node.extension || ``).toLowerCase()) +} + async function onCreateNode( { node, actions, loadNodeContent, createNodeId, createContentDigest }, options = {} ) { - const { createNode, createParentChildLink } = actions - const extensions = `xls|xlsx|xlsm|xlsb|xml|xlw|xlc|csv|txt|dif|sylk|slk|prn|ods|fods|uos|dbf|wks|123|wq1|qpw|htm|html`.split( - `|` - ) - if (extensions.indexOf((node.extension || ``).toLowerCase()) == -1) { + if (!unstable_shouldOnCreateNode({ node })) { return } + + const { createNode, createParentChildLink } = actions + // Load binary string const content = await _loadNodeContent(node, loadNodeContent) @@ -86,4 +115,5 @@ async function onCreateNode( return } +exports.unstable_shouldOnCreateNode = unstable_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 ab009bc55b551..d47928241a9d9 100644 --- a/packages/gatsby-transformer-hjson/src/gatsby-node.js +++ b/packages/gatsby-transformer-hjson/src/gatsby-node.js @@ -2,6 +2,16 @@ const _ = require(`lodash`) const path = require(`path`) const HJSON = require(`hjson`) +function unstable_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 + return ( + node.internal.mediaType === `text/hjson` || + node.internal.mediaType === `application/hjson` + ) +} + async function onCreateNode({ node, actions, @@ -9,6 +19,10 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { + if (!unstable_shouldOnCreateNode({ node })) { + return + } + const { createNode, createParentChildLink } = actions function transformObject(obj, id, type) { @@ -27,16 +41,6 @@ async function onCreateNode({ createParentChildLink({ parent: node, child: jsonNode }) } - // 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 - if ( - node.internal.mediaType !== `text/hjson` && - node.internal.mediaType !== `application/hjson` - ) { - return - } - const content = await loadNodeContent(node) const parsedContent = HJSON.parse(content) @@ -59,4 +63,5 @@ async function onCreateNode({ } } +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js b/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js index 420caba781c0b..e15a64a2f1f46 100644 --- a/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js +++ b/packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js @@ -2,20 +2,25 @@ const _ = require(`lodash`) const babylon = require(`@babel/parser`) const traverse = require(`@babel/traverse`).default +const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`] + +function unstable_shouldOnCreateNode({ node }) { + // This only processes JavaScript and TypeScript files. + return fileExtsToProcess.includes(node.extension) +} + async function onCreateNode({ node, actions, loadNodeContent, createContentDigest, }) { - const { createNode, createParentChildLink } = actions - const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`] - - // This only processes JavaScript and TypeScript files. - if (!_.includes(fileExtsToProcess, node.extension)) { + if (!unstable_shouldOnCreateNode({ node })) { return } + const { createNode, createParentChildLink } = actions + const code = await loadNodeContent(node) const options = { sourceType: `module`, @@ -135,4 +140,5 @@ async function onCreateNode({ } } +exports.unstable_shouldOnCreateNode = unstable_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 3ff8d86825258..efdbdddaf2129 100644 --- a/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js +++ b/packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js @@ -2,6 +2,11 @@ const _ = require(`lodash`) const babylon = require(`@babel/parser`) const traverse = require(`@babel/traverse`).default +function unstable_shouldOnCreateNode({ node }) { + // This only processes JavaScript files. + return node.internal.mediaType === `application/javascript` +} + async function onCreateNode({ node, getNode, @@ -10,13 +15,12 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - const { createNode, createParentChildLink } = actions - - // This only processes JavaScript files. - if (node.internal.mediaType !== `application/javascript`) { + if (!unstable_shouldOnCreateNode({ node })) { return } + const { createNode, createParentChildLink } = actions + const code = await loadNodeContent(node) const options = { sourceType: `unambigious`, @@ -150,4 +154,5 @@ async function onCreateNode({ } } +exports.unstable_shouldOnCreateNode = unstable_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 65ea7c33193ff..b69fc66a03170 100644 --- a/packages/gatsby-transformer-json/src/gatsby-node.js +++ b/packages/gatsby-transformer-json/src/gatsby-node.js @@ -1,10 +1,19 @@ const _ = require(`lodash`) const path = require(`path`) +function unstable_shouldOnCreateNode({ node }) { + // We only care about JSON content. + return node.internal.mediaType === `application/json` +} + 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 }) @@ -36,11 +45,6 @@ async function onCreateNode( const { createNode, createParentChildLink } = actions - // We only care about JSON content. - if (node.internal.mediaType !== `application/json`) { - return - } - const content = await loadNodeContent(node) let parsedContent try { @@ -71,4 +75,5 @@ async function onCreateNode( } } +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-pdf/src/gatsby-node.js b/packages/gatsby-transformer-pdf/src/gatsby-node.js index 1c26439b939fe..17857a30a4fb1 100644 --- a/packages/gatsby-transformer-pdf/src/gatsby-node.js +++ b/packages/gatsby-transformer-pdf/src/gatsby-node.js @@ -1,6 +1,11 @@ const Promise = require(`bluebird`) const PDFParser = require(`pdf2json`) +function unstable_shouldOnCreateNode({ node }) { + // Filter out non-pdf content + return node.extension === `pdf` +} + const convertToJson = path => new Promise((res, rej) => { const pdfParser = new PDFParser(this, 1) @@ -21,13 +26,12 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - const { createNode, createParentChildLink } = actions - - // Filter out non-pdf content - if (node.extension !== `pdf`) { + if (!unstable_shouldOnCreateNode({ node })) { return } + const { createNode, createParentChildLink } = actions + let parsedContent = await convertToJson(node.absolutePath) const pdfNode = { @@ -46,4 +50,5 @@ async function onCreateNode({ createParentChildLink({ parent: node, child: pdfNode }) } +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.onCreateNode = onCreateNode diff --git a/packages/gatsby-transformer-react-docgen/src/__tests__/on-node-create.js b/packages/gatsby-transformer-react-docgen/src/__tests__/on-node-create.js index 28025d35a77ea..09b3db08aefe0 100644 --- a/packages/gatsby-transformer-react-docgen/src/__tests__/on-node-create.js +++ b/packages/gatsby-transformer-react-docgen/src/__tests__/on-node-create.js @@ -1,6 +1,6 @@ import fs from "fs" import { groupBy } from "lodash" -import onCreateNode from "../on-node-create" +import { onCreateNode } from "../on-node-create" import path from "path" const readFile = file => diff --git a/packages/gatsby-transformer-react-docgen/src/gatsby-node.js b/packages/gatsby-transformer-react-docgen/src/gatsby-node.js index 16650dcafbac8..c522a84d3679a 100644 --- a/packages/gatsby-transformer-react-docgen/src/gatsby-node.js +++ b/packages/gatsby-transformer-react-docgen/src/gatsby-node.js @@ -1,2 +1,7 @@ -exports.onCreateNode = require(`./on-node-create`).default +const { + onCreateNode, + unstable_shouldOnCreateNode, +} = require(`./on-node-create`) +exports.unstable_shouldOnCreateNode = unstable_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 197197860956d..8ecd14e102a32 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,11 @@ function createPropNodes( return node } -export default async function onCreateNode( +export function unstable_shouldOnCreateNode({ node }) { + return canParse(node) +} + +export async function onCreateNode( { node, loadNodeContent, @@ -99,10 +103,10 @@ export default async function onCreateNode( }, pluginOptions ) { - const { createNode, createParentChildLink } = actions - if (!canParse(node)) return + const { createNode, createParentChildLink } = actions + const content = await loadNodeContent(node) let components diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index b47dce011f945..faa91945fb742 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -19,13 +19,13 @@ module.exports.onCreateNode = async function onCreateNode( }, pluginOptions ) { - const { createNode, createParentChildLink } = actions - // We only care about markdown content. if (!unstable_shouldOnCreateNode({ node })) { return {} } + const { createNode, createParentChildLink } = actions + const content = await loadNodeContent(node) try { diff --git a/packages/gatsby-transformer-screenshot/src/gatsby-node.js b/packages/gatsby-transformer-screenshot/src/gatsby-node.js index 280082221e6a1..400ff9c7b66f9 100644 --- a/packages/gatsby-transformer-screenshot/src/gatsby-node.js +++ b/packages/gatsby-transformer-screenshot/src/gatsby-node.js @@ -76,21 +76,27 @@ exports.onPreBootstrap = ( }) } -exports.onCreateNode = async ( - { node, actions, store, cache, createNodeId, createContentDigest, getCache }, - pluginOptions -) => { - const { createNode, createParentChildLink } = actions - +function unstable_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) */ const validNodeTypes = [`SitesYaml`].concat(pluginOptions.nodeTypes || []) - if (!validNodeTypes.includes(node.internal.type) || !node.url) { + 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 } + const { createNode, createParentChildLink } = actions + try { const screenshotNode = await new Promise((resolve, reject) => { screenshotQueue diff --git a/packages/gatsby-transformer-sharp/src/on-node-create.js b/packages/gatsby-transformer-sharp/src/on-node-create.js index 30d319187cbf3..035627dcffcf8 100644 --- a/packages/gatsby-transformer-sharp/src/on-node-create.js +++ b/packages/gatsby-transformer-sharp/src/on-node-create.js @@ -4,17 +4,19 @@ function unstable_shouldOnCreateNode({ node }) { return !!supportedExtensions[node.extension] } +module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode + module.exports.onCreateNode = async function onCreateNode({ node, actions, createNodeId, }) { - const { createNode, createParentChildLink } = actions - if (!unstable_shouldOnCreateNode({ node })) { return } + const { createNode, createParentChildLink } = actions + const imageNode = { id: createNodeId(`${node.id} >> ImageSharp`), children: [], @@ -30,5 +32,3 @@ module.exports.onCreateNode = async function onCreateNode({ return } - -module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode diff --git a/packages/gatsby-transformer-toml/src/gatsby-node.js b/packages/gatsby-transformer-toml/src/gatsby-node.js index 944a538ffc2d0..df34d54c430ac 100644 --- a/packages/gatsby-transformer-toml/src/gatsby-node.js +++ b/packages/gatsby-transformer-toml/src/gatsby-node.js @@ -1,6 +1,13 @@ const toml = require(`toml`) const _ = require(`lodash`) +function unstable_shouldOnCreateNode({ node }) { + // Filter out non-toml content + // Currently TOML files are considered null in 'mime-db' + // Hence the extension test instead of mediaType test + return node.extension === `toml` +} + async function onCreateNode({ node, actions, @@ -8,13 +15,12 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - const { createNode, createParentChildLink } = actions - // Filter out non-toml content - // Currently TOML files are considered null in 'mime-db' - // Hence the extension test instead of mediaType test - if (node.extension !== `toml`) { + if (!unstable_shouldOnCreateNode({ node })) { return } + + const { createNode, createParentChildLink } = actions + // Load TOML contents const content = await loadNodeContent(node) // Parse @@ -45,4 +51,5 @@ async function onCreateNode({ return } +exports.unstable_shouldOnCreateNode = unstable_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 b603466b3e106..392a48563a20b 100644 --- a/packages/gatsby-transformer-xml/src/gatsby-node.js +++ b/packages/gatsby-transformer-xml/src/gatsby-node.js @@ -1,6 +1,11 @@ const parseXml = require(`xml-parser`) const _ = require(`lodash`) +function unstable_shouldOnCreateNode({ node }) { + // We only care about XML content. + return [`application/xml`, `text/xml`].includes(node.internal.mediaType) +} + async function onCreateNode({ node, actions, @@ -8,12 +13,12 @@ async function onCreateNode({ createNodeId, createContentDigest, }) { - const { createNode, createParentChildLink } = actions - - // We only care about XML content. - if (![`application/xml`, `text/xml`].includes(node.internal.mediaType)) { + if (!unstable_shouldOnCreateNode({ node })) { return } + + const { createNode, createParentChildLink } = actions + const rawXml = await loadNodeContent(node) const parsedXml = parseXml(rawXml) const nodeArray = parsedXml.root.children.map((obj, i) => { @@ -42,4 +47,5 @@ async function onCreateNode({ return } +exports.unstable_shouldOnCreateNode = unstable_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 9961d7303eaa1..850d65277c5b1 100644 --- a/packages/gatsby-transformer-yaml/src/gatsby-node.js +++ b/packages/gatsby-transformer-yaml/src/gatsby-node.js @@ -2,10 +2,18 @@ const jsYaml = require(`js-yaml`) const _ = require(`lodash`) const path = require(`path`) +function unstable_shouldOnCreateNode({ node }) { + return node.internal.mediaType === `text/yaml` +} + 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 }) @@ -37,10 +45,6 @@ async function onCreateNode( const { createNode, createParentChildLink } = actions - if (node.internal.mediaType !== `text/yaml`) { - return - } - const content = await loadNodeContent(node) const parsedContent = jsYaml.load(content) @@ -61,4 +65,5 @@ async function onCreateNode( } } +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode exports.onCreateNode = onCreateNode From b7cbef95abf0a7cb98748571a784ee69e1a93b59 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Tue, 20 Oct 2020 14:21:32 +0200 Subject: [PATCH 2/2] Drop changes in /benchmarks, fix typo --- benchmarks/gabe-csv-markdown/gatsby-node.js | 31 +++--- benchmarks/markdown_id/gatsby-node.js | 23 ++--- benchmarks/markdown_slug/gatsby-node.js | 23 ++--- benchmarks/md/gatsby-node.js | 32 +++---- benchmarks/mdx/gatsby-node.js | 18 ++-- benchmarks/source-agilitycms/gatsby-node.js | 100 +++++++++----------- benchmarks/source-cosmicjs/gatsby-node.js | 14 +-- benchmarks/source-datocms/gatsby-node.js | 8 +- benchmarks/source-drupal/gatsby-node.js | 8 +- benchmarks/source-flotiq/gatsby-node.js | 8 +- packages/gatsby-plugin-mdx/gatsby-node.js | 2 +- 11 files changed, 105 insertions(+), 162 deletions(-) diff --git a/benchmarks/gabe-csv-markdown/gatsby-node.js b/benchmarks/gabe-csv-markdown/gatsby-node.js index 689f0607142fd..e175aa95b70f4 100644 --- a/benchmarks/gabe-csv-markdown/gatsby-node.js +++ b/benchmarks/gabe-csv-markdown/gatsby-node.js @@ -42,26 +42,21 @@ exports.createPages = async ({ graphql, actions }) => { }) } -function unstable_shouldOnCreateNode({ node }) { - return node.internal.type === `GendataCsv` -} - // Not sure if there is a better way than to create a proxy node for markdown to pick up // I certainly can't get remark to to pick up csv nodes :( -function onCreateNode({ node, actions }) { +exports.onCreateNode = ({ node, actions }) => { const { createNode } = actions - createNode({ - id: `${node.id}-MarkdownProxy`, - parent: node.id, - internal: { - type: `MarkdownProxy`, - mediaType: "text/markdown", - content: node.articleContent, - contentDigest: node.articleContent, - }, - }) + if (node.internal.type === `GendataCsv`) { + createNode({ + id: `${node.id}-MarkdownProxy`, + parent: node.id, + internal: { + type: `MarkdownProxy`, + mediaType: "text/markdown", + content: node.articleContent, + contentDigest: node.articleContent, + }, + }) + } } - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode -exports.onCreateNode = onCreateNode diff --git a/benchmarks/markdown_id/gatsby-node.js b/benchmarks/markdown_id/gatsby-node.js index 268c09adf7a38..40a81a43188ce 100644 --- a/benchmarks/markdown_id/gatsby-node.js +++ b/benchmarks/markdown_id/gatsby-node.js @@ -48,20 +48,15 @@ exports.createPages = async ({ graphql, actions }) => { }) } -function unstable_shouldOnCreateNode({ node }) { - return node.internal.type === `MarkdownRemark` -} - -function onCreateNode({ node, actions, getNode }) { +exports.onCreateNode = ({ node, actions, getNode }) => { const { createNodeField } = actions - const value = createFilePath({ node, getNode }) - createNodeField({ - name: `slug`, - node, - value, - }) + if (node.internal.type === `MarkdownRemark`) { + const value = createFilePath({ node, getNode }) + createNodeField({ + name: `slug`, + node, + value, + }) + } } - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode -exports.onCreateNode = onCreateNode diff --git a/benchmarks/markdown_slug/gatsby-node.js b/benchmarks/markdown_slug/gatsby-node.js index 216953ccd0661..8c980141d3a5b 100644 --- a/benchmarks/markdown_slug/gatsby-node.js +++ b/benchmarks/markdown_slug/gatsby-node.js @@ -47,20 +47,15 @@ exports.createPages = async ({ graphql, actions }) => { }) } -function unstable_shouldOnCreateNode({ node }) { - return node.internal.type === `MarkdownRemark` -} - -function onCreateNode({ node, actions, getNode }) { +exports.onCreateNode = ({ node, actions, getNode }) => { const { createNodeField } = actions - const value = createFilePath({ node, getNode }) - createNodeField({ - name: `slug`, - node, - value, - }) + if (node.internal.type === `MarkdownRemark`) { + const value = createFilePath({ node, getNode }) + createNodeField({ + name: `slug`, + node, + value, + }) + } } - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode -exports.onCreateNode = onCreateNode diff --git a/benchmarks/md/gatsby-node.js b/benchmarks/md/gatsby-node.js index 15efb43402ec2..4855458bd3b53 100644 --- a/benchmarks/md/gatsby-node.js +++ b/benchmarks/md/gatsby-node.js @@ -1,6 +1,20 @@ const path = require("path") const { createFilePath } = require("gatsby-source-filesystem") +exports.onCreateNode = ({ node, actions, getNode }) => { + const { createNodeField } = actions + + if (node.internal.type === "MarkdownRemark") { + const value = createFilePath({ node, getNode }) + + createNodeField({ + name: "path", + node, + value, + }) + } +} + exports.createPages = async ({ graphql, actions, reporter }) => { const { createPage } = actions @@ -33,21 +47,3 @@ exports.createPages = async ({ graphql, actions, reporter }) => { }) }) } - -function unstable_shouldOnCreateNode({ node }) { - return node.internal.type === `MarkdownRemark` -} - -function onCreateNode({ node, actions, getNode }) { - const { createNodeField } = actions - - const value = createFilePath({ node, getNode }) - createNodeField({ - name: `path`, - node, - value, - }) -} - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode -exports.onCreateNode = onCreateNode diff --git a/benchmarks/mdx/gatsby-node.js b/benchmarks/mdx/gatsby-node.js index 5b013e69efb03..1388e4c84e7e6 100644 --- a/benchmarks/mdx/gatsby-node.js +++ b/benchmarks/mdx/gatsby-node.js @@ -1,20 +1,18 @@ const path = require("path") const { createFilePath } = require("gatsby-source-filesystem") -exports.unstable_shouldOnCreateNode = function ({ node }) { - return node.internal.type === "Mdx" -} - exports.onCreateNode = ({ node, actions, getNode }) => { const { createNodeField } = actions - const value = createFilePath({ node, getNode }) + if (node.internal.type === "Mdx") { + const value = createFilePath({ node, getNode }) - createNodeField({ - name: "path", - node, - value, - }) + createNodeField({ + name: "path", + node, + value, + }) + } } exports.createPages = async ({ graphql, actions, reporter }) => { diff --git a/benchmarks/source-agilitycms/gatsby-node.js b/benchmarks/source-agilitycms/gatsby-node.js index 70748d6705f90..86648f9fb6261 100644 --- a/benchmarks/source-agilitycms/gatsby-node.js +++ b/benchmarks/source-agilitycms/gatsby-node.js @@ -1,69 +1,57 @@ -const agility = require("./src/agility/utils") +const agility = require('./src/agility/utils') const { createRemoteFileNode } = require("gatsby-source-filesystem") //gatsy-node.js //CREATE RESOLVERS ******************************************************************************************* exports.createResolvers = (args) => { - const { - createResolvers, - getNode, - createNodeId, - createNode, - createContentDigest, - configOptions, - } = args - const resolvers = { - //on the 'agilityPost' node type... - agilityPost: { - //get the sitemap node that represents this item - useful for retrieving the URL for the item - sitemapNode: agility.getDynamicPageItemSitemapNode(), + const { createResolvers, getNode, createNodeId, createNode, createContentDigest, configOptions } = args; - //[Not Implemented] - //if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query - //linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' }) - }, + const resolvers = { + //on the 'agilityPost' node type... + agilityPost: { + //get the sitemap node that represents this item - useful for retrieving the URL for the item + sitemapNode: agility.getDynamicPageItemSitemapNode(), - //[Not Implemented] - //if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query - // agilityImageSlider: { - // linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' }) - // } - } - createResolvers(resolvers) -} + //[Not Implemented] + //if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query + //linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' }) + }, -function unstable_shouldOnCreateNode({node}) { - return ( - node.properties && node.properties.referenceName.toLowerCase() === `posts` - ) + //[Not Implemented] + //if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query + // agilityImageSlider: { + // linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' }) + // } + } + createResolvers(resolvers) } -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - exports.onCreateNode = async ({ - node, - actions: { createNode }, - store, - cache, - createNodeId, + node, + actions: { createNode }, + store, + cache, + createNodeId, }) => { - if (unstable_shouldOnCreateNode({node})) { - let field = "image" - let imageUrl = node.customFields[field] - if (imageUrl) { - let fileNode = await createRemoteFileNode({ - url: imageUrl, // string that points to the URL of the image - parentNodeId: node.id, // id of the parent node of the fileNode you are going to create - createNode, // helper function in gatsby-node to generate the node - createNodeId, // helper function in gatsby-node to generate the node id - cache, // Gatsby's cache - store, // Gatsby's redux store - }) - // if the file was created, attach the new node to the parent node - if (fileNode) { - node.customFields[`${field}LocalImg___NODE`] = fileNode.id - } - } - } -} + + if (node.properties + && node.properties.referenceName.toLowerCase() === `posts`) { + let field = "image" + let imageUrl = node.customFields[field] + if (imageUrl) { + let fileNode = await createRemoteFileNode({ + url: imageUrl, // string that points to the URL of the image + parentNodeId: node.id, // id of the parent node of the fileNode you are going to create + createNode, // helper function in gatsby-node to generate the node + createNodeId, // helper function in gatsby-node to generate the node id + cache, // Gatsby's cache + store, // Gatsby's redux store + }) + // if the file was created, attach the new node to the parent node + if (fileNode) { + node.customFields[`${field}LocalImg___NODE`] = fileNode.id + } + } + } +} \ No newline at end of file diff --git a/benchmarks/source-cosmicjs/gatsby-node.js b/benchmarks/source-cosmicjs/gatsby-node.js index 4912615a66686..40ab25a22e39b 100644 --- a/benchmarks/source-cosmicjs/gatsby-node.js +++ b/benchmarks/source-cosmicjs/gatsby-node.js @@ -1,15 +1,9 @@ const kebabCase = require(`lodash.kebabcase`) -function unstable_shouldOnCreateNode({node}) { - return node.internal.type === "node__article" -} - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (unstable_shouldOnCreateNode({node})) { + if (node.internal.type === 'node__article') { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } @@ -33,13 +27,13 @@ exports.createPages = async ({ actions, graphql, reporter }) => { reporter.panicOnBuild(result.errors) } - result.data.articles.edges.map((article) => { + result.data.articles.edges.map(article => { createPage({ path: article.node.slug, component: require.resolve(`./src/templates/article.js`), context: { slug: article.node.slug, - }, + } }) }) -} +} \ No newline at end of file diff --git a/benchmarks/source-datocms/gatsby-node.js b/benchmarks/source-datocms/gatsby-node.js index 4e53a79c06bb2..75f590e92c7a0 100644 --- a/benchmarks/source-datocms/gatsby-node.js +++ b/benchmarks/source-datocms/gatsby-node.js @@ -1,15 +1,9 @@ const kebabCase = require(`lodash.kebabcase`) -function unstable_shouldOnCreateNode({node}) { - return node.internal.type === `node__article` -} - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (unstable_shouldOnCreateNode({node})) { + if (node.internal.type === `node__article`) { createNodeField({ node, name: `slug`, value: kebabCase(node.title) }) } } diff --git a/benchmarks/source-drupal/gatsby-node.js b/benchmarks/source-drupal/gatsby-node.js index ca3007f4bad09..d161fa76d59a7 100644 --- a/benchmarks/source-drupal/gatsby-node.js +++ b/benchmarks/source-drupal/gatsby-node.js @@ -1,15 +1,9 @@ const kebabCase = require(`lodash.kebabcase`) -function unstable_shouldOnCreateNode({node}) { - return node.internal.type === `node__article` -} - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (unstable_shouldOnCreateNode({node})) { + if (node.internal.type === "node__article") { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } diff --git a/benchmarks/source-flotiq/gatsby-node.js b/benchmarks/source-flotiq/gatsby-node.js index 7b29474d0da07..26f4f739e91fe 100644 --- a/benchmarks/source-flotiq/gatsby-node.js +++ b/benchmarks/source-flotiq/gatsby-node.js @@ -1,15 +1,9 @@ const kebabCase = require(`lodash.kebabcase`) -function unstable_shouldOnCreateNode({node}) { - return node.internal.type === `node__article` -} - -exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode - exports.onCreateNode = ({ actions, node }) => { const { createNodeField } = actions - if (unstable_shouldOnCreateNode({node})) { + if (node.internal.type === "node__article") { createNodeField({ node, name: "slug", value: kebabCase(node.title) }) } } diff --git a/packages/gatsby-plugin-mdx/gatsby-node.js b/packages/gatsby-plugin-mdx/gatsby-node.js index 247fbaf343302..e39974ed0a1c9 100644 --- a/packages/gatsby-plugin-mdx/gatsby-node.js +++ b/packages/gatsby-plugin-mdx/gatsby-node.js @@ -15,7 +15,7 @@ const { exports.sourceNodes = require(`./gatsby/source-nodes`) /** - * Check whether the Create Mdx nodes from MDX files. + * Check whether to create Mdx nodes from MDX files. */ exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode