From 5d4851f6bc9c55077c1848491db0e72c24c6cde0 Mon Sep 17 00:00:00 2001 From: Peter van der Zee <209817+pvdz@users.noreply.github.com> Date: Tue, 5 Jan 2021 17:44:13 +0100 Subject: [PATCH] perf(gatsby-plugin-mdx): Stop clobbering the same file over and over again (#27974) * perf(gatsby-plugin-mdx): Stop clobbering the same file over and over again * Undo the await part, can be separate PR Co-authored-by: gatsbybot (cherry picked from commit 3d100e306e59048ab7e980056c38815b8c9076c1) --- packages/gatsby-plugin-mdx/gatsby/on-create-node.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/gatsby-plugin-mdx/gatsby/on-create-node.js b/packages/gatsby-plugin-mdx/gatsby/on-create-node.js index 4735d2a1c1d01..8203ad71dfa82 100644 --- a/packages/gatsby-plugin-mdx/gatsby/on-create-node.js +++ b/packages/gatsby-plugin-mdx/gatsby/on-create-node.js @@ -157,6 +157,8 @@ async function onCreateNodeLessBabel( }) } +const writeCache = new Set() + async function cacheScope({ cache, scopeImports, @@ -169,6 +171,16 @@ async function cacheScope({ export default { ${scopeIdentifiers.join(`, `)} }` + // Multiple files sharing the same imports/exports will lead to the same file writes. + // Prevent writing the same content to the same file over and over again (reduces io pressure). + // This also prevents an expensive babel step whose outcome is based on this same value + if (writeCache.has(scopeFileContent)) { + return + } + + // Make sure other calls see this value being processed during async time + writeCache.add(scopeFileContent) + // if parent node is a file, convert relative imports to be // relative to new .cache location if (parentNode.internal.type === `File`) {