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): add stage to nested babel-preset-gatsby #36249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Jul 27, 2022

Description

Our code that ensures stage is added to babel-preset-gatsby options was only working when local babelrc (or other config file) was adding our preset. If user is using custom preset that adds our preset it wasn't working anymore.

This PR will resolve presets other than ours to be able to recursively check for babel-preset-gatsby and add stage to options.

This PR is also making sure that reactRuntime and reactImportSource are passed as well, as we do that for our default -

fallbackPresets.push(
babel.createConfigItem(
[
resolve(`babel-preset-gatsby`),
{
stage,
reactRuntime,
reactImportSource,
},
],
{
type: `preset`,
}
)
)

Related Issues

Fixes #35731

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 27, 2022
@pieh pieh added topic: webpack/babel Webpack or babel and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 27, 2022
@LekoArts
Copy link
Contributor

So does that also fix #35069 (#34518) and we could remove the note?

@pieh
Copy link
Contributor Author

pieh commented Jul 27, 2022

So does that also fix #35069 (#34518) and we could remove the note?

I will say that I didn't really test jsxRuntime and jsxImportSource addition (I'm really not quite sure how to heh), but it should force passing those along stage. In current shape of PR this will mean that user can no longer set those and only place to set those will be gatsby-config (that seems reasonable?). I can also swap some things around so we default to ones from config, but if user provided their own we will use that (is there a usecase for that?)

So we should be able to remove the note about passing those from packages/babel-preset-gatsby/README.md - at least once that is shipped

@pieh pieh force-pushed the fix/add-stage-to-nested-gatsby-plugin-preset branch from 7bbdafd to 215cd98 Compare July 27, 2022 12:12
@LekoArts
Copy link
Contributor

I will say that I didn't really test jsxRuntime and jsxImportSource addition (I'm really not quite sure how to heh)

If you set jsxRuntime to automatic but then use Emotion so you'll want to have another reactImportSource: https://emotion.sh/docs/css-prop#jsx-pragma

iirc that's how you reproduce that

@LekoArts
Copy link
Contributor

cc @aaronadamsCA

@pieh
Copy link
Contributor Author

pieh commented Jul 27, 2022

If you set jsxRuntime to automatic but then use Emotion so you'll want to have another reactImportSource: emotion.sh/docs/css-prop#jsx-pragma

Tried it - with gatsby@next and custom .babelrc I do have to do what we have in babel-preset-gatsby note currently (after removing React imports). With this change I don't need to pass anything to babel, as long as I have my those options defined in gatsby-config (and same as stage it also work when babel-preset-gatsby is in nested presets).

The issue with this and the note that we already have is that if user only set those jsxRuntime settings in their custom babel, but not gatsby-config, those will suddenly stop working - so I will make a change that prefer babel preset options if provided by user and fallback to gatsby-config ones so that users using current "workaround" suddenly will have breakages when they upgrade.

After thinking a bit more on it - I do think those options need to stay in babel-preset-gatsby README, because user might be using babel config for other tools ( jest, storybook ) - and this is the only way for them to work correctly. But I would edit the note a little bit - say "from gatsby@x we will automatically inject jsxRuntime options in Gatsby context, but if you use additional tools that use your babel config, you will need to set them in your babel config for them to work .

@pieh
Copy link
Contributor Author

pieh commented Jul 27, 2022

Added the preference on options provided by babelrc to ensure behaviour won't change for people already passing those. In actual practice it seems like user does need those settings in gatsby-config (the automatic or classic one at least) as otherwise our default eslint would fail webpack (!) due to

// New versions of react use a special jsx runtime that remove the requirement
// for having react in scope for jsx. Once the jsx runtime is backported to all
// versions of react we can make this always be `off`.
// I would also assume that eslint-config-react-app will switch their flag to `off`
// when jsx runtime is stable in all common versions of React.
"react/jsx-uses-react": usingAutomaticJsxRuntime ? `off` : `error`,
"react/react-in-jsx-scope": usingAutomaticJsxRuntime ? `off` : `error`,

So more than likely users already have them duplicated in config and in custom babelrc. This change most likely will only impact users that will try to use those in the future, and not ones that are already using it (as they won't have to add options to custom babelrc unless they use additional 3rd party tools)

@aaronadamsCA
Copy link
Contributor

(Side note, today I wasn't entirely certain how Babel configurations were being consumed, especially in a monorepo, and then I got seriously confused by what turned out to be #35435.)

My two cents: If I specified JSX import source in a Babel config, preferably that would win. IMHO the Gatsby option could even be deprecated; it's not too much to ask a user to create a short Babel config.

If this gets us a step closer to that, great!

Copy link

@lararthur lararthur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge please? 😔
@pieh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: webpack/babel Webpack or babel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

babel-preset-gatsby: Code-splitting issue when custom babel preset is used
5 participants