LoginSignup
3

More than 5 years have passed since last update.

Cloud Functions for Firebase で Babel

Last updated at Posted at 2017-08-09

Firebase Functionsを使うのにFlowつかいたいけど、コメントスタイルも嫌だったので、Babelした。Flowの設定もはいったままになってます。

要点

firebase deployしたときfunctionsディレクトリを見にいくのを変えて、カレントディレクトリする。package.jsonをコピーしたくなかったから。

firebase.json
{
  "functions": {
    "source": "."
  }
}

Babel後のファイルはdist/index.jsにするので、その位置を指定するために、"main":dist/index.jsにしてる。
デプロイ用のスクリプトはbabel src --out-dir dist && firebase deploy --only functions

package.json
{
{
  "name": "firebase functions babel",
  "version": "1.0.0",
  "author": "Tomohiko Himura <eiel.hal@gmail.com>",
  "license": "MIT",
  "main": "dist/index.js",
  "scripts": {
    "deploy": "babel src --out-dir dist && firebase deploy --only functions"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-preset-env": "^1.6.0",
    "babel-preset-flow": "^6.23.0",
    "flow-bin": "^0.52.0"
  }
}

Cloud Functions runs Node v.6.11.1, so we recommend that you develop locally with this version.

ってかいてあったので、.babelrcで指定

{
  "presets": [
    [
      "env", {
        "targets": { "node": "v6.11.1" }
      }
    ],
    "flow"
  ]
}

サンプルコード。

src/index.js
// @flow
import * as functions from "firebase-functions"

export const https_test = functions.https.onRequest((request, response) => {
  response.send('ok');
});

参考

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
3