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 “ルート/ページの作成”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インフラストラクチャからの設定は、ランタイム設定を通じてウェブサイトに提供されます。これにより、デプロイ時まで不明なAPI URLなどの詳細にアクセス可能になります。
インフラストラクチャ
Section titled “インフラストラクチャ”RuntimeConfig
CDKコンストラクトを使用して設定の追加と取得が可能です。@aws/nx-plugin
で生成されたCDKコンストラクト(tRPC APIやFastAPIなど)は自動的に適切な値をRuntimeConfig
に追加します。
ウェブサイトCDKコンストラクトは、ランタイム設定をS3バケットのルートにruntime-config.json
ファイルとしてデプロイします。
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
ファイルに設定が含まれるように、ウェブサイトの宣言は他のコンストラクトより後に行う必要があります。
ウェブサイトコード
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
ターゲットを更新する必要があります。
ローカル開発サーバー
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ドキュメントを参照してください。
test
ターゲットでテストを実行できます:
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'); }}