diff --git a/docs/content/2.deploy/providers/firebase.md b/docs/content/2.deploy/providers/firebase.md index 10b2c61aed..eca9471b3a 100644 --- a/docs/content/2.deploy/providers/firebase.md +++ b/docs/content/2.deploy/providers/firebase.md @@ -244,3 +244,33 @@ You may be warned that other cloud functions will be deleted when you deploy you ```bash firebase deploy --only functions:server,hosting ``` + +## Advanced + +### Renaming Function + +When deploying multiple apps within the same Firebase project, you must give your server a unique name in order to avoid overwriting +your functions. + +You can specify a new name for the deployed Firebase function in your configuration: + +::code-group +```ts [nitro.config.ts] +import { defineNitroConfig } from 'nitropack/config' + +export default defineNitroConfig({ + firebase: { + serverFunctionName: "" + } +}) +``` +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + nitro: { + firebase: { + serverFunctionName: "" + } + } +}) +``` +:: diff --git a/src/presets/firebase.ts b/src/presets/firebase.ts index 564a5651b1..0ddc1abdb2 100644 --- a/src/presets/firebase.ts +++ b/src/presets/firebase.ts @@ -1,6 +1,7 @@ import { existsSync } from "node:fs"; -import { join, relative } from "pathe"; +import { join, relative, basename } from "pathe"; import { readPackageJSON, writePackageJSON } from "pkg-types"; +import type { Plugin } from "rollup"; import { writeFile } from "../utils"; import { defineNitroPreset } from "../preset"; import type { Nitro } from "../types"; @@ -19,7 +20,7 @@ export const firebase = defineNitroPreset({ await writeFirebaseConfig(nitro); await updatePackageJSON(nitro); }, - "rollup:before": (nitro) => { + "rollup:before": (nitro, rollupConfig) => { const _gen = nitro.options.firebase?.gen as unknown; if (!_gen || _gen === "default") { nitro.logger.warn( @@ -30,6 +31,22 @@ export const firebase = defineNitroPreset({ } nitro.options.appConfig.nitro = nitro.options.appConfig.nitro || {}; nitro.options.appConfig.nitro.firebase = nitro.options.firebase; + + // Replace __firebaseServerFunctionName__ to actual name in entries + (rollupConfig.plugins as Plugin[]).unshift({ + name: "nitro:firebase", + transform: (code, id) => { + if (basename(id).startsWith("firebase-gen-")) { + return { + code: code.replace( + /__firebaseServerFunctionName__/g, + nitro.options.firebase?.serverFunctionName || "server" + ), + map: null, + }; + } + }, + } satisfies Plugin); }, }, }); diff --git a/src/runtime/entries/firebase-gen-1.ts b/src/runtime/entries/firebase-gen-1.ts index aff2d7b939..eaf1601396 100644 --- a/src/runtime/entries/firebase-gen-1.ts +++ b/src/runtime/entries/firebase-gen-1.ts @@ -6,7 +6,7 @@ import { useAppConfig } from "#internal/nitro"; const firebaseConfig = useAppConfig().nitro.firebase; -export const server = functions +export const __firebaseServerFunctionName__ = functions .region(firebaseConfig.region ?? functions.RESET_VALUE) .runWith(firebaseConfig.runtimeOptions ?? functions.RESET_VALUE) .https.onRequest(toNodeListener(nitroApp.h3App)); diff --git a/src/runtime/entries/firebase-gen-2.ts b/src/runtime/entries/firebase-gen-2.ts index d4a42d59d8..4aabb4063e 100644 --- a/src/runtime/entries/firebase-gen-2.ts +++ b/src/runtime/entries/firebase-gen-2.ts @@ -6,7 +6,7 @@ import { useAppConfig } from "#internal/nitro"; const firebaseConfig = useAppConfig().nitro.firebase; -export const server = onRequest( +export const __firebaseServerFunctionName__ = onRequest( { // Must be set to public to allow all public requests by default invoker: "public", diff --git a/src/runtime/entries/firebase-gen-default.ts b/src/runtime/entries/firebase-gen-default.ts index 3418a0e4df..4b5765c612 100644 --- a/src/runtime/entries/firebase-gen-default.ts +++ b/src/runtime/entries/firebase-gen-default.ts @@ -1,2 +1,2 @@ -// we need this file to detect if the user is passing a `gen` property -export { server } from "./firebase-gen-1"; +// We need this file to detect if the user is not passing a `gen` property +export { __firebaseServerFunctionName__ } from "./firebase-gen-1"; diff --git a/src/types/presets.ts b/src/types/presets.ts index e1619f10be..e47116bc73 100644 --- a/src/types/presets.ts +++ b/src/types/presets.ts @@ -63,6 +63,13 @@ interface FirebaseOptionsBase { * @see https://cloud.google.com/functions/docs/concepts/nodejs-runtime */ nodeVersion?: "20" | "18" | "16"; + /** + * When deploying multiple apps within the same Firebase project + * you must give your server a unique name in order to avoid overwriting your functions. + * + * @default "server" + */ + serverFunctionName?: string; } interface FirebaseOptionsGen1 extends FirebaseOptionsBase {