Skip to content

Commit df914d9

Browse files
authored
perf: implement shouldOnCreateNode for all our plugins/benchmarks (#27545)
* perf: implement shouldOnCreateNode for all our plugins * Drop changes in /benchmarks, fix typo
1 parent 73d50b7 commit df914d9

File tree

20 files changed

+225
-106
lines changed

20 files changed

+225
-106
lines changed

packages/gatsby-plugin-mdx/gatsby-node.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ const { MDX_SCOPES_LOCATION } = require(`./constants`)
44
const defaultOptions = require(`./utils/default-options`)
55
const fs = require(`fs`)
66

7+
const {
8+
onCreateNode,
9+
unstable_shouldOnCreateNode
10+
} = require(`./gatsby/on-create-node`)
11+
712
/**
813
* Create Mdx types and resolvers
914
*/
1015
exports.sourceNodes = require(`./gatsby/source-nodes`)
1116

17+
/**
18+
* Check whether to create Mdx nodes from MDX files.
19+
*/
20+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
21+
1222
/**
1323
* Create Mdx nodes from MDX files.
1424
*/
15-
exports.onCreateNode = require(`./gatsby/on-create-node`)
25+
exports.onCreateNode = onCreateNode
1626

1727
/**
1828
* Add frontmatter as page context for MDX pages

packages/gatsby-plugin-mdx/gatsby/on-create-node.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,26 @@ const { findImports } = require(`../utils/gen-mdx`)
1010

1111
const contentDigest = val => createContentDigest(val)
1212

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 (
1433
{
1534
node,
1635
loadNodeContent,
@@ -28,19 +47,7 @@ module.exports = async (
2847
const { createNode, createParentChildLink } = actions
2948
const options = defaultOptions(pluginOptions)
3049

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)) {
4451
return
4552
}
4653

packages/gatsby-transformer-asciidoc/src/gatsby-node.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
const asciidoc = require(`asciidoctor`)()
22
const _ = require(`lodash`)
33

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+
414
async function onCreateNode(
515
{
616
node,
@@ -13,15 +23,7 @@ async function onCreateNode(
1323
},
1424
pluginOptions
1525
) {
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)) {
2527
return
2628
}
2729

@@ -138,4 +140,5 @@ const extractPageAttributes = allAttributes =>
138140
return pageAttributes
139141
}, {})
140142

143+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
141144
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-csv/src/gatsby-node.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ const convertToJson = (data, options) =>
88
.fromString(data)
99
.then(jsonData => jsonData, new Error(`CSV to JSON conversion failed!`))
1010

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+
1118
async function onCreateNode(
1219
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
1320
pluginOptions
1421
) {
22+
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
23+
return
24+
}
25+
1526
const { createNode, createParentChildLink } = actions
1627

1728
// 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 || {}
2530

2631
// Load file contents
2732
const content = await loadNodeContent(node)
@@ -79,4 +84,5 @@ async function onCreateNode(
7984
return
8085
}
8186

87+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
8288
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-documentationjs/src/gatsby-node.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,29 @@ exports.createResolvers = ({ createResolvers }) => {
185185
})
186186
}
187187

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+
188199
/**
189200
* Implement the onCreateNode API to create documentation.js nodes
190201
* @param {Object} super this is a super param
191202
*/
192203
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 })) {
204205
return null
205206
}
206207

208+
const { createNodeId, createContentDigest } = helpers
209+
const { createNode, createParentChildLink } = actions
210+
207211
let documentationJson
208212
try {
209213
documentationJson = await documentation.build(node.absolutePath, {

packages/gatsby-transformer-excel/src/gatsby-node.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,46 @@ function _loadNodeContent(fileNode, fallback) {
99
: fallback(fileNode)
1010
}
1111

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+
1242
async function onCreateNode(
1343
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
1444
options = {}
1545
) {
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 })) {
2147
return
2248
}
49+
50+
const { createNode, createParentChildLink } = actions
51+
2352
// Load binary string
2453
const content = await _loadNodeContent(node, loadNodeContent)
2554

@@ -86,4 +115,5 @@ async function onCreateNode(
86115
return
87116
}
88117

118+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
89119
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-hjson/src/gatsby-node.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@ const _ = require(`lodash`)
22
const path = require(`path`)
33
const HJSON = require(`hjson`)
44

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+
515
async function onCreateNode({
616
node,
717
actions,
818
loadNodeContent,
919
createNodeId,
1020
createContentDigest,
1121
}) {
22+
if (!unstable_shouldOnCreateNode({ node })) {
23+
return
24+
}
25+
1226
const { createNode, createParentChildLink } = actions
1327

1428
function transformObject(obj, id, type) {
@@ -27,16 +41,6 @@ async function onCreateNode({
2741
createParentChildLink({ parent: node, child: jsonNode })
2842
}
2943

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-
4044
const content = await loadNodeContent(node)
4145
const parsedContent = HJSON.parse(content)
4246

@@ -59,4 +63,5 @@ async function onCreateNode({
5963
}
6064
}
6165

66+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
6267
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ const _ = require(`lodash`)
22
const babylon = require(`@babel/parser`)
33
const traverse = require(`@babel/traverse`).default
44

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+
512
async function onCreateNode({
613
node,
714
actions,
815
loadNodeContent,
916
createContentDigest,
1017
}) {
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 })) {
1619
return
1720
}
1821

22+
const { createNode, createParentChildLink } = actions
23+
1924
const code = await loadNodeContent(node)
2025
const options = {
2126
sourceType: `module`,
@@ -135,4 +140,5 @@ async function onCreateNode({
135140
}
136141
}
137142

143+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
138144
exports.onCreateNode = onCreateNode

packages/gatsby-transformer-javascript-static-exports/src/gatsby-node.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ const _ = require(`lodash`)
22
const babylon = require(`@babel/parser`)
33
const traverse = require(`@babel/traverse`).default
44

5+
function unstable_shouldOnCreateNode({ node }) {
6+
// This only processes JavaScript files.
7+
return node.internal.mediaType === `application/javascript`
8+
}
9+
510
async function onCreateNode({
611
node,
712
getNode,
@@ -10,13 +15,12 @@ async function onCreateNode({
1015
createNodeId,
1116
createContentDigest,
1217
}) {
13-
const { createNode, createParentChildLink } = actions
14-
15-
// This only processes JavaScript files.
16-
if (node.internal.mediaType !== `application/javascript`) {
18+
if (!unstable_shouldOnCreateNode({ node })) {
1719
return
1820
}
1921

22+
const { createNode, createParentChildLink } = actions
23+
2024
const code = await loadNodeContent(node)
2125
const options = {
2226
sourceType: `unambigious`,
@@ -150,4 +154,5 @@ async function onCreateNode({
150154
}
151155
}
152156

157+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
153158
exports.onCreateNode = onCreateNode

0 commit comments

Comments
 (0)