babel.config.js
Babel 配置文件的参考。
Babel 用作 JavaScript 编译器,将现代 JavaScript(ES6+)转换为与移动设备上的 JavaScript 引擎兼容的版本。
🌐 Babel is used as the JavaScript compiler to transform modern JavaScript (ES6+) into a version compatible with the JavaScript engine on mobile devices.
使用 npx create-expo-app 创建的每个新的 Expo 项目都会自动配置 Babel,并使用 babel-preset-expo 作为默认预设。除非你需要自定义 Babel 配置,否则无需创建 babel.config.js 文件。
🌐 Each new Expo project created using npx create-expo-app configures Babel automatically and uses babel-preset-expo as the default preset. There is no need to create a babel.config.js file unless you need to customize the Babel configuration.
创建 babel.config.js
🌐 Create babel.config.js
如果你的项目需要自定义 Babel 配置,你需要按照以下步骤在项目中创建 babel.config.js 文件:
🌐 If your project requires a custom Babel configuration, you need to create the babel.config.js file in your project by following the steps below:
- 导航到你的项目根目录,并在终端中运行以下命令。这将在你的项目根目录生成一个 babel.config.js 文件。
- npx expo customize babel.config.js- babel.config.js 文件包含以下默认配置:
module.exports = function (api) { api.cache(true); return { presets: ['babel-preset-expo'], }; };
- 如果你对 babel.config.js 文件进行了修改,你需要重启 Metro 打包器以应用更改,并使用 Expo CLI 的
--clear选项来清除 Metro 打包器缓存:
- npx expo start --clearbabel-preset-expo
babel-preset-expo 是 Expo 项目中使用的默认预设。它扩展了默认的 React Native 预设(@react-native/babel-preset),并添加了对装饰器、网络库的树摇优化以及字体图标加载的支持。