Fix babel config file detection #738
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since v7 Babel supports two types of config formats:
babel.config
files, with the different extensions (json
,js
,cjs
,mjs
).babelrc
files, with the different extensions (none,json
,js
,cjs
,mjs
)package.json
files with a "babel" keyEncore, however, only checks for the existence of file-relative config files.
This is a problem if you want to compile packages from the
node_modules
folder (via.configureBabel(null, {includeNodeModules: ['...']})
) because project-wide configuration files are needed for this.This means that if you only have a
.babelrc.js
file, the package from thenode_modules
folder will be compiled by Babel, but without the settings defined in the.babelrc.js
file.If you now rename the
.babelrc.js
tobabel.config.js
, Encore fails to detect it and applies its default Babel config, which results in everything being complied without the settings defined inbabel.config.js
.So my solution for now is to keep an empty
.babelrc.js
to make Encore think there is a Babel config file, and have an additionalbabel.config.js
file with the real config. This results in both the application code and node modules being compiled with the desired settings.But of course I dug a little deeper into Encore and Babel to fix the problem and the fix is using the
hasFilesystemConfig()
method instead of checking thebabelrc
property (the wording in the comment is a bit outdated, I already created a PR for that ;)):https://github.com/babel/babel/blob/af669297efd775016725ee39318cdb391cf00a21/packages/babel-core/src/config/partial.js#L191-L200
I added some tests, too. I'm not sure if it's worth it to add tests for the different config file extensions, though.
I tried to improve the error messages as well. Tell me if you want another wording or different file examples and I'll change it.