From df914d94a7c47c6082b6f165eb44dc6e15e12c7d Mon Sep 17 00:00:00 2001 From: Peter van der Zee <209817+pvdz@users.noreply.github.com> Date: Tue, 20 Oct 2020 14:22:06 +0200 Subject: [PATCH] perf: implement shouldOnCreateNode for all our plugins/benchmarks (#27545) * perf: implement shouldOnCreateNode for all our plugins * Drop changes in /benchmarks, fix typo --- 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 ++++-- 20 files changed, 225 insertions(+), 106 deletions(-) diff --git a/packages/gatsby-plugin-mdx/gatsby-node.js b/packages/gatsby-plugin-mdx/gatsby-node.js index 0035204c58d04..e39974ed0a1c9 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 to 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