> ## 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/ja/quickstart.md)では、新しいアプリからテストが通るまで、この2つを順に進めます。このページでは、
何が設定されるのかを説明します。プロジェクトを確認するときや、手順の一部を手作業で行うときに使ってください。Expoを使わないアプリは
[素のReact Nativeアプリのセットアップ](https://docs.quiesco.dev/ja/react-native.md)を見てください。

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

`init`は使うテストランナー（Vitest、Jest、Cucumber）を尋ねます。既存のファイルは上書きせず、
自分で行う作業を一覧で示します（[CLI](https://docs.quiesco.dev/ja/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になり、JSをMetroから読み込みます。
`@quiesco/vitest`はVitest用の連携パッケージです。[JestとCucumber](https://docs.quiesco.dev/ja/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()`は、`EXPO_PUBLIC_QUIESCO=1`を付けて作ったバンドルにはエージェントを入れ、それ以外の
バンドルには何もしないスタブを入れます。

**4. エントリーファイル**

```ts index.ts theme={null}
import '@quiesco/agent'; // 必ず最初のimportにする
import 'expo-router/entry';
```

エージェントはアプリの最初の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`はこの作業を行いません。ルートレイアウト（`src/app/_layout.tsx`）を
`<QuiescoRoot>`で囲んでください。

**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はJSをMetroから読み込みます。
      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/ja/config.md)にあります。ランナー自身の設定と最初のテストは
[テストランナー](https://docs.quiesco.dev/ja/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/ja/cli.md#quiesco-build)）。autolinkingが`@quiesco/native`をリンクするのは、この変数があるときだけです。EASで
ビルドする場合は[EAS Build](https://docs.quiesco.dev/ja/eas.md)を見てください。

## e2eビルド以外のビルド

`EXPO_PUBLIC_QUIESCO=1`がなければ、Metroはエージェントの代わりにスタブをバンドルし、autolinkingは
`@quiesco/native`をリンクせず、config pluginも何も書き込みません。そのためリリース版にQuiescoのコードは入らず、
そのための設定も要りません。ネイティブのディレクトリは1種類のビルド用に作られます。e2eビルドのあとに同じチェックアウトからリリースをビルドするときは、
先に変数なしで`npx expo prebuild --clean`を実行してください。


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