> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quiesco.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 配置 Expo 应用

> quiesco init 在 Expo 应用里配置了什么，以及如何构建和运行 e2e 应用。

在 Expo 应用（SDK 55 或更高版本）里，`quiesco init` 完成配置，`quiesco build` 生成 e2e 构建；
[快速开始](https://docs.quiesco.dev/zh/quickstart.md)会带你走完这两步，从新建应用到跑通一个测试。本页说明配置了哪些东西，
方便你检查一个项目，或者手动完成其中某一步。不使用 Expo 的应用，请看
[配置纯 React Native 应用](https://docs.quiesco.dev/zh/react-native.md)。

```sh theme={null}
npx quiesco init
```

`init` 会询问使用哪个测试运行器（Vitest、Jest 或 Cucumber），从不覆盖已有文件，并列出
留给你完成的事项（[CLI](https://docs.quiesco.dev/zh/cli.md#quiesco-init)）。

## 配置了什么

**1. 包**

```sh theme={null}
npx expo install expo-dev-client
npm install @quiesco/agent @quiesco/native
npm install --save-dev quiesco @quiesco/core @quiesco/expo-plugin @quiesco/vitest vitest
```

`expo-dev-client` 让 e2e 构建成为 development build，从 Metro 加载 JS。
`@quiesco/vitest` 是 Vitest 的集成；[Jest 和 Cucumber](https://docs.quiesco.dev/zh/runners.md) 的做法相同。

**2. Config plugin 和应用 ID**

```json app.json theme={null}
{
  "expo": {
    "scheme": "myapp",
    "ios": { "bundleIdentifier": "com.example.myapp" },
    "android": { "package": "com.example.myapp" },
    "plugins": ["expo-router", "expo-dev-client", "@quiesco/expo-plugin"]
  }
}
```

`@quiesco/expo-plugin` 让 e2e 构建可以连接 `localhost` 上的测试进程；对其他构建
没有任何改动。写明 `scheme`、`ios.bundleIdentifier` 和 `android.package`，是为了让构建和
`quiesco.config.mts` 指的是同一个应用。使用 `app.config.js` 或 `app.config.ts` 时，需要自己加上。

**3. Metro**

```js metro.config.js theme={null}
const { getDefaultConfig } = require('expo/metro-config');
const { withQuiesco } = require('@quiesco/agent/metro');

module.exports = withQuiesco(getDefaultConfig(__dirname));
```

`withQuiesco()` 会把 agent 放进以 `EXPO_PUBLIC_QUIESCO=1` 生成的 bundle，其他 bundle 里放的是一个
空实现。

**4. 入口文件**

```ts index.ts theme={null}
import '@quiesco/agent'; // 必须保持为第一个 import
import 'expo-router/entry';
```

agent 必须是应用的第一个 import。Expo Router 应用会得到这个入口文件，以及
`package.json` 里的 `"main": "index.ts"`。

**5. 根组件**

```tsx src/app/_layout.tsx theme={null}
import { QuiescoRoot } from '@quiesco/agent';
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <QuiescoRoot>
      <Stack />
    </QuiescoRoot>
  );
}
```

在 Expo Router 应用里，这一步 `init` 留给你：用 `<QuiescoRoot>` 包住根布局（`src/app/_layout.tsx`）。

**6. 配置文件**

```ts quiesco.config.mts theme={null}
import { defineConfig } from '@quiesco/core';

export default defineConfig({
  apps: {
    ios: {
      bundleId: 'com.example.myapp',
      binaryPath: 'ios/build/Build/Products/Debug-iphonesimulator/myapp.app',
      // development build 从 Metro 加载 JS。
      devClient: { metroUrl: 'http://localhost:8081' },
    },
    android: {
      package: 'com.example.myapp',
      binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk',
      devClient: { scheme: 'myapp', metroUrl: 'http://localhost:8081' },
    },
    web: { distDir: 'dist' },
  },
});
```

`devClient` 表示应用是 development build：Metro 没有运行时，`quiesco test` 会启动它。
所有选项见[配置](https://docs.quiesco.dev/zh/config.md)；运行器自己的配置和第一个测试见
[测试运行器](https://docs.quiesco.dev/zh/runners.md)。

## 构建和运行

> [tab] iOS
>
> ```sh theme={null}
> npx quiesco build ios
> npx quiesco test --platform ios
> ```

> [tab] Android
>
> ```sh theme={null}
> npx quiesco build android
> npx quiesco test --platform android
> ```

> [tab] Web
>
> ```sh theme={null}
> EXPO_PUBLIC_QUIESCO=1 npx expo export --platform web
> npx quiesco test --platform web
> ```

`quiesco build` 带着 `EXPO_PUBLIC_QUIESCO=1` 运行 `expo prebuild`，然后执行原生构建
（[CLI](https://docs.quiesco.dev/zh/cli.md#quiesco-build)）；只有带着这个变量，autolinking 才会链接 `@quiesco/native`。要在 EAS 上
构建，请看 [EAS Build](https://docs.quiesco.dev/zh/eas.md)。

## e2e 以外的构建

没有 `EXPO_PUBLIC_QUIESCO=1` 时，Metro 打包进去的是代替 agent 的空实现，autolinking 不链接
`@quiesco/native`，config plugin 也不写入任何东西，所以正式发布的版本里没有任何 Quiesco 的代码，
也不需要为此做任何设置。原生目录是为某一种构建生成的：做过 e2e 构建之后，要在同一份代码目录里构建正式版本，
先不带这个变量运行 `npx expo prebuild --clean`。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.