Reactウェブサイト
このジェネレータは、CloudScapeが設定された新しいReactウェブサイトと、S3でホストされた静的ウェブサイトをCloudFrontで配信しWAFで保護するクラウドデプロイ用のAWS CDKインフラストラクチャを作成します。
生成されるアプリケーションはビルドツールとバンドラーとしてViteを使用し、型安全なルーティングにTanStack Routerを採用しています。
Reactウェブサイトの生成
Section titled “Reactウェブサイトの生成”新しいReactウェブサイトは2つの方法で生成できます:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#react-website
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#react-website
yarn nx g @aws/nx-plugin:ts#react-website
npx nx g @aws/nx-plugin:ts#react-website
bunx nx g @aws/nx-plugin:ts#react-website
変更されるファイルを確認するためにドライランを実行することもできます
pnpm nx g @aws/nx-plugin:ts#react-website --dry-run
yarn nx g @aws/nx-plugin:ts#react-website --dry-run
npx nx g @aws/nx-plugin:ts#react-website --dry-run
bunx nx g @aws/nx-plugin:ts#react-website --dry-run
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
name 必須 | string | - | The name of the application. |
directory | string | packages | The directory of the new application. |
enableTailwind | boolean | true | Enable TailwindCSS for utility-first styling. |
enableTanstackRouter | boolean | true | Enable Tanstack router for type-safe routing. |
ジェネレータの出力
Section titled “ジェネレータの出力”ジェネレータは<directory>/<name>
ディレクトリに以下のプロジェクト構造を作成します:
- index.html HTMLエントリポイント
- public 静的アセット
Directorysrc
- main.tsx Reactセットアップを含むアプリケーションエントリポイント
- config.ts アプリケーション設定(ロゴなど)
Directorycomponents
- AppLayout CloudScapeレイアウトとナビゲーションバー用コンポーネント
Directoryhooks
- useAppLayout.tsx ネストされたコンポーネントからAppLayoutを調整するフック
Directoryroutes
Directorywelcome
- index.tsx @tanstack/react-router用のサンプルルート(ページ)
- styles.css グローバルスタイル
- vite.config.ts ViteとVitestの設定
- tsconfig.json ソースとテスト用の基本TypeScript設定
- tsconfig.app.json ソースコード用TypeScript設定
- tsconfig.spec.json テスト用TypeScript設定
また、packages/common/constructs
ディレクトリにウェブサイトデプロイ用のCDKインフラストラクチャコードを生成します:
Directorysrc
Directoryapp
Directorystatic-websites
- <name>.ts ウェブサイト固有のインフラストラクチャ
Directorycore
- static-website.ts 汎用StaticWebsiteコンストラクト
Reactウェブサイトの実装
Section titled “Reactウェブサイトの実装”ReactドキュメントはReact開発の基本を学ぶのに最適です。CloudScapeドキュメントでは利用可能なコンポーネントとその使用方法を確認できます。
ルーティング
Section titled “ルーティング”ルート/ページの作成
Section titled “ルート/ページの作成”CloudScapeウェブサイトにはデフォルトでTanStack Routerが設定されています。新しいルートの追加が容易です:
- ローカル開発サーバーを起動
src/routes
に新しい<page-name>.tsx
ファイルを作成(ファイルツリーの位置がパスを表現)Route
とRouteComponent
が自動生成されます。ここでページ構築を開始できます!
ページ間ナビゲーション
Section titled “ページ間ナビゲーション”Link
コンポーネントまたはuseNavigate
フックを使用してページ間を移動:
import { Link, useNavigate } from '@tanstack/react-router';
export const MyComponent = () => { const navigate = useNavigate();
const submit = async () => { const id = await ... // 非同期処理後のリダイレクトに`navigate`を使用 navigate({ to: '/products/$id', { params: { id }} }); };
return ( <> <Link to="/products">キャンセル</Link> <Button onClick={submit}>送信</Button> </> )};
詳細はTanStack Routerドキュメントを参照。
ランタイム設定
Section titled “ランタイム設定”AWS CDKインフラストラクチャからの設定は、Runtime Configurationを通じてウェブサイトに提供されます。これにより、デプロイ時まで不明なAPI URLなどの詳細にアクセス可能になります。
インフラストラクチャ
Section titled “インフラストラクチャ”RuntimeConfig
CDKコンストラクトを使用して設定の追加/取得が可能です。@aws/nx-plugin
で生成されたCDKコンストラクト(tRPC APIやFastAPIなど)は自動的にRuntimeConfig
に適切な値を追加します。
ウェブサイトCDKコンストラクトはランタイム設定をruntime-config.json
ファイルとしてS3バケットのルートにデプロイします。
import { Stack } from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite } from ':my-scope/common-constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string) { super(scope, id);
// RuntimeConfigに自動的に値を追加 new MyApi(this, 'MyApi', { integrations: MyApi.defaultIntegrations(this).build(), });
// runtime-config.jsonへ自動デプロイ new MyWebsite(this, 'MyWebsite'); }}
runtime-config.json
に設定を反映させるには、ウェブサイトの宣言をRuntimeConfig
に追加するコンストラクトより後に行う必要があります。
ウェブサイトコード
Section titled “ウェブサイトコード”useRuntimeConfig
フックを使用してランタイム設定値を取得:
import { useRuntimeConfig } from '../hooks/useRuntimeConfig';
const MyComponent = () => { const runtimeConfig = useRuntimeConfig();
// ランタイム設定値にアクセス const apiUrl = runtimeConfig.apis.MyApi;};
ローカルランタイム設定
Section titled “ローカルランタイム設定”ローカル開発サーバー実行時には、バックエンドURLや認証設定などを知らせるためpublic
ディレクトリにruntime-config.json
ファイルが必要です。
ウェブサイトプロジェクトはデプロイ済みアプリケーションからruntime-config.json
を取得するload:runtime-config
ターゲットを装備:
pnpm nx run <my-website>:"load:runtime-config"
yarn nx run <my-website>:"load:runtime-config"
npx nx run <my-website>:"load:runtime-config"
bunx nx run <my-website>:"load:runtime-config"
インフラストラクチャプロジェクトのsrc/main.ts
でステージ名プレフィックスを変更した場合、ウェブサイトのproject.json
ファイル内のload:runtime-config
ターゲットを更新する必要があります。
また、このターゲットは単一ステージがAWS認証情報の環境にデプロイされていることを前提としています。同一アカウント/リージョンに複数ステージをデプロイする場合はコマンドを調整してください。
ローカル開発サーバー
Section titled “ローカル開発サーバー”serve
またはserve-local
ターゲットを使用してローカル開発サーバーを起動可能です。
Serveターゲット
Section titled “Serveターゲット”serve
ターゲットはウェブサイトのローカル開発サーバーを起動します。このターゲットを使用するには、ウェブサイトが連携するインフラストラクチャのデプロイとローカルランタイム設定の読み込みが必要です。
次のコマンドで実行:
pnpm nx run <my-website>:serve
yarn nx run <my-website>:serve
npx nx run <my-website>:serve
bunx nx run <my-website>:serve
このターゲットは「本番」APIを参照しながらウェブサイトを変更する場合に有用です。
Serve Localターゲット
Section titled “Serve Localターゲット”serve-local
ターゲットはViteのMODE
をserve-local
に設定したローカルサーバーを起動し、API接続ジェネレータで接続したAPIのローカルサーバーも同時起動します。
このターゲットで起動した場合、runtime-config.json
は自動的にローカルAPI URLを指すように上書きされます。
次のコマンドで実行:
pnpm nx run <my-website>:serve-local
yarn nx run <my-website>:serve-local
npx nx run <my-website>:serve-local
bunx nx run <my-website>:serve-local
このターゲットはウェブサイトとAPIを同時に開発する際の迅速な反復処理に最適です。
このモードで実行時、runtime-config.json
が存在せずCloudScapeウェブサイト認証を設定している場合、ログインがスキップされ認証ヘッダーが含まれません。
serve-local
で認証を有効化するには、インフラストラクチャをデプロイしランタイム設定を読み込んでください。
build
ターゲットでウェブサイトをビルド可能です。Viteを使用して本番用バンドルをdist/packages/<my-website>/bundle
ディレクトリに生成し、型チェック・コンパイル・リンターを実行します。
pnpm nx run <my-website>:build
yarn nx run <my-website>:build
npx nx run <my-website>:build
bunx nx run <my-website>:build
テストの作成方法は標準TypeScriptプロジェクトと同様です。TypeScriptプロジェクトガイドを参照してください。
React固有のテストにはReact Testing Libraryが事前インストールされています。詳細はReact Testing Libraryドキュメントを参照。
次のコマンドでテストを実行:
pnpm nx run <my-website>:test
yarn nx run <my-website>:test
npx nx run <my-website>:test
bunx nx run <my-website>:test
ウェブサイトのデプロイ
Section titled “ウェブサイトのデプロイ”デプロイにはTypeScriptインフラストラクチャジェネレータを使用したCDKアプリケーションの作成を推奨します。
packages/common/constructs
に生成されたCDKコンストラクトを使用してウェブサイトをデプロイ可能です。
import { Stack } from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite } from ':my-scope/common-constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string) { super(scope, id);
new MyWebsite(this, 'MyWebsite'); }}