> ## 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.

# 配置纯 React Native 应用

> 在不使用 Expo 的 React Native 应用中配置 Quiesco。

Quiesco 的各个包只依赖 React Native：`@quiesco/native` 是 React Native 的 TurboModule，
`@quiesco/agent` 是纯 JS 包。因此用 React Native Community CLI（`npx @react-native-community/cli init`）
创建的应用不必安装 `expo`，就可以在 iOS 和 Android 上测试。
应用需要 React Native 0.83 或更高版本，并启用新架构。Expo 应用请看
[配置 Expo 应用](https://docs.quiesco.dev/zh/expo.md)。

`quiesco init` 完成配置，`quiesco build` 生成 e2e 构建，用的都是项目自己的 `ios/` 和
`android/`：不会安装或运行任何 Expo 的东西。仓库里有一个完整的示例：
[examples/bare-rn-app](https://github.com/h2nology/quiesco/tree/main/examples/bare-rn-app)。

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

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

## 配置了什么

**1. 包**

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

`@quiesco/vitest` 是 Vitest 的集成；[Jest 和 Cucumber](https://docs.quiesco.dev/zh/runners.md) 的做法相同。

**2. Metro**

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

const config = {};

module.exports = withQuiesco(mergeConfig(getDefaultConfig(__dirname), config));
```

`withQuiesco()` 会把 agent 放进以 `EXPO_PUBLIC_QUIESCO=1` 生成的 bundle，其他 bundle 里放的是一个
空实现。在不使用 Expo 的应用里，这个变量的名字不变。`init` 会包装 React Native 模板自带的配置；
其他写法的配置，它会告诉你要加什么。

**3. 入口文件**

```js index.js theme={null}
import { QuiescoRoot } from '@quiesco/agent'; // 必须保持为第一个 import
import { createElement } from 'react';
import { AppRegistry } from 'react-native';

import App from './App';
import { name as appName } from './app.json';

// 在没有 EXPO_PUBLIC_QUIESCO=1 的构建里，<QuiescoRoot> 原样渲染 App。
function QuiescoApp() {
  return createElement(QuiescoRoot, null, createElement(App));
}

AppRegistry.registerComponent(appName, () => QuiescoApp);
```

agent 必须是应用的第一个 import，`<QuiescoRoot>` 负责把应用连接到测试进程。
对 React Native 模板的入口文件，`init` 会写成这样；你也可以自己在
`App.tsx` 里包住根组件。

**4. 配置文件**

```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',
    },
    android: {
      package: 'com.example.myapp',
      binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk',
      // debug 构建从主机 localhost 上的 Metro 加载 JS。
      reversePorts: [8081],
    },
  },
  // Metro 没有运行时，quiesco test 会启动它。
  devServer: { url: 'http://localhost:8081', command: ['npx', 'react-native', 'start', '--port', '{port}'] },
});
```

应用 ID 是从原生工程里读出来的。`devServer.url` 表示 debug 构建从 Metro 加载
JS：Metro 没有运行时，`quiesco test` 会启动它。`reversePorts` 让模拟器能访问到它。
所有选项见[配置](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
> ```

