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

RFC: Support babel.config.json for better cacheability #10495

Closed
devongovett opened this issue Sep 26, 2019 · 5 comments · Fixed by #10501
Closed

RFC: Support babel.config.json for better cacheability #10495

devongovett opened this issue Sep 26, 2019 · 5 comments · Fixed by #10501
Labels
Has PR i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@devongovett
Copy link
Contributor

At the moment, Babel supports several configuration options: .babelrc, .babelrc.js, and babel.config.js. Many projects have migrated to babel.config.js since it was introduced, especially monorepos. However, many configs are still quite simple and could be JSON instead of JavaScript. This has several benefits, including better cacheability. Babel should support babel.config.json in addition to its existing options for those cases.

Using JavaScript for configs can be nice when you need to do complex condition-based configuration, but it has several downsides including preventing caching. In Parcel 2, we have to bail on caching all files compiled with Babel when a JavaScript config file is used instead of JSON. Unfortunately, that leaves no recourse to users if they want the benefits of caching, but want to use a root config. If Babel supported babel.config.json, these issues would be alleviated somewhat. For simple configs that are JSON serializable, Parcel and other tools could correctly cache files as is already possible with .babelrc.

I am aware of the Babel config caching API, but it's not really designed for use in a cache that is serialized to disk. It is designed to avoid re-calling the config function, not to avoid re-compiling files. In addition, the API relies on callback functions to determine if the cache should be invalidated, but that only works when the callback can be stored in memory - we cannot serialize it to disk.

@babel-bot
Copy link
Collaborator

Hey @devongovett! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

Previous discussion: #7358 (comment)

Personally, I would like this feature. For most users, the only dynamism that they might need in their config is provided by the env key.
Other that caching, I think that it has some advantages:

  • It would allow us to esily lint configuration files (Babel Config Linter #7880, babel-doctor)
  • It make it possible to have autocompletion in editors, using a simple JSON schema. For example, I never remember if it's overwrite, overwrites, override or overrides, as well as parserOpts vs parserOptions.
  • babel-upgrade is dying, and it a pity: it's a powerful tool which can have its advantages also when upgrading accross minor versions (for example, to remove an unnecessary plugin) or in the Babel 7 -> Babel 8 update. Unfortunatly, it can only work with JSON files.

@devongovett
Copy link
Contributor Author

That's a good list of other advantages of JSON over JS. I'm happy to make a PR for this if it is something Babel would consider.

cc. @loganfsmyth from the previous discussion

@devongovett
Copy link
Contributor Author

Made a PR since it was pretty easy: #10501

@Jessidhia
Copy link
Member

Jessidhia commented Sep 27, 2019

@nicolo-ribaudo it actually is possible to have autocompletion in typescript-aware editors with babel.config.js -- but it takes some boilerplate. You need @types/babel__core installed and

// @ts-check
'use strict'

/**
 * @param {import('@babel/core').ConfigAPI} api
 * @returns {import('@babel/core').TransformOptions}
 */
module.exports = function(api) {
  return {/* ... */}
}

(the // @ts-check is optional)

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 28, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants