firebase functions & monorepo の tips メモ
codebase を設定する
"functions": {
"codebase": "my-codebase"
}
fiebase.json の codebase を設定すると、firebase functions を deploy する際に、そのソース配下の functions のみを分割して deploy できる。
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)
これがないと、deploy するたびに上記のような質問が prompt が表示される。別の workspace で管理する functions を削除しないように注意しなければならず、最悪ミスると functions が削除されてしまい危険。
複数指定する場合は配列を利用 ↓
"functions": [
{
"source": "teamA",
"codebase": "team-a"
},
{
"source": "teamB",
"codebase": "team-b"
},
]
functions を grouping する
functions を object に nest させることで grouping が可能。
export const myCompany = {
teamA: {
func1: functions.https.onRequest((request, response) => {
// ...
}),
func2: functions.https.onRequest((request, response) => {
// ...
}),
},
teamB: {
func3: functions.https.onRequest((request, response) => {
// ...
}),
func4: functions.https.onRequest((request, response) => {
// ...
}),
}
}
deploy する際に、function 名を指定することでスコープを制御することができる。
firebase deploy --only functions:myCompany.teamA
=> func1, func2 が deploy される
firebase deploy --only functions:myCompany.teamB
=> func3, func4 が deploy される
今は codebase による管理ができるようになったので、このようなスコープの制御は不要になったかもしれない...
codebase & grouping を利用した functions の deploy
codebase を指定した上で、かつ grouping した functions の一部を deploy する場合は、対象の functions を functions:{codebase}:{grouping} で指定する。
firebase deploy --only functions:my-codebase:myGroup.myFunc
この場合、my-codebase の myGroup.myFunc の functions が deploy される。
"functions": [
{
"codebase": "my-codebase"
},
]
export const myGroup = {
// ↓この function を指定
myFunc: functions.https.onRequest((request, response) => {
// ...
}),
// ...
}
deploy 時のこの挙動については、こちらの issue で詳細に説明されている↓
Running firebase deploy --only functions:hadsignup means that the deployment logic will first look for a codebase named hadsignup; if no codebase by that name is found, then we will look for a specific function named hadsignup in the default codebase to deploy. However, if your hadsignup function is not in the default codebase, you can specify a codebase prefix for the function, e.g. [codebase-name]:hadsignup.
functions:foo を指定した場合、
- まず、
fooという名前の codebase を探す - なければ、
fooという名前の function をdefaultcodebase から探す
という挙動になる。