GeezerVison is showing github.com — perf: implement shouldOnCreateNode for all our plugins/benchmarks (#2… · gatsbyjs/gatsby@df914d9
File treeExpand file treeCollapse file tree Open diff view settings gatsby-transformer-asciidoc/src gatsby-transformer-csv/src gatsby-transformer-documentationjs/src gatsby-transformer-excel/src gatsby-transformer-hjson/src gatsby-transformer-javascript-frontmatter/src gatsby-transformer-javascript-static-exports/src gatsby-transformer-json/src gatsby-transformer-pdf/src gatsby-transformer-react-docgen/src gatsby-transformer-remark/src gatsby-transformer-screenshot/src gatsby-transformer-sharp/src gatsby-transformer-toml/src gatsby-transformer-xml/src gatsby-transformer-yaml/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-plugin-mdx/gatsby-node.js +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,15 +4,25 @@ const { MDX_SCOPES_LOCATION } = require(`./constants`) | 4 | 4 | const defaultOptions = require(`./utils/default-options`) | 5 | 5 | const fs = require(`fs`) | 6 | 6 |
| | 7 | +const { | | 8 | + onCreateNode, | | 9 | + unstable_shouldOnCreateNode | | 10 | +} = require(`./gatsby/on-create-node`) | | 11 | + | 7 | 12 | /** | 8 | 13 | * Create Mdx types and resolvers | 9 | 14 | */ | 10 | 15 | exports.sourceNodes = require(`./gatsby/source-nodes`) | 11 | 16 |
| | 17 | +/** | | 18 | + * Check whether to create Mdx nodes from MDX files. | | 19 | + */ | | 20 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | | 21 | + | 12 | 22 | /** | 13 | 23 | * Create Mdx nodes from MDX files. | 14 | 24 | */ | 15 | | -exports.onCreateNode = require(`./gatsby/on-create-node`) | | 25 | +exports.onCreateNode = onCreateNode | 16 | 26 |
| 17 | 27 | /** | 18 | 28 | * Add frontmatter as page context for MDX pages | |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-plugin-mdx/gatsby/on-create-node.js +21-14Lines changed: 21 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,26 @@ const { findImports } = require(`../utils/gen-mdx`) | 10 | 10 |
| 11 | 11 | const contentDigest = val => createContentDigest(val) | 12 | 12 |
| 13 | | -module.exports = async ( | | 13 | +function unstable_shouldOnCreateNode({ node }, pluginOptions) { | | 14 | + const options = defaultOptions(pluginOptions) | | 15 | + | | 16 | + return _unstable_shouldOnCreateNode({ node }, options) | | 17 | +} | | 18 | + | | 19 | +function _unstable_shouldOnCreateNode({ node }, options) { | | 20 | + // options check to stop transformation of the node | | 21 | + if (options.shouldBlockNodeFromTransformation(node)) { | | 22 | + return false | | 23 | + } | | 24 | + | | 25 | + return node.internal.type === `File` | | 26 | + ? options.extensions.includes(node.ext) | | 27 | + : options.mediaTypes.includes(node.internal.mediaType) | | 28 | +} | | 29 | + | | 30 | +module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | | 31 | + | | 32 | +module.exports.onCreateNode = async ( | 14 | 33 | { | 15 | 34 | node, | 16 | 35 | loadNodeContent, | @@ -28,19 +47,7 @@ module.exports = async ( | 28 | 47 | const { createNode, createParentChildLink } = actions | 29 | 48 | const options = defaultOptions(pluginOptions) | 30 | 49 |
| 31 | | - // options check to stop transformation of the node | 32 | | - if (options.shouldBlockNodeFromTransformation(node)) { | 33 | | - return | 34 | | - } | 35 | | - | 36 | | - // if we shouldn't process this node, then return | 37 | | - if ( | 38 | | - !(node.internal.type === `File` && options.extensions.includes(node.ext)) && | 39 | | - !( | 40 | | - node.internal.type !== `File` && | 41 | | - options.mediaTypes.includes(node.internal.mediaType) | 42 | | - ) | 43 | | - ) { | | 50 | + if (!_unstable_shouldOnCreateNode({ node }, options)) { | 44 | 51 | return | 45 | 52 | } | 46 | 53 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-asciidoc/src/gatsby-node.js +12-9Lines changed: 12 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | const asciidoc = require(`asciidoctor`)() | 2 | 2 | const _ = require(`lodash`) | 3 | 3 |
| | 4 | +function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { | | 5 | + const extensionsConfig = pluginOptions.fileExtensions | | 6 | + | | 7 | + // make extensions configurable and use adoc and asciidoc as default | | 8 | + const supportedExtensions = | | 9 | + extensionsConfig instanceof Array ? extensionsConfig : [`adoc`, `asciidoc`] | | 10 | + | | 11 | + return supportedExtensions.includes(node.extension) | | 12 | +} | | 13 | + | 4 | 14 | async function onCreateNode( | 5 | 15 | { | 6 | 16 | node, | @@ -13,15 +23,7 @@ async function onCreateNode( | 13 | 23 | }, | 14 | 24 | pluginOptions | 15 | 25 | ) { | 16 | | - const extensionsConfig = pluginOptions.fileExtensions | 17 | | - | 18 | | - // make extensions configurable and use adoc and asciidoc as default | 19 | | - const supportedExtensions = | 20 | | - typeof extensionsConfig !== `undefined` && extensionsConfig instanceof Array | 21 | | - ? extensionsConfig | 22 | | - : [`adoc`, `asciidoc`] | 23 | | - | 24 | | - if (!supportedExtensions.includes(node.extension)) { | | 26 | + if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { | 25 | 27 | return | 26 | 28 | } | 27 | 29 |
| @@ -138,4 +140,5 @@ const extractPageAttributes = allAttributes => | 138 | 140 | return pageAttributes | 139 | 141 | }, {}) | 140 | 142 |
| | 143 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 141 | 144 | exports.onCreateNode = onCreateNode |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-csv/src/gatsby-node.js +13-7Lines changed: 13 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,20 +8,25 @@ const convertToJson = (data, options) => | 8 | 8 | .fromString(data) | 9 | 9 | .then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`)) | 10 | 10 |
| | 11 | +function unstable_shouldOnCreateNode({ node }, pluginOptions = {}) { | | 12 | + const { extension } = node | | 13 | + const { extensions } = pluginOptions | | 14 | + | | 15 | + return extensions ? extensions.includes(extension) : extension === `csv` | | 16 | +} | | 17 | + | 11 | 18 | async function onCreateNode( | 12 | 19 | { node, actions, loadNodeContent, createNodeId, createContentDigest }, | 13 | 20 | pluginOptions | 14 | 21 | ) { | | 22 | + if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) { | | 23 | + return | | 24 | + } | | 25 | + | 15 | 26 | const { createNode, createParentChildLink } = actions | 16 | 27 |
| 17 | 28 | // Destructure out our custom options | 18 | | - const { typeName, nodePerFile, extensions, ...options } = pluginOptions || {} | 19 | | - | 20 | | - // Filter out unwanted content | 21 | | - const filterExtensions = extensions ?? [`csv`] | 22 | | - if (!filterExtensions.includes(node.extension)) { | 23 | | - return | 24 | | - } | | 29 | + const { typeName, nodePerFile, ...options } = pluginOptions || {} | 25 | 30 |
| 26 | 31 | // Load file contents | 27 | 32 | const content = await loadNodeContent(node) | @@ -79,4 +84,5 @@ async function onCreateNode( | 79 | 84 | return | 80 | 85 | } | 81 | 86 |
| | 87 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 82 | 88 | exports.onCreateNode = onCreateNode |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-documentationjs/src/gatsby-node.js +15-11Lines changed: 15 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -185,25 +185,29 @@ exports.createResolvers = ({ createResolvers }) => { | 185 | 185 | }) | 186 | 186 | } | 187 | 187 |
| | 188 | +function unstable_shouldOnCreateNode({ node }) { | | 189 | + return ( | | 190 | + node.internal.type === `File` && | | 191 | + (node.internal.mediaType === `application/javascript` || | | 192 | + node.extension === `tsx` || | | 193 | + node.extension === `ts`) | | 194 | + ) | | 195 | +} | | 196 | + | | 197 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | | 198 | + | 188 | 199 | /** | 189 | 200 | * Implement the onCreateNode API to create documentation.js nodes | 190 | 201 | * @param {Object} super this is a super param | 191 | 202 | */ | 192 | 203 | exports.onCreateNode = async ({ node, actions, ...helpers }) => { | 193 | | - const { createNodeId, createContentDigest } = helpers | 194 | | - const { createNode, createParentChildLink } = actions | 195 | | - | 196 | | - if ( | 197 | | - node.internal.type !== `File` || | 198 | | - !( | 199 | | - node.internal.mediaType === `application/javascript` || | 200 | | - node.extension === `tsx` || | 201 | | - node.extension === `ts` | 202 | | - ) | 203 | | - ) { | | 204 | + if (!unstable_shouldOnCreateNode({ node })) { | 204 | 205 | return null | 205 | 206 | } | 206 | 207 |
| | 208 | + const { createNodeId, createContentDigest } = helpers | | 209 | + const { createNode, createParentChildLink } = actions | | 210 | + | 207 | 211 | let documentationJson | 208 | 212 | try { | 209 | 213 | documentationJson = await documentation.build(node.absolutePath, { | |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-excel/src/gatsby-node.js +35-5Lines changed: 35 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,17 +9,46 @@ function _loadNodeContent(fileNode, fallback) { | 9 | 9 | : fallback(fileNode) | 10 | 10 | } | 11 | 11 |
| | 12 | +const extensions = [ | | 13 | + `xls`, | | 14 | + `xlsx`, | | 15 | + `xlsm`, | | 16 | + `xlsb`, | | 17 | + `xml`, | | 18 | + `xlw`, | | 19 | + `xlc`, | | 20 | + `csv`, | | 21 | + `txt`, | | 22 | + `dif`, | | 23 | + `sylk`, | | 24 | + `slk`, | | 25 | + `prn`, | | 26 | + `ods`, | | 27 | + `fods`, | | 28 | + `uos`, | | 29 | + `dbf`, | | 30 | + `wks`, | | 31 | + `123`, | | 32 | + `wq1`, | | 33 | + `qpw`, | | 34 | + `htm`, | | 35 | + `html`, | | 36 | +] | | 37 | + | | 38 | +function unstable_shouldOnCreateNode({ node }) { | | 39 | + return extensions.includes((node.extension || ``).toLowerCase()) | | 40 | +} | | 41 | + | 12 | 42 | async function onCreateNode( | 13 | 43 | { node, actions, loadNodeContent, createNodeId, createContentDigest }, | 14 | 44 | options = {} | 15 | 45 | ) { | 16 | | - const { createNode, createParentChildLink } = actions | 17 | | - 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( | 18 | | - `|` | 19 | | - ) | 20 | | - if (extensions.indexOf((node.extension || ``).toLowerCase()) == -1) { | | 46 | + if (!unstable_shouldOnCreateNode({ node })) { | 21 | 47 | return | 22 | 48 | } | | 49 | + | | 50 | + const { createNode, createParentChildLink } = actions | | 51 | + | 23 | 52 | // Load binary string | 24 | 53 | const content = await _loadNodeContent(node, loadNodeContent) | 25 | 54 |
| @@ -86,4 +115,5 @@ async function onCreateNode( | 86 | 115 | return | 87 | 116 | } | 88 | 117 |
| | 118 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 89 | 119 | exports.onCreateNode = onCreateNode |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-hjson/src/gatsby-node.js +15-10Lines changed: 15 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,13 +2,27 @@ const _ = require(`lodash`) | 2 | 2 | const path = require(`path`) | 3 | 3 | const HJSON = require(`hjson`) | 4 | 4 |
| | 5 | +function unstable_shouldOnCreateNode({ node }) { | | 6 | + // We only care about HJSON content. | | 7 | + // NOTE the mime package does not recognize HJSON yet | | 8 | + // See RFC https://hjson.org/rfc.html#rfc.section.1.3 | | 9 | + return ( | | 10 | + node.internal.mediaType === `text/hjson` || | | 11 | + node.internal.mediaType === `application/hjson` | | 12 | + ) | | 13 | +} | | 14 | + | 5 | 15 | async function onCreateNode({ | 6 | 16 | node, | 7 | 17 | actions, | 8 | 18 | loadNodeContent, | 9 | 19 | createNodeId, | 10 | 20 | createContentDigest, | 11 | 21 | }) { | | 22 | + if (!unstable_shouldOnCreateNode({ node })) { | | 23 | + return | | 24 | + } | | 25 | + | 12 | 26 | const { createNode, createParentChildLink } = actions | 13 | 27 |
| 14 | 28 | function transformObject(obj, id, type) { | @@ -27,16 +41,6 @@ async function onCreateNode({ | 27 | 41 | createParentChildLink({ parent: node, child: jsonNode }) | 28 | 42 | } | 29 | 43 |
| 30 | | - // We only care about HJSON content. | 31 | | - // NOTE the mime package does not recognize HJSON yet | 32 | | - // See RFC https://hjson.org/rfc.html#rfc.section.1.3 | 33 | | - if ( | 34 | | - node.internal.mediaType !== `text/hjson` && | 35 | | - node.internal.mediaType !== `application/hjson` | 36 | | - ) { | 37 | | - return | 38 | | - } | 39 | | - | 40 | 44 | const content = await loadNodeContent(node) | 41 | 45 | const parsedContent = HJSON.parse(content) | 42 | 46 |
| @@ -59,4 +63,5 @@ async function onCreateNode({ | 59 | 63 | } | 60 | 64 | } | 61 | 65 |
| | 66 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 62 | 67 | exports.onCreateNode = onCreateNode |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js +11-5Lines changed: 11 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,20 +2,25 @@ const _ = require(`lodash`) | 2 | 2 | const babylon = require(`@babel/parser`) | 3 | 3 | const traverse = require(`@babel/traverse`).default | 4 | 4 |
| | 5 | +const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`] | | 6 | + | | 7 | +function unstable_shouldOnCreateNode({ node }) { | | 8 | + // This only processes JavaScript and TypeScript files. | | 9 | + return fileExtsToProcess.includes(node.extension) | | 10 | +} | | 11 | + | 5 | 12 | async function onCreateNode({ | 6 | 13 | node, | 7 | 14 | actions, | 8 | 15 | loadNodeContent, | 9 | 16 | createContentDigest, | 10 | 17 | }) { | 11 | | - const { createNode, createParentChildLink } = actions | 12 | | - const fileExtsToProcess = [`js`, `jsx`, `ts`, `tsx`] | 13 | | - | 14 | | - // This only processes JavaScript and TypeScript files. | 15 | | - if (!_.includes(fileExtsToProcess, node.extension)) { | | 18 | + if (!unstable_shouldOnCreateNode({ node })) { | 16 | 19 | return | 17 | 20 | } | 18 | 21 |
| | 22 | + const { createNode, createParentChildLink } = actions | | 23 | + | 19 | 24 | const code = await loadNodeContent(node) | 20 | 25 | const options = { | 21 | 26 | sourceType: `module`, | @@ -135,4 +140,5 @@ async function onCreateNode({ | 135 | 140 | } | 136 | 141 | } | 137 | 142 |
| | 143 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 138 | 144 | exports.onCreateNode = onCreateNode |
Collapse file Copy file name to clipboardExpand all lines: packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,11 @@ const _ = require(`lodash`) | 2 | 2 | const babylon = require(`@babel/parser`) | 3 | 3 | const traverse = require(`@babel/traverse`).default | 4 | 4 |
| | 5 | +function unstable_shouldOnCreateNode({ node }) { | | 6 | + // This only processes JavaScript files. | | 7 | + return node.internal.mediaType === `application/javascript` | | 8 | +} | | 9 | + | 5 | 10 | async function onCreateNode({ | 6 | 11 | node, | 7 | 12 | getNode, | @@ -10,13 +15,12 @@ async function onCreateNode({ | 10 | 15 | createNodeId, | 11 | 16 | createContentDigest, | 12 | 17 | }) { | 13 | | - const { createNode, createParentChildLink } = actions | 14 | | - | 15 | | - // This only processes JavaScript files. | 16 | | - if (node.internal.mediaType !== `application/javascript`) { | | 18 | + if (!unstable_shouldOnCreateNode({ node })) { | 17 | 19 | return | 18 | 20 | } | 19 | 21 |
| | 22 | + const { createNode, createParentChildLink } = actions | | 23 | + | 20 | 24 | const code = await loadNodeContent(node) | 21 | 25 | const options = { | 22 | 26 | sourceType: `unambigious`, | @@ -150,4 +154,5 @@ async function onCreateNode({ | 150 | 154 | } | 151 | 155 | } | 152 | 156 |
| | 157 | +exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode | 153 | 158 | exports.onCreateNode = onCreateNode |
|