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(gatsby-plugin-mdx): Stop clobbering the same file over and over again #27974

Merged
merged 4 commits into from
Jan 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/gatsby-plugin-mdx/gatsby/on-create-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require(`fs`)
const fs = require(`fs-extra`)
const path = require(`path`)
const babel = require(`@babel/core`)
const { createContentDigest } = require(`gatsby-core-utils`)
Expand Down Expand Up @@ -157,6 +157,8 @@ async function onCreateNodeLessBabel(
})
}

const writeCache = new Set()

async function cacheScope({
cache,
scopeImports,
Expand All @@ -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`) {
Expand All @@ -189,7 +201,7 @@ export default { ${scopeIdentifiers.join(`, `)} }`
`${createContentDigest(scopeFileContent)}.js`
)

fs.writeFileSync(filePath, scopeFileContent)
await fs.writeFile(filePath, scopeFileContent)
}

const declare = require(`@babel/helper-plugin-utils`).declare
Expand Down