Reference version

This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).

babel.config.js

编辑页面

Babel 配置文件参考。


For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

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 文件:

  1. 进入项目根目录,并在终端中运行以下命令。这将会在项目根目录生成一个 babel.config.js 文件。
Terminal
npx expo customize babel.config.js
  1. babel.config.js 文件包含以下默认配置:
babel.config.js
module.exports = function (api) { api.cache(true); return { presets: ['babel-preset-expo'], }; };
  1. 如果你对 babel.config.js 文件进行了更改,需要重启 Metro 打包器以应用这些更改,并使用 Expo CLI 的 --clear 选项清除 Metro 打包器缓存:
Terminal
npx expo start --clear

babel-preset-expo

babel-preset-expo 是 Expo 项目中使用的默认预设。它扩展了默认的 React Native 预设(@react-native/babel-preset),并添加了对装饰器、Web 库树摇优化以及加载字体图标的支持。