13
13

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 5 years have passed since last update.

FirebaseとReactjs - Hosting

Last updated at Posted at 2017-10-06

はじめに

最近FirebaseやReactの勉強をはじめたばかりで、自分なりに備忘録をつけていこうと思います。
ご指摘等ありましたら、ぜひお願いします。
今回は環境設定について書いていきます。

React.jsの準備

Reactの導入にはcreate-react-appコマンドを使います。
今回は get-start-fr という名前のプロジェクトを作成していきます。

$ create-react-app get-start-fr

Firebaseの準備

firebaseのコンソールで新しいプロジェクトを作成します。

次にコマンドを叩いていきます。一行目でFirebase CLIをインストールしていますが、インストールするにはNode.jsをインストールする必要があります。

$ npm install -g firebase-tools

これでFirebaseコマンドが使えるようになりました。
次はログインです。

$ firebase login

すでにログインしているときはAlready logged in as <アカウント名>と表示されます。アカウントを切り替えたいときは$firebase logoutでログアウトできます。
ログインしたら、さっき作ったディレクトリに移動してFirebaseを初期化します。

$ cd get-start-fr
$ firebase init

$ firebase initを叩くと色々と質問が飛んできます。

? Which Firebase CLI features do you want to setup for this folder? Press Spa
ce to select features, then Enter to confirm your choices.

->Hosting: Configure and deploy Firebase Hosting sites

? Select a default Firebase project for this directory: 

->get-start-fr (get-start-fr)

? What do you want to use as your public directory? (public)

->public

? Configure as a single-page app (rewrite all urls to /index.html)?

->No

? File public/index.html already exists. Overwrite? (y/N)

->No

次に、npm run buildでbuildファイルを生成します。
現在の状態のままでデプロイすると、publicがデフォルトで読み込まれるので、buildファイルが読み込まれるようにfirebase.jsonを編集します。

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

を次のように編集しました。

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

これでFirebaseの準備ができました。

Firebaseへデプロイする

ではビルドします。

npm run build

ビルドしたらデプロイします。

$ firebase deploy

すると、

Hosting URL: https://get-start-fr.firebaseapp.com

と出るのでurlを叩くと、デプロイできているか確認できます。

所感

「とりあえず動く」くらいのハードルでやって見ました。これでうまくいくとはちょっと思えないので

13
13
0

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
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?