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

Expose Babel config for Gatsby #6170

Closed
kkemple opened this issue Jun 26, 2018 · 12 comments
Closed

Expose Babel config for Gatsby #6170

kkemple opened this issue Jun 26, 2018 · 12 comments
Labels
good first issue Issue that doesn't require previous experience with Gatsby

Comments

@kkemple
Copy link
Contributor

kkemple commented Jun 26, 2018

Summary

Exposing Gatsby's Babel configuration would allow Gatsby apps to integrate with 3rd party tooling like linters and testing frameworks. There are two options for accomplishing this, making Gatsby's config a babel preset, or printing a .babelrc file in root of project (or somewhere else).

Motivation

Currently getting testing or linting configuration requires essentially copying what Gatsby does into your own .babelrc file, at this point it would be much cleaner and easier on the user to just install and include babel-preset-gatsby.

@kkemple kkemple added type: maintenance An issue or pull request describing a change that isn't a bug, feature or documentation change 🏷 type: feature and removed type: maintenance An issue or pull request describing a change that isn't a bug, feature or documentation change labels Jun 26, 2018
@melissamcewen
Copy link
Contributor

oh yeah, just noticed this when I was working with Jest tests on a Gatsby site. I wondered if my babelrc I created for Jest was basically duping something Webpack is already doing?

@melissamcewen
Copy link
Contributor

Related #5824

After the upgrade, Gatsby which now depends on Babel 7, will now reflect Babel's configuration resolution behavior, which will now defer to the consuming project's top level .babelrc instead, which generated this error

@kkemple
Copy link
Contributor Author

kkemple commented Jun 29, 2018

Yeah, I think it makes sense expose it as a preset so it's really easy to configure with tooling. I hit issues with Storybook as well.

@Tomekmularczyk
Copy link

Tomekmularczyk commented Aug 22, 2018

Yeah, same here. Some tools require babel-config available like babel-plugin-react-intl. The same if I wanted to create my own tool that relies on babel and I don't necessarily want it in my build system (by extending webpack config).

I see that most of examples and even documentation suggests copying specific presets and plugins. But this is less than optimal and more like a workaround I believe. It will make harder to migrate app to newer versions. Specifying packages in config that are not installed explicitly by user is also fragile I think.

What I would ideally see is something like they do in NextJS. If you want a custom .babelrc you create a file with a nextjs preset which has all that is necessary to work by a framework:

.babelrc

{
  "presets": [
    "next/babel"
  ]
  ...
}

@RobertWSaunders
Copy link
Contributor

Agreed, this would be super useful! Setting up Jest in my project has been a pain and this would help streamline things while also being more explicit about what is happening behind the scenes. 👍

@kkemple
Copy link
Contributor Author

kkemple commented Sep 17, 2018

V2 is being released shortly and there are a lot of new docs that explain how to set up jest for testing! I would recommend going that route as we experimented with exposing config but it actually becomes harder to work with as you can't get the babel config until gatsby develop is running!

https://next.gatsbyjs.org/docs/unit-testing/

@Undistraction
Copy link
Contributor

@kkemple I have to say I still find the docs confusing. It's unclear what approach should be taken if we just want to add a babel plugin. The old recommendation was to copy Gatsby's babelrc to the root and make changes there, but how should we be doing it now? How should we add a single babel plugin, for example babel-plugin-styled-components? Secondary to this, if we are modifying the babel config, how will this effect the approach to testing outlined in the docs.

@chibicode
Copy link

@kkemple I echo the concern brought up by @Undistraction.

@kkemple
Copy link
Contributor Author

kkemple commented Oct 2, 2018

@Undistraction do you want to add the babel plugin to your testing config or to gatsby's build itself?

If for testing you would add that to the jest-preprocessor config you create, if for gatsby itself you would use the babel config API.

The reason exposing the actual babel config is difficult is because of the plugin architecture. Any plugin can modify that config, so in order to get a proper babel config you would have to run develop or build to first get all plugins and config bootstrapped then print out the babelrc file.

The other issue this opens up that in babel 7 a babelrc file at root of project will be read in by gatsby, so if you add a plugin to your now printed babelrc file then gatsby will use it during builds. If you still want to print it out you can do something very similar to this plugin.

@DSchau DSchau added status: inkteam to review good first issue Issue that doesn't require previous experience with Gatsby and removed status: inkteam to review labels Oct 2, 2018
@DSchau
Copy link
Contributor

DSchau commented Oct 2, 2018

Love this issue. I'll provide some context if someone wants to grab this for Hacktoberfest or honestly, just in general.

Summary

Expose an NPM package babel-preset-gatsby, that will be usable standalone (i.e. in Jest tests) and is also used internally so we get benefits "for free" as changes are made to that package.

