Skip to content

How do we want to handle .babelrc lookup in 7.x #6766

@loganfsmyth

Description

@loganfsmyth

Edit 2

Most recent status: #6766 (comment)

Edit 1

All below still applies, but also see more explanation in #6766 (comment)

Original:

Not a fleshed out proposal, but filing for tracking and discussion.

Currently if you have:

.babelrc
src/
  index.js
  utils/
    .babelrc
    other.js

compiling index.js will use the root-level .babelrc, and other.js will use the src/utils/.babelrc. I assume this pattern was pulled from other utilities like ESLint, but not 100% sure.

We're at the point now where this approach has become a problem for us because:

  1. Very few people seem to expect this behavior to begin with.
  2. People expect their project-level .babelrc file to also apply to things that live in node_modules. Babel 6 was terrible at figuring out how to handle node_modules because it always assumes things are strict mode, and always rewrites this, but a lot of those issues aren't as big in Babel 7, or at least ideally won't be, because we plan to have better handling of the sourceType: script vs sourceType: module distinction. This kind of works now, but see point (3).
  3. If people publish packages to npm and include .babelrc files in their packages, or more troublingly, use the babel key in package.json (which you can't really get around publishing), users trying to use Webpack or anything like it to compile node_modules get extremely confusing errors about not being able to find plugins and presets referenced in those files. See Enable babel-preset-env for node_modules that target newer Node versions facebook/create-react-app#1125 for a massive list of users that want to compile node_modules, but would absolutely be bitten by this issue.

So where does that leave us? The simplest thing I can think for us to do would be to change up Babel's API to, by default, accept a root directory as an argument where it would search for the config, defaulting to the working directory.

We're already at a point where I've thought about adding an argument for root directory to resolve the filename relative to, so that should be uncontroversial. Actually changing config resolution is a bigger question.

It would be pretty easy for us to allow users to opt for the old behavior, so I think my proposal would probably be to add this new root-dir config resolution logic and make it the default, but keep the old behavior around as an opt in. So where now we have

babelrc: boolean

to toggle config resolution off and on, I could imagine us doing

babelrc: true | "root" => Look for config in root directory
babelrc: "relative" => Look for config, starting from compiled file, working upward
babelrc: false => Skip search entirely.

instead.

The primary loss of functionality here is that if users do want to use .babelrc files inside nested folders to change Babel configs based on path, they don't have an easy way to do it anymore. I don't however have a good grasp of how many users that might be. It seems like we may want to consider #5451 as something to be included in this effort if we do want to change, which might end up blocked on #6765.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority: HighoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions