fix(gatsby-plugin-image): do not return from preprocessSource if nothing is changed #28678
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.
Description
It only makes sense to return the actual value from the
preprocessSource
if it changes the content. Otherwise, it causes a redundant babel parsing whengatsby-plugin-image
is used alongside other plugins, i.e.gatsby-plugin-mdx
.Some context. We run this
preprocessSource
hook here when extracting queries:gatsby/packages/gatsby/src/query/file-parser.js
Lines 83 to 91 in 8138b97
And then doing additional
babelParseToAst
for every returned value.If we return
undefined
instead, results ofgatsby-plugin-image
will be simply ignored here and won't be added totranspiled
array above. Here is where we filter empty plugin results:gatsby/packages/gatsby/src/utils/api-runner-node.js
Lines 682 to 683 in 8138b97
So to recap:
If there are multiple plugins with
preprocessSource
(e.g.gatsby-plugin-image
andgatsby-plugin-mdx
- we'll do a redundantbabelParseToAst
.Also, it will likely throw in this combination and hit another slow path as well as cause weird issues with query extraction, see #28676 for a related fix in the core.