`quiesco build ios` 运行 `pod install` 和 `xcodebuild`；`quiesco build android` 删除 React Native 的
autolinking 列表并运行 `gradlew`。两者都会设置 `EXPO_PUBLIC_QUIESCO=1`，因为只有这样 autolinking 才会链接
`@quiesco/native`（[CLI](https://docs.quiesco.dev/zh/cli.md#without-expo)）。

运行日志会显示应用里是否有原生模块（`native module true`）。如果显示的是
`false`，说明构建时没有设置这个变量，或者用了之前某次构建的 autolinking 结果：
这时动作会退回到 [`js` 模式](./api-actions)。

## 不用 CLI 构建

`quiesco build` 运行的就是普通的 React Native 构建。下面是手动的做法，比如 CI 任务里
已经有自己的构建步骤时：

> [tab] iOS
>
> ```sh theme={null}
> cd ios
> EXPO_PUBLIC_QUIESCO=1 pod install
> EXPO_PUBLIC_QUIESCO=1 xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -configuration Debug \
>   -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' -derivedDataPath build build
> ```

> [tab] Android
>
> ```sh theme={null}
> rm -rf android/build/generated/autolinking
> cd android
> EXPO_PUBLIC_QUIESCO=1 ./gradlew assembleDebug
> ```
>
> React Native 的 Gradle 插件把已链接库的列表保存在
> `android/build/generated/autolinking` 中，直到某个 lockfile 发生变化；删除它，Gradle 就会重新查询。

debug 构建从 Metro 加载 JS，Metro 也必须带着这个变量运行（`quiesco test` 就是这样启动它的）。
release 构建的 JS 已经打包在应用里；带着这个变量构建，并从
`quiesco.config.mts` 里去掉 `devServer`。

## e2e 以外的构建

不设置这个变量时，autolinking 不会链接 `@quiesco/native`，Metro 打包进去的也是代替 agent 的空实现，
所以正式发布的版本里没有任何 Quiesco 的代码，应用也不需要为此做任何设置。从 e2e 构建切换回来时：

* iOS：不带这个变量重新运行 `pod install`。在此之前，这个 pod 一直留在 Xcode 工程里。
* Android：如果构建仍会按缓存的列表链接这个模块，它会带着提示信息停下，并删除那份列表；
  再运行一次即可。

> [!WARNING]
>
> `@quiesco/native` 用 UIKit 的私有 API 注入触摸，App Store 不接受这类 API。正式构建要在
> `pod install` 没有带 `EXPO_PUBLIC_QUIESCO` 运行过的代码目录里进行，CI 上本来就是这样。

## Web

应用如果还通过 `react-native-web` 在浏览器里运行，也可以在浏览器里测试。Web 构建由项目自己负责。
Quiesco 支持用 Vite 做的 Web 构建：`quiescoAgent()` 对 Vite 的作用，相当于 `withQuiesco()` 对 Metro 的作用。

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

export default defineConfig({
  plugins: [quiescoAgent()],
  resolve: {
    alias: [{ find: /^react-native$/, replacement: 'react-native-web' }],
    extensions: ['.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
  },
});
```

这个插件在带 `EXPO_PUBLIC_QUIESCO=1` 的构建里放入 agent，在其他构建里换成空实现。在 Web 构建的入口文件
（`index.html` 里的模块脚本）中，agent 同样必须是第一个 import。

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

export default defineConfig({
  apps: {
    web: { url: 'http://localhost:5173' },
  },
});
```

```sh theme={null}
npm install --save-dev @quiesco/web playwright
npx playwright install chromium
npx quiesco test --platform web
```

`apps.web.url` 上没有响应时，`quiesco test` 会带着这个变量启动 Vite 的开发服务器，结束后再停止它。
要测试构建好的应用，把 `apps.web.distDir` 设为带这个变量执行 `vite build` 的输出目录。
项目里有 `react-native-web` 和 Vite 配置时，`quiesco init` 会完成这些配置。
在浏览器里，触摸和文本输入由 Playwright 完成，所以 Web 构建里没有 `@quiesco/native`。目前只有 Vite 有对应的插件。

## 与 Expo 应用的不同之处

* 应用必须能连接到 `localhost` 上的测试进程。debug 构建可以；release 构建需要在
  Info.plist 的 App Transport Security 设置里有 `NSAllowsLocalNetworking`（React Native 模板里已有），
  在 Android 上还需要一份允许对 `localhost` 使用明文流量的网络安全配置。
  对 Expo 应用，这两项由 `@quiesco/expo-plugin` 写入。
* `apps.<platform>.devClient` 用于 Expo 的 development build。不要设置它：React Native 的 debug 构建
  会自己找到 Metro，`devServer.url` 则告诉 `quiesco test` Metro 在哪里。
* `app.device.relaunch({ url })` 通过 React Native 的 `Linking.getInitialURL()` 把链接交给应用。
* Web 构建由项目自己用 Vite 完成，不使用 Expo 的 Web 导出（[Web](#web)）。
* `quiesco build` 从不生成或删除 `ios/` 和 `android/`，并且每次都会构建：原生工程是你自己的。


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