Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: implement shouldOnCreateNode for all our plugins/benchmarks #27545

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/gatsby-plugin-mdx/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 21 additions & 14 deletions packages/gatsby-plugin-mdx/gatsby/on-create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
21 changes: 12 additions & 9 deletions packages/gatsby-transformer-asciidoc/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -138,4 +140,5 @@ const extractPageAttributes = allAttributes =>
return pageAttributes
}, {})

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
20 changes: 13 additions & 7 deletions packages/gatsby-transformer-csv/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -79,4 +84,5 @@ async function onCreateNode(
return
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
26 changes: 15 additions & 11 deletions packages/gatsby-transformer-documentationjs/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
40 changes: 35 additions & 5 deletions packages/gatsby-transformer-excel/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -86,4 +115,5 @@ async function onCreateNode(
return
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
25 changes: 15 additions & 10 deletions packages/gatsby-transformer-hjson/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ 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,
loadNodeContent,
createNodeId,
createContentDigest,
}) {
if (!unstable_shouldOnCreateNode({ node })) {
return
}

const { createNode, createParentChildLink } = actions

function transformObject(obj, id, type) {
Expand All @@ -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)

Expand All @@ -59,4 +63,5 @@ async function onCreateNode({
}
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -135,4 +140,5 @@ async function onCreateNode({
}
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`,
Expand Down Expand Up @@ -150,4 +154,5 @@ async function onCreateNode({
}
}

exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode
exports.onCreateNode = onCreateNode