Skip to content

Commit df914d9

Browse files
authoredOct 20, 2020
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
 

Diff for: ‎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

Diff for: ‎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

Diff for: ‎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

Diff for: ‎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

Diff for: ‎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, {

Diff for: ‎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

Diff for: ‎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

Diff for: ‎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

Diff for: ‎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

Diff for: ‎packages/gatsby-transformer-json/src/gatsby-node.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
const _ = require(`lodash`)
22
const path = require(`path`)
33

4+
function unstable_shouldOnCreateNode({ node }) {
5+
// We only care about JSON content.
6+
return node.internal.mediaType === `application/json`
7+
}
8+
49
async function onCreateNode(
510
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
611
pluginOptions
712
) {
13+
if (!unstable_shouldOnCreateNode({ node })) {
14+
return
15+
}
16+
817
function getType({ node, object, isArray }) {
918
if (pluginOptions && _.isFunction(pluginOptions.typeName)) {
1019
return pluginOptions.typeName({ node, object, isArray })
@@ -36,11 +45,6 @@ async function onCreateNode(
3645

3746
const { createNode, createParentChildLink } = actions
3847

39-
// We only care about JSON content.
40-
if (node.internal.mediaType !== `application/json`) {
41-
return
42-
}
43-
4448
const content = await loadNodeContent(node)
4549
let parsedContent
4650
try {
@@ -71,4 +75,5 @@ async function onCreateNode(
7175
}
7276
}
7377

78+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
7479
exports.onCreateNode = onCreateNode

Diff for: ‎packages/gatsby-transformer-pdf/src/gatsby-node.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const Promise = require(`bluebird`)
22
const PDFParser = require(`pdf2json`)
33

4+
function unstable_shouldOnCreateNode({ node }) {
5+
// Filter out non-pdf content
6+
return node.extension === `pdf`
7+
}
8+
49
const convertToJson = path =>
510
new Promise((res, rej) => {
611
const pdfParser = new PDFParser(this, 1)
@@ -21,13 +26,12 @@ async function onCreateNode({
2126
createNodeId,
2227
createContentDigest,
2328
}) {
24-
const { createNode, createParentChildLink } = actions
25-
26-
// Filter out non-pdf content
27-
if (node.extension !== `pdf`) {
29+
if (!unstable_shouldOnCreateNode({ node })) {
2830
return
2931
}
3032

33+
const { createNode, createParentChildLink } = actions
34+
3135
let parsedContent = await convertToJson(node.absolutePath)
3236

3337
const pdfNode = {
@@ -46,4 +50,5 @@ async function onCreateNode({
4650
createParentChildLink({ parent: node, child: pdfNode })
4751
}
4852

53+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
4954
exports.onCreateNode = onCreateNode

Diff for: ‎packages/gatsby-transformer-react-docgen/src/__tests__/on-node-create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs"
22
import { groupBy } from "lodash"
3-
import onCreateNode from "../on-node-create"
3+
import { onCreateNode } from "../on-node-create"
44
import path from "path"
55

66
const readFile = file =>
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
exports.onCreateNode = require(`./on-node-create`).default
1+
const {
2+
onCreateNode,
3+
unstable_shouldOnCreateNode,
4+
} = require(`./on-node-create`)
5+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
6+
exports.onCreateNode = onCreateNode
27
exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).default

Diff for: ‎packages/gatsby-transformer-react-docgen/src/on-node-create.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ function createPropNodes(
8888
return node
8989
}
9090

91-
export default async function onCreateNode(
91+
export function unstable_shouldOnCreateNode({ node }) {
92+
return canParse(node)
93+
}
94+
95+
export async function onCreateNode(
9296
{
9397
node,
9498
loadNodeContent,
@@ -99,10 +103,10 @@ export default async function onCreateNode(
99103
},
100104
pluginOptions
101105
) {
102-
const { createNode, createParentChildLink } = actions
103-
104106
if (!canParse(node)) return
105107

108+
const { createNode, createParentChildLink } = actions
109+
106110
const content = await loadNodeContent(node)
107111

108112
let components

Diff for: ‎packages/gatsby-transformer-remark/src/on-node-create.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ module.exports.onCreateNode = async function onCreateNode(
1919
},
2020
pluginOptions
2121
) {
22-
const { createNode, createParentChildLink } = actions
23-
2422
// We only care about markdown content.
2523
if (!unstable_shouldOnCreateNode({ node })) {
2624
return {}
2725
}
2826

27+
const { createNode, createParentChildLink } = actions
28+
2929
const content = await loadNodeContent(node)
3030

3131
try {

Diff for: ‎packages/gatsby-transformer-screenshot/src/gatsby-node.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,27 @@ exports.onPreBootstrap = (
7676
})
7777
}
7878

79-
exports.onCreateNode = async (
80-
{ node, actions, store, cache, createNodeId, createContentDigest, getCache },
81-
pluginOptions
82-
) => {
83-
const { createNode, createParentChildLink } = actions
84-
79+
function unstable_shouldOnCreateNode({ node }, pluginOptions) {
8580
/*
8681
* Check if node is of a type we care about, and has a url field
8782
* (originally only checked sites.yml, hence including by default)
8883
*/
8984
const validNodeTypes = [`SitesYaml`].concat(pluginOptions.nodeTypes || [])
90-
if (!validNodeTypes.includes(node.internal.type) || !node.url) {
85+
return validNodeTypes.includes(node.internal.type) && node.url
86+
}
87+
88+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
89+
90+
exports.onCreateNode = async (
91+
{ node, actions, store, cache, createNodeId, createContentDigest, getCache },
92+
pluginOptions
93+
) => {
94+
if (!unstable_shouldOnCreateNode({ node }, pluginOptions)) {
9195
return
9296
}
9397

98+
const { createNode, createParentChildLink } = actions
99+
94100
try {
95101
const screenshotNode = await new Promise((resolve, reject) => {
96102
screenshotQueue

Diff for: ‎packages/gatsby-transformer-sharp/src/on-node-create.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ function unstable_shouldOnCreateNode({ node }) {
44
return !!supportedExtensions[node.extension]
55
}
66

7+
module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
8+
79
module.exports.onCreateNode = async function onCreateNode({
810
node,
911
actions,
1012
createNodeId,
1113
}) {
12-
const { createNode, createParentChildLink } = actions
13-
1414
if (!unstable_shouldOnCreateNode({ node })) {
1515
return
1616
}
1717

18+
const { createNode, createParentChildLink } = actions
19+
1820
const imageNode = {
1921
id: createNodeId(`${node.id} >> ImageSharp`),
2022
children: [],
@@ -30,5 +32,3 @@ module.exports.onCreateNode = async function onCreateNode({
3032

3133
return
3234
}
33-
34-
module.exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode

Diff for: ‎packages/gatsby-transformer-toml/src/gatsby-node.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
const toml = require(`toml`)
22
const _ = require(`lodash`)
33

4+
function unstable_shouldOnCreateNode({ node }) {
5+
// Filter out non-toml content
6+
// Currently TOML files are considered null in 'mime-db'
7+
// Hence the extension test instead of mediaType test
8+
return node.extension === `toml`
9+
}
10+
411
async function onCreateNode({
512
node,
613
actions,
714
loadNodeContent,
815
createNodeId,
916
createContentDigest,
1017
}) {
11-
const { createNode, createParentChildLink } = actions
12-
// Filter out non-toml content
13-
// Currently TOML files are considered null in 'mime-db'
14-
// Hence the extension test instead of mediaType test
15-
if (node.extension !== `toml`) {
18+
if (!unstable_shouldOnCreateNode({ node })) {
1619
return
1720
}
21+
22+
const { createNode, createParentChildLink } = actions
23+
1824
// Load TOML contents
1925
const content = await loadNodeContent(node)
2026
// Parse
@@ -45,4 +51,5 @@ async function onCreateNode({
4551
return
4652
}
4753

54+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
4855
exports.onCreateNode = onCreateNode

Diff for: ‎packages/gatsby-transformer-xml/src/gatsby-node.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
const parseXml = require(`xml-parser`)
22
const _ = require(`lodash`)
33

4+
function unstable_shouldOnCreateNode({ node }) {
5+
// We only care about XML content.
6+
return [`application/xml`, `text/xml`].includes(node.internal.mediaType)
7+
}
8+
49
async function onCreateNode({
510
node,
611
actions,
712
loadNodeContent,
813
createNodeId,
914
createContentDigest,
1015
}) {
11-
const { createNode, createParentChildLink } = actions
12-
13-
// We only care about XML content.
14-
if (![`application/xml`, `text/xml`].includes(node.internal.mediaType)) {
16+
if (!unstable_shouldOnCreateNode({ node })) {
1517
return
1618
}
19+
20+
const { createNode, createParentChildLink } = actions
21+
1722
const rawXml = await loadNodeContent(node)
1823
const parsedXml = parseXml(rawXml)
1924
const nodeArray = parsedXml.root.children.map((obj, i) => {
@@ -42,4 +47,5 @@ async function onCreateNode({
4247
return
4348
}
4449

50+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
4551
exports.onCreateNode = onCreateNode

Diff for: ‎packages/gatsby-transformer-yaml/src/gatsby-node.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ const jsYaml = require(`js-yaml`)
22
const _ = require(`lodash`)
33
const path = require(`path`)
44

5+
function unstable_shouldOnCreateNode({ node }) {
6+
return node.internal.mediaType === `text/yaml`
7+
}
8+
59
async function onCreateNode(
610
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
711
pluginOptions
812
) {
13+
if (!unstable_shouldOnCreateNode({ node })) {
14+
return
15+
}
16+
917
function getType({ node, object, isArray }) {
1018
if (pluginOptions && _.isFunction(pluginOptions.typeName)) {
1119
return pluginOptions.typeName({ node, object, isArray })
@@ -37,10 +45,6 @@ async function onCreateNode(
3745

3846
const { createNode, createParentChildLink } = actions
3947

40-
if (node.internal.mediaType !== `text/yaml`) {
41-
return
42-
}
43-
4448
const content = await loadNodeContent(node)
4549
const parsedContent = jsYaml.load(content)
4650

@@ -61,4 +65,5 @@ async function onCreateNode(
6165
}
6266
}
6367

68+
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
6469
exports.onCreateNode = onCreateNode

0 commit comments

Comments
 (0)
Please sign in to comment.