この記事の最終更新日: 2023年4月6日
![React StoryBook TypeScript](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/04/react_storybook_typescript.png?resize=1001%2C1000&ssl=1)
開始時ディレクトリ構成
.
└ docker
| └ Dockerfile
└ docker-compose.yml
Dockerfile
FROM node:19.8.1
WORKDIR /data/src
RUN npm install -g npm@latest && npm install -g vite@latest
docker-compose.yml
version: '3'
services:
app:
build: ./docker
container_name: front
tty: true
volumes:
- ./src:/data
ports:
- "5173:5173"
- "6006:6006"
React+TypeScriptプロジェクトを作成
dockerディレクトリ上で、以下のコマンドを実行
$ docker-compose build
$ docker-compose run --rm app npm create vite@latest .
yを押して、言語とビルドの設定を選択すると、プロジェクトが作成されます。
(base) react-ts % docker-compose run --rm app npm create vite@latest .
[+] Running 1/1
⠿ Network ln-sb_default Created 0.1s
Need to install the following packages:
create-vite@4.2.0
Ok to proceed? (y) y
✔ Select a framework: › React
✔ Select a variant: › TypeScript + SWC
Scaffolding project in /data/src...
Done. Now run:
npm install
npm run dev
(base) react-ts %
コンテナ上で、npm install
Dockerコンテナを立ち上げ、コンテナに入る。
その後npm install, npm run build, npm run devを実行。
$ docker-compose up -d
$ docker exec -it front sh
$ npm install
$ npm run build
$ npm run dev
React + TypeScript + Viteのプロジェクトはこれで完成です。
http://localhost:5173/
![React Vit TypeScript](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/04/スクリーンショット-2023-04-06-11.43.12.png?resize=1024%2C519&ssl=1)
StoryBook導入
次はStoryBookを導入します。
$ docker-compose up -d
$ docker exec -it front sh
コンテナを起動後、実行コマンドを入力。
$ npx sb@latest init
Need to install the following packages:
sb@7.0.2
Ok to proceed? (y)
$ npm run storybook
このような出力が出ればnpm run storybookしましょう。
(npx sb@latest initコマンド入力後は時間がかかるので、しばらく待ちましょう。)
![npx sb@latest init](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/04/スクリーンショット-2023-04-05-2.49.22.png?resize=1024%2C684&ssl=1)
npm run storybookでビルドが完了すると以下のような出力になります。
![ビルド完了](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/04/スクリーンショット-2023-04-05-2.57.04.png?resize=1024%2C836&ssl=1)
画面の確認
http://localhost:6006/?path=/docs/example-button–docs
![](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/04/スクリーンショット-2023-04-05-2.57.44.png?resize=1024%2C525&ssl=1)
以上で、プロジェクト作成完了です。
質問などがあれば、コメント欄へよろしくお願いします。
![ゆるプロ日記](https://i0.wp.com/yurupro.cloud/wp-content/uploads/2023/01/12245_paint.png?resize=100%2C100&ssl=1)
大阪のエンジニアが書いているブログ。
コメント