perf(gatsby-plugin-mdx): prevent dupe file writes and simplify Babel step #25808
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The cacheScope function would try to write the same file over and over again which is 10x more expensive than caching whether or not that file had already been written (or even fs.existsSync, but caching is much cheaper). Still needs some tweaking. This doesn't change much, it's just 4s on the 64k benchmark, so 0.1%, although it might be better for your disk :)
I'm not a super fan of the Map for caching this, though, since this means having to juggle it from a central point somehow. Or putting it global, which might also not be desirable. Additionally, the map needs to do file path + contents, not just contents (I think... but perhaps that's actually not necessary since the digest of the contents dictates the path).
Dropping the babel step in favor of a regex scrub saves a little bit more. Still only like 30s on the 64k benchmark (~1%).
The idea for the scrubbing is that the inputs must be valid ES6 import statements, which will have a
from
keyword followed by a quoted string. This is an invariant in the syntax and so it should be safe to run this regex on it rather than turn on the whole Babel engine. Note that the input strings may contain multiple import statements, hence theg
flag and non-greedy match.Need to verify this more thoroughly so shelving it before I go on vacation.
(This'll be two separate PRs)