-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
How do we want to handle .babelrc
lookup in 7.x
#6766
Comments
Seems totally reasonable to me
|
Thanks for all your work on babel folks! 👏 Here are some thoughts:
I hope that's helpful! Thanks again! |
There is a Compiling node_modules is for when you want to consume a package from npm that is ES2015+. Maybe you support a different environment so you need to compile it down to a different level than ES5. Example: maybe a react package uses async but you don't want to use the "main" file which uses regenerator, so you want the package to output async. It may or may not be transpiled to async (no change), generators, or regenerator depending on the users
|
👍 Solid use case example 👍 So long as it's possible that'd be good with me. And if I could disable the lookup using webpack/babel-cli etc. then that'd be sufficient I think |
How about non-precompiled/non-distributable monorepos? This is currently what we do at @futuresimple, so e.g. we have To handle such a case well, I think the default behavior should be to use the |
I've seen nested babelrc files in repos which contain server and client build. |
I'm sure not everyone here will have read everything but we could possibly do #5451 which is an "overrides" option similar to eslint which gives you glob config + different config so you don't need nested .babelrcs. |
I'm strongly in favor of only using the root babelrc, since config files in nested directories are really easy to miss. +1 for #5451 |
@hzoo #5451 won't help for monorepos (when used without a build step). You'd have to re-declare all the babelrc's in the root of the monorepo, which doesn't sound fun. When you install packages in a monorepo, they end up symlinked as I am all for this change, as long as it's possible to declare in the root |
@loganfsmyth I imagine when having both front end and back end code in your monorepo you can have different babel configs. Also personally I like to be explicit about my deps, even used transforms, and I do not include a jsx transform when transpiling utility library even though in other directory of the monorepo I have a React UI library |
@loganfsmyth exactly as @Andarist says. We have both CLI and Frontend packages in the monorepo, at the same time we'd like to be able to open source some of the packages, so each package in the monorepo should be able to be run without the dependency of the root directory of the monorepo. #5451 introduces such a dependency, i.e. the package won't work when it's moved out of the monorepo into it's own repository via a |
Sounds good, thanks for the clarification. So it is not that there is are package-level |
As for me - that would be desired. If I'd like to extend some other config I might do that easily with |
Meteor supports nested We do not attempt to merge multiple
@nicolo-ribaudo So don't use nested |
@benjamn Would #5451 address any of your concerns? That would potentially allow you to do something like
so the root config could still easily delegate to another config. I could also imagine the monorepo case being handled like
for instance, so the root config would control everything, but still allow for flexibility. Alternatively we could have the root config allow a Maybe we could even do I feel like I'd want the file to merge on top of the root config by default, but maybe we allow config files to say My point is, there are tons of options that would allow essentially the same flexibility. I just want it to be explicit instead of implicit in Babel's core behavior, because the implicit relative lookup is causing problems, as stated in the original issue. |
AVA, which uses Babel to compile test files, can be directed to inherit from a user's Babel config. We only look for In other words I'm 👍 on this proposal (as stated in @loganfsmyth's opening comment). |
(I also agree with #6766 (comment)) I occasionally create a nested .babelrc file in my tests directory, so that certain transforms only apply to test files (not the same as "only transform in the test NODE_ENV"). Similarly, I might want certain transforms in a dev-only directory, and different ones in a production directory. How would I do this with this proposal? |
Just my 2 cents. I would like to explicitly pass the configuration file location and in addition I would disable completely the config lookup (not a BC since it's a new option). This would solve many issues with "how can I use Babel from this dir? can I store my config there? ..." and probably some .babelrc config inheritance issues. |
@ljharb What are your thoughts on my comments in #6766 (comment) I'm definitely not looking to break that usecase, though I am promoting changing the implementation and mental model behind how they would fit in. If I had to summarize the goal of this issue, it's to try to clarify that Babel configs are configuring a project as a whole with compilation logic, rather than defining configs for specific files. That means files in node_modules shouldn't have their configs processed unless the root project asks for that, and project-root configs should be able to influence how node_modules are compiled. As Node gets more and more ES6 features, people publish more and more modules to npm with ES6 modules. We can tell people to compile to ES5 before publishing all we want, but modules containing ES6 are going to happen either way, and I think it is important that we position Babel in a place where it can address those concerns without it being extremely painful for users. Additionally, I'm very concerned about the pain users will feel if they run Babel 7 and some of their node_modules have published |
Certainly, eslint 4+'s glob overrides approach would address the use case, but then it doesn't allow me to "hide" the overridden settings inside the directories that have different approaches. Perhaps this isn't a big deal. It's a fair point that people who try to transpile Perhaps - tangential to this issue - babel configs for 7+ should be required to explicitly declare their version number? Additionally, perhaps those who choose to transpile code they didn't write should be required to explicitly specify the location for the config (whether that's a specific .babelrc, or a specific package.json's "babel" section)? |
Specifying parent config and override paths seems like heavy config for answering the question of should this config be treated as done or rolled into a parent config. I think something like a |
We already stop at the first I should clarify my main goal on this. Primary IssueThe primary issue at the core of this is that people often expect their project's Even in the case of dependency modules with An alternative to my original proposal would be that we check if a file is in
If this project tries to compile The primary edge case there is that I guess technically your project itself could for some reason be inside a folder called To expand, right now we do:
but you could imagine us changing this to (as non-production code anyway)
|
I think that's how I thought it would work. Here's how I resolve config for I walk backwards in the directory structure, stopping at For folks that want to force a set of options across package boundaries we have a programmatic convention for that. Update: |
Could you clarify that or point me at particular code? In my description the config inside |
I treat |
Gotcha. So for us I'm saying we'd explicitly never load configs a dependency package's own config, and then (maybe as an opt-in), the project-level |
Having thought about this more over the last 2 weeks, I think where I've settled on the following:
I think this would address most of the current major concerns while keeping the existing behavior relatively consistent for the average case, at least inside your own package. For other raised points:
|
I'm currently using this (package.json):
All I'd like to do is having |
IMHO, compilation of
Instead, library authors should just pick an arbitrary minimum supported Node.js version and compile down to that with the env preset and bundle as (CJS and MJS) with Rollup. When Node 10 is released, it will automatically pick up the If they support browser usage, authors should pick which ECMAScript specs they support (or multiple specs). Note: To do this, we need #6731 in
|
FYI, we're starting the work to compile deps with Let us know if you have feedback about how this should work. Note we intentionally don’t plan to respect |
@loganfsmyth I wonder if it might be worth allowing the |
@guybedford I think that may just confuse things because I generally think users expect |
FYI there is a discussion about this in parcel as well. parcel-bundler/parcel#13 We will also be supporting compilation of node_modules with babel-preset-env. We will use the package.json engines field, or browserslist field to determine the language of the source module, and the browserslist of the target app to determine what to compile to. Then we do a diff to include only the necessary plugins. That way modules will not be compiled all the way to ES5, but to the app’s browser target instead. parcel-bundler/parcel#559 Most of the discussion now revolves around custom Babel plugins in node_modules, which is convenient for local development with linked modules. I proposed adding a field to package.json to selectively turn on custom babelrc support in node_modules. Would be interested to hear what you all think of that. I’d like to come up with something that can be used across tools, not just something parcel specific. |
@devongovett how are you going to get the entrypoint of the untranspiled sources of your dependencies in node_modules? |
Just made an alternative suggestion to enable distributing both transpiled code and source code together. parcel-bundler/parcel#13 (comment) |
Alright everyone, I've created #7358. Please look over what I've posted there and let me know what you think. |
@sokra and others. For this of us searching for a solution to the problem now using Babel 6 how is this usually solved? I've got a client side repo included in a server side repo via git submodules. I need to import some of the client code into the server side but the babelrc for the client application is screwing up the import due to the different configurations. |
@phawxby Feel free to join Slack or post on our recommended support channels, but pinging everyone on this thread about unrelated stuff will distract from the goals of this issue so I'd rather not help here. |
A little late to the party, just wanted to add some background for transforming/transpiling packages in node_modules (cc @ljharb). For maximum performance of front-end bundles, some people (e.g., Phil Walton, myself and the team I work with) want to pull as many dependencies as possible in as uncompiled ES modules. This:
I got bit by the nested babelrc's this week, and would love a |
Hey @robwierzbowski thanks for your input! And yeah we have moved discussion a while back to #7358 and are going to be moving forward with that. And yes we understand compiling node_modules is a use-case we all want to shoot for, not really the reasoning but how it would work! |
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:
compiling
index.js
will use the root-level.babelrc
, andother.js
will use thesrc/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:
.babelrc
file to also apply to things that live innode_modules
. Babel 6 was terrible at figuring out how to handlenode_modules
because it always assumes things are strict mode, and always rewritesthis
, 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 thesourceType: script
vssourceType: module
distinction. This kind of works now, but see point (3)..babelrc
files in their packages, or more troublingly, use thebabel
key inpackage.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 compilenode_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 thefilename
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
to toggle config resolution off and on, I could imagine us doing
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.The text was updated successfully, but these errors were encountered: