This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
babel.config.js
编辑页面
Babel 配置文件参考。
Babel 被用作 JavaScript 编译器,将现代 JavaScript(ES6+)转换为与移动设备上的 JavaScript 引擎兼容的版本。
使用 npx create-expo-app 创建的每个新的 Expo 项目都会自动配置 Babel,并默认使用 babel-preset-expo 作为默认预设。除非你需要自定义 Babel 配置,否则无需创建 babel.config.js 文件。
创建 babel.config.js
如果你的项目需要自定义 Babel 配置,你需要按照以下步骤在项目中创建 babel.config.js 文件:
- 导航到项目根目录,并在终端中运行以下命令。这将会在项目根目录生成一个 babel.config.js 文件。
Terminal
- npx expo customize babel.config.js- babel.config.js 文件包含以下默认配置:
babel.config.js
module.exports = function (api) { api.cache(true); return { presets: ['babel-preset-expo'], }; };
- 如果你对 babel.config.js 文件进行了更改,你需要重启 Metro bundler 以应用这些更改,并使用 Expo CLI 的
--clear选项清除 Metro bundler 缓存:
Terminal
- npx expo start --clearbabel-preset-expo
babel-preset-expo 是 Expo 项目中使用的默认预设。它扩展了默认的 React Native 预设(@react-native/babel-preset),并增加了对装饰器、Web 库树摇优化以及加载字体图标的支持。