CloudScapeウェブサイト
このジェネレータはReactのウェブサイトを新規作成し、CloudScapeの設定と、静的ウェブサイトをクラウドにデプロイするためのAWS CDKインフラストラクチャ(S3ホスティング、CloudFront配信、WAF保護)を提供します。
生成されるアプリケーションはビルドツールとしてViteを、型安全なルーティングにTanStack Routerを使用します。
使用方法
CloudScape Websiteの生成
新しいCloudScape Websiteを2つの方法で生成できます:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#cloudscape-website
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#cloudscape-website
yarn nx g @aws/nx-plugin:ts#cloudscape-website
npx nx g @aws/nx-plugin:ts#cloudscape-website
bunx nx g @aws/nx-plugin:ts#cloudscape-website
変更されるファイルを確認するためにドライランを実行することもできます
pnpm nx g @aws/nx-plugin:ts#cloudscape-website --dry-run
yarn nx g @aws/nx-plugin:ts#cloudscape-website --dry-run
npx nx g @aws/nx-plugin:ts#cloudscape-website --dry-run
bunx nx g @aws/nx-plugin:ts#cloudscape-website --dry-run
オプション
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
name 必須 | string | - | The name of the application. |
directory | string | packages | The directory of the new application. |
ジェネレータの出力
ジェネレータは<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コンストラクト
CloudScape Websiteの実装
ReactドキュメントはReact開発の基礎を学ぶのに適しています。CloudScapeドキュメントでは利用可能なコンポーネントとその使用方法を確認できます。
ルーティング
ルート/ページの作成
TanStack Routerが設定済みの状態で、新しいルートを簡単に追加できます:
- ローカル開発サーバーを起動
src/routes
に新しい<page-name>.tsx
ファイルを作成(ファイルツリーの位置がパスを表現)Route
とRouteComponent
が自動生成されます。ここでページの構築を開始できます!
ページ間ナビゲーション
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ドキュメントを参照してください。
ランタイム設定
AWS CDKインフラストラクチャからの設定はRuntime Configurationを通じてウェブサイトに提供されます。これにより、デプロイ時まで不明なAPI URLなどの詳細情報にアクセス可能です。
インフラストラクチャ
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');
// runtime-config.jsonへ自動デプロイ new MyWebsite(this, 'MyWebsite'); }}
runtime-config.json
ファイルに値が確実に含まれるよう、RuntimeConfig
に値を追加するコンストラクトの後にウェブサイトを宣言してください。
ウェブサイトコード
useRuntimeConfig
フックを使用してランタイム設定値を取得できます:
import { useRuntimeConfig } from '../hooks/useRuntimeConfig';
const MyComponent = () => { const runtimeConfig = useRuntimeConfig();
// ランタイム設定値にアクセス const apiUrl = runtimeConfig.httpApis.MyApi;};
ローカルランタイム設定
ローカル開発サーバーを実行する際、バックエンドURLや認証設定などを認識させるためpublic
ディレクトリに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
ターゲットを更新する必要があります。
ローカル開発サーバー
ローカル開発サーバーを起動する前に、インフラストラクチャのデプロイとローカルランタイム設定の読み込みが完了していることを確認してください。
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
ビルド
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 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
ウェブサイトのデプロイ
TypeScript Infrastructure Generatorを使用して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'); }}