1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

React+Typescriptアプリをgithub pagesにデプロイする

Last updated at Posted at 2023-07-01

はじめに

Vite React Typescriptで作成したアプリをgithub pagesにデプロイする方法。
今更感がありますがメモしておきます。

前提条件

yarn、viteは導入済み

プロジェクト作成

下記のコマンドを実行。

yarn create vite
  1. プロジェクト名を入力
  2. Reactを選択
  3. Typescriptを選択
  4. 作成したプロジェクトのディレクトリに移動

package.jsonの編集

homepageとdeployを追加

{
    ...
    "homepage": "https://villhell.github.io/リポジトリ名",
    "scripts": {
    ...
    "deploy": "yarn build && gh-pages -d build"
  },
}

vite.config.tsの編集

baseを追加。

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  base: '/githubのリポジトリ名/',
  plugins: [react()],
});

git

git init
  • .gitignoreファイルからdistをコメントアウト
git add .
git commit -m "first commit."

ここで一旦Pushしておきます。
githubにアクセスしてリポジトリが作成できているか確認して下さい。

gh-pagesブランチを作成

このブランチはサブディレクトリで管理したいので以下の手順を実行。

git checkout --orphan gh-pages
git reset --hard
echo 'Hello' > index.html
git add index.html
git commit -am 'Create gh-pages'

ここでも一旦pushしておいてgh-pagesにindex.htmlがあることを確認してください。

git worktreeを使う

git checkout master
git worktree add dist gh-pages

これでmasterからブランチを切り替えずにgh-pagesブランチをコミットすることができるようになります。

package.jsonの内容をインストール

yarn

viteをインストール

yarn add vite

buildする

yarn build

buildができればコミットしてプッシュします。

githubの設定

githubで先程プッシュしたページにアクセスします。

「Setting」をクリック
image.png

左側のメニューから「Pages」をクリックして、ブランチをgh-pagesにしてSaveしてください。
image.png

少し待つとページへのリンクが表示されるようになるので「Visit Site」をクリック
image.png

画面が表示されていればOKです!
image.png

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?