Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

rollup-plugin-babel cannot read .babelrc when run from repo root #271

Closed
mjackson opened this issue Nov 14, 2018 · 10 comments
Closed

rollup-plugin-babel cannot read .babelrc when run from repo root #271

mjackson opened this issue Nov 14, 2018 · 10 comments

Comments

@mjackson
Copy link

It seems like rollup-plugin-babel only finds and reads .babelrc files when it is run outside the directory where that package is contained. I am going to post a repro here shortly.

Possibly related to #232, but I'm not sure because the source repo has since been deleted.

@mjackson
Copy link
Author

OK, repro is at mjackson/rollup-plugin-babel-bug. If you run that repo, you'll see what I'm talking about.

@Andarist
Copy link
Member

This is in fact strange and I'm not sure why this happens, however rollup-plugin-babel doesn't load babelrc files on it own - that's babel responsibility, so the issue got kinda reported in the wrong repository.

You can "fix it" by passing babelrcRoots: ['./packages/*'] as option to you rollup-plugin-babel's instance - it's babel@7 option https://babeljs.io/docs/en/options#babelrcroots .

cc @loganfsmyth

@loganfsmyth
Copy link
Collaborator

loganfsmyth commented Nov 15, 2018

The core of it is that we use the working directory as the default for the root package where .babelrc are taken into account. There are a few ways to get around that, but since this seems to be a monorepo structure, the standard recommendation is to create a babel.config.js in the repo root with module.exports = { babelrcRoots: ["./packages/*"] }; and then pass rootMode: "upward" to the Rollup plugin, since that'll properly load the root config, and the root config will activate .babelrc resolution for all monorepo packages. You can also skip the babel.config.js and pass that right in the rollup config, but then if you have multiple things that might consume that setting, like Jest or @babel/register it can get annoying to repeat the babelrcRoots value for each of them.

Probably worth reading through https://babeljs.io/docs/en/config-files though if you have suggestions for improvements, I'm happy to hear them. Realistically, we should have a section specifically about configuring monorepos. The critical part from there in this context is

The "filename" being compiled must be inside of "babelrcRoots" packages, or else searching will be skipped entirely.

@mjackson
Copy link
Author

Thanks for taking a look, @Andarist. I think you're right, this is a babel issue. Sorry for filing in the wrong repo.

Thanks for your comment, @loganfsmyth. I read through those docs earlier today, but this part doesn't seem accurate:

Babel loads .babelrc (and .babelrc.js / package.json#babel) files by searching up the directory structure starting from the "filename" being compiled. This can be powerful because it allows you to create independent configurations for subsections of a repository.

If that were true, wouldn't the settings in a .babelrc file always apply to files in that directory or lower, regardless of the current working directory?

For example, in the sample repo I provided the file being compiled is packages/react-router/modules/index.js. There is a .babelrc at packages/react-router/modules/.babelrc that, according to the docs, should be loaded for that file. But it isn't always because it depends on the current working directory. So the docs are a little misleading IMO.

Maybe it's worth mentioning how the current working directory affects the lookup of .babelrc files somewhere in those docs?

Thanks again for taking a look.

@Andarist
Copy link
Member

Thanks for taking a look, @Andarist. I think you're right, this is a babel issue. Sorry for filing in the wrong repo.

No problem at all.

I definitely agree with @mjackson that current docs around this are misleading - I was reading them while investigating this and couldnt figure out from them what's the exact reason why this babelrc wasnt picked up. I just knew from other issues reported previously how to fix it (with babelrcRoots) but it was unclear for me why it has to be specified in this case.

@loganfsmyth
Copy link
Collaborator

Yeah you are right that that passage is overly broad. It should probably have a , subject to the edge cases below. at the end or something.

@mjackson
Copy link
Author

Well, it looks like this was just a misunderstanding of the way I believed the .babelrc file worked. Happy to close.

@ntucker
Copy link

ntucker commented Jan 17, 2019

When I run babel-cli it works fine - .babelrc or babel.config.js.

with rollup it's not picking up either

@Andarist
Copy link
Member

@ntucker could u provide a repository with the issue reproduced?

@ntucker
Copy link

ntucker commented Jan 18, 2019

@Andarist Turns out I was missing the extensions: ['.js', '.ts'] on babel options so babel was just skipping my .ts files due to that.

DrSensor added a commit to DrSensor/bot-byte that referenced this issue Feb 4, 2019
- [npm start] remove typescript declaration file
- [npm start] use babel with typescript preset
- [npm build] use typescript compiler

references:
- rollup/rollup-plugin-babel#255
- rollup/rollup-plugin-typescript#129
- rollup/rollup-plugin-babel#271
- rollup/rollup-plugin-babel#279
- microsoft/TypeScript-Babel-Starter#29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants