何时使用 babel.config.js 和 .babelrc
When to use babel.config.js and .babelrc
我正在学习 Babel,想学习如何配置 Babel。我找到了两种配置 Babel 的方法:创建 babel.config.js 和 .babelrc 文件。在什么情况下我们应该更喜欢一个配置文件而不是另一个?
来自文档
https://babeljs.io/docs/en/config-files#project-wide-configuration
Babel 有两种并行的配置文件格式,可以一起使用,也可以独立使用。
Project-wide configuration
babel.config.json files, with the different extensions
File-relative configuration
.babelrc.json files, with the different extensions
package.json files with a "babel" key
Babel loads .babelrc.json files, or an equivalent one using the supported extensions, by searching up the directory structure starting from the "filename" being compiled
鉴于该信息
.babelrc 如果你想 运行 某些文件/目录的子集上的某些转换/插件会很有用。也许你不想成为 3rd 方库 transformed/changed by babel.
babel.config.json 如果您的项目中有多个包(即多个 package.json)目录使用单个 babel 配置,那么它很有用。这种情况不太常见。
如果您的问题是关于 babel 配置的文件扩展名(即 .js
与 .json
)
使用 .js
公开 babel 配置 api。
https://babeljs.io/docs/en/config-files#config-function-api
请记住,这会增加缓存方面的复杂性,大多数时候最好使用 .json
静态配置
这两个配置似乎有些不同,
正在看这个问题:
Jest transformIgnorePatterns not working
有时,某些功能只适用于某个文件,这是非常模糊的,当然也没有记录。 (我可以确认上述问题存在,重命名配置文件即可解决)
此外,有时 babel 配置文件会被完全忽略,例如 webpack 和 babel-loader。您希望 babel 加载在项目根目录中找到的 .babelrc 文件,但事实证明它会忽略它和 运行 Webpack 中提供的选项。
所以不幸的是,答案有点模糊,而且缺乏关于这些功能如何工作的文档并不能改善这种情况。
我正在学习 Babel,想学习如何配置 Babel。我找到了两种配置 Babel 的方法:创建 babel.config.js 和 .babelrc 文件。在什么情况下我们应该更喜欢一个配置文件而不是另一个?
来自文档 https://babeljs.io/docs/en/config-files#project-wide-configuration
Babel 有两种并行的配置文件格式,可以一起使用,也可以独立使用。
Project-wide configuration
babel.config.json files, with the different extensions
File-relative configuration
.babelrc.json files, with the different extensions
package.json files with a "babel" key
Babel loads .babelrc.json files, or an equivalent one using the supported extensions, by searching up the directory structure starting from the "filename" being compiled
鉴于该信息
.babelrc 如果你想 运行 某些文件/目录的子集上的某些转换/插件会很有用。也许你不想成为 3rd 方库 transformed/changed by babel.
babel.config.json 如果您的项目中有多个包(即多个 package.json)目录使用单个 babel 配置,那么它很有用。这种情况不太常见。
如果您的问题是关于 babel 配置的文件扩展名(即 .js
与 .json
)
使用 .js
公开 babel 配置 api。
https://babeljs.io/docs/en/config-files#config-function-api
请记住,这会增加缓存方面的复杂性,大多数时候最好使用 .json
静态配置
这两个配置似乎有些不同,
正在看这个问题:
Jest transformIgnorePatterns not working
有时,某些功能只适用于某个文件,这是非常模糊的,当然也没有记录。 (我可以确认上述问题存在,重命名配置文件即可解决)
此外,有时 babel 配置文件会被完全忽略,例如 webpack 和 babel-loader。您希望 babel 加载在项目根目录中找到的 .babelrc 文件,但事实证明它会忽略它和 运行 Webpack 中提供的选项。
所以不幸的是,答案有点模糊,而且缺乏关于这些功能如何工作的文档并不能改善这种情况。