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

fix(gatsby-plugin-image): do not return from preprocessSource if nothing is changed #28678

Merged
merged 1 commit into from
Dec 18, 2020

Conversation

vladar
Copy link
Contributor

@vladar vladar commented Dec 17, 2020

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 when gatsby-plugin-image is used alongside other plugins, i.e. gatsby-plugin-mdx.

Some context. We run this preprocessSource hook here when extracting queries:

const transpiled = await apiRunnerNode(`preprocessSource`, {
filename: filePath,
contents: fileStr,
parentSpan: parentSpan,
})
if (transpiled && transpiled.length) {
for (const item of transpiled) {
try {
const tmp = babelParseToAst(item, filePath)

And then doing additional babelParseToAst for every returned value.

If we return undefined instead, results of gatsby-plugin-image will be simply ignored here and won't be added to transpiled array above. Here is where we filter empty plugin results:

// Filter empty results
apiRunInstance.results = results.filter(result => !_.isEmpty(result))

So to recap:

If there are multiple plugins with preprocessSource (e.g. gatsby-plugin-image and gatsby-plugin-mdx - we'll do a redundant babelParseToAst.

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.

@vladar vladar requested a review from ascorbic December 17, 2020 20:09
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Dec 17, 2020
@vladar vladar removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Dec 17, 2020
@ascorbic
Copy link
Contributor

Looks good. I didn't realise this worked

@vladar vladar added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Dec 18, 2020
@gatsbybot gatsbybot merged commit 9a49286 into master Dec 18, 2020
@gatsbybot gatsbybot deleted the vladar/fix-image-preprocess branch December 18, 2020 09:32
vladar added a commit that referenced this pull request Dec 23, 2020
vladar added a commit that referenced this pull request Dec 23, 2020
…ing is changed (#28678) (#28745)

(cherry picked from commit 9a49286)

Co-authored-by: Vladimir Razuvaev <[email protected]>
@vladar
Copy link
Contributor Author

vladar commented Dec 23, 2020

Published in [email protected]

pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants