在 Expo(或更广义的 React Native managed workflow)里,Config Plugin(配置插件)是一种机制,允许你在不 eject(弹出)出“纯 JS”项目的情况下,对底层 iOS/Android 原生配置做定制化改动。

1. 为什么要用 Config Plugin

2. Config Plugin 的工作流程

  1. 定义插件:你写一个函数,接收并返回一个 Expo config 对象,内部用 withXxx 系列 HOC 去修改 iOS/Android 原生项目文件。
  2. 注册插件:在 app.jsonapp.config.js 里声明 plugins: [...]
  3. 预构建(prebuild)或 EAS 构建时,Expo 会遍历所有插件,把你的改动打到原生工程里,再执行原生编译。

3. 基本用法示例

app.config.js 中注册插件

// app.config.js
import { ExpoConfig, ConfigContext } from '@expo/config';

export default ({ config }: ConfigContext): ExpoConfig => ({
  ...config,
  name: "MyApp",
  slug: "my-app",
  version: "1.0.0",
  plugins: [
    // 官方社区插件
    'expo-build-properties',
    // 自定义本地插件
    './plugins/with-custom-splash.js',
  ],
});

一个简单的自定义插件

// plugins/with-custom-splash.js
const { withSplashScreen } = require('@expo/config-plugins');

module.exports = function withCustomSplash(config) {
  return withSplashScreen(config, {
    image: './assets/splash-custom.png',
    resizeMode: 'cover',
    backgroundColor: '#ffffff',
  });
};

这里,withSplashScreen 底层会修改 iOS 的 LaunchScreen.storyboard, Android 的 launch_background.xml,并把你指定的图片、背景色等属性写进去。

4. 常见官方 Config Plugins

5. 自定义写法要点

  1. 返回新的 config:插件函数收 config,要返回修改过的 config