Detail

  • Gatsby is managed as a monorepo, so all published packages are housed in packages/
  • Create a packages/babel-preset-gatsby package (use another package, e.g. packages/gatsby-dev-cli for a simple example of project structure, babel setup, etc.)
  • Create a babel preset in src/index.js (e.g. module.exports = {} // preset here)
  • (Eventually, not necessarily as part of this PR) replace babel-loader-helpers babel preset with this plugin. Note: this may require some type of separate utility to expose configuration options to tie into Gatsby lifecycle stuff.

As always, feel free to reach out to any of us on the Gatsby team for more assistance and detail 😄 🎉

philipp-spiess added a commit to philipp-spiess/gatsby that referenced this issue Oct 2, 2018
This PR adds `babel-preset-gatsby`, a new package that exposes our babel
config as a handy preset that we can re-use in all our internal packages
as well as in public packages.

I've added a new option, `targets`, to overwrite the preset-env targets.
You can see the changes in the updated Babel documentation.

In gatsbyjs#6170, @DSchau mentioned that we want to replace [`babel-loader-helpers`][]
with this preset as well (eventually). I'm not really sure what this
helper is doing right now so I decided to not touch it. It seems like
the helper is used for users of Gatsby.

I'm thinking now that my changes to the Babel documentation article
should not be made until we also upgrade the above helper to use our
preset (e.g. the helper adds `babel-plugin-macros` which is not used in
our internal preset). Let me know if I should revert my changes to the
article.

There are a few other guides that explain a custom babel config. Should
I update them as well (test-css-in-js and unit-testing)?

[`babel-loader-helpers`]: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/babel-loader-helpers.js
@philipp-spiess
Copy link
Contributor

@DSchau Thanks for your awesome write-up. I started with this in #8724. Would be great if you can take a look. 🙂

DSchau pushed a commit that referenced this issue Oct 23, 2018
Part of #6170 

This PR adds `babel-preset-gatsby`, a new package that exposes our babel config as a handy preset that we can re-use in all our internal packages as well as in public packages.

I've added a new option, `targets`, to overwrite the preset-env targets. You can see the changes in the updated Babel documentation.

In #6170, @DSchau mentioned that we want to replace [`babel-loader-helpers`][] with this preset as well (eventually). I'm not really sure what this helper is doing right now so I decided to not touch it. It seems like the helper is used for users of Gatsby.

I'm thinking now that my changes to the Babel documentation article should not be made until we also upgrade the above helper to use our preset (e.g. the helper adds `babel-plugin-macros` which is not used in our internal preset). Let me know if I should revert my changes to the article.

There are a few other guides that explain a custom babel config. Should I update them as well (test-css-in-js and unit-testing)?

[`babel-loader-helpers`]: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/babel-loader-helpers.js
@amberleyromo
Copy link
Contributor

Closed in #8724

gpetrioli pushed a commit to gpetrioli/gatsby that referenced this issue Jan 22, 2019
Part of gatsbyjs#6170 

This PR adds `babel-preset-gatsby`, a new package that exposes our babel config as a handy preset that we can re-use in all our internal packages as well as in public packages.

I've added a new option, `targets`, to overwrite the preset-env targets. You can see the changes in the updated Babel documentation.

In gatsbyjs#6170, @DSchau mentioned that we want to replace [`babel-loader-helpers`][] with this preset as well (eventually). I'm not really sure what this helper is doing right now so I decided to not touch it. It seems like the helper is used for users of Gatsby.

I'm thinking now that my changes to the Babel documentation article should not be made until we also upgrade the above helper to use our preset (e.g. the helper adds `babel-plugin-macros` which is not used in our internal preset). Let me know if I should revert my changes to the article.

There are a few other guides that explain a custom babel config. Should I update them as well (test-css-in-js and unit-testing)?

[`babel-loader-helpers`]: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/babel-loader-helpers.js
mwfrost pushed a commit to mwfrost/gatsby that referenced this issue Apr 20, 2023
Part of gatsbyjs#6170 

This PR adds `babel-preset-gatsby`, a new package that exposes our babel config as a handy preset that we can re-use in all our internal packages as well as in public packages.

I've added a new option, `targets`, to overwrite the preset-env targets. You can see the changes in the updated Babel documentation.

In gatsbyjs#6170, @DSchau mentioned that we want to replace [`babel-loader-helpers`][] with this preset as well (eventually). I'm not really sure what this helper is doing right now so I decided to not touch it. It seems like the helper is used for users of Gatsby.

I'm thinking now that my changes to the Babel documentation article should not be made until we also upgrade the above helper to use our preset (e.g. the helper adds `babel-plugin-macros` which is not used in our internal preset). Let me know if I should revert my changes to the article.

There are a few other guides that explain a custom babel config. Should I update them as well (test-css-in-js and unit-testing)?

[`babel-loader-helpers`]: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/babel-loader-helpers.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that doesn't require previous experience with Gatsby
Projects
None yet
Development

No branches or pull requests

9 participants