Commit 407d81e
committed
bug #738 Fix babel config file detection (jdreesen)
This PR was squashed before being merged into the master branch.
Discussion
----------
Fix babel config file detection
Since v7 [Babel supports two types of config formats](https://babeljs.io/docs/en/config-files):
- Project-wide configuration
- `babel.config` files, with the different extensions (`json`, `js`, `cjs`, `mjs`)
- File-relative configuration
- `.babelrc` files, with the different extensions (none, `json`, `js`, `cjs`, `mjs`)
- `package.json` files with a "babel" key
Encore, 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 the `node_modules` folder **will** be compiled by Babel, but **without** the settings defined in the `.babelrc.js` file.
If you now rename the `.babelrc.js` to `babel.config.js`, Encore fails to detect it and applies its default Babel config, which results in everything being complied without the settings defined in `babel.config.js`.
> Note: Configuring babel through Encore is not an option for me, because I want to make use of Babels [Config Function API](https://babeljs.io/docs/en/config-files#config-function-api) which is not available in Encore afaik.
And apart from that I prefer to have the configuration in dedicated files.
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 additional `babel.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 the `babelrc` 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.
Commits
-------
5ce489e Fix babel config file detectionFile tree
3 files changed
+37
-13
lines changed- lib
- config
- test/config
3 files changed
+37
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
399 | | - | |
| 399 | + | |
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
| 418 | + | |
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| |||
462 | 462 | | |
463 | 463 | | |
464 | 464 | | |
465 | | - | |
| 465 | + | |
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
99 | 111 | | |
100 | 112 | | |
101 | 113 | | |
| |||
108 | 120 | | |
109 | 121 | | |
110 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
111 | 135 | | |
112 | 136 | | |
113 | 137 | | |
| |||
0 commit comments