Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firebase): support renaming exported server function #1377

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/content/2.deploy/providers/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<new_function_name>"
}
})
```
```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
firebase: {
serverFunctionName: "<new_function_name>"
}
}
})
```
::
21 changes: 19 additions & 2 deletions src/presets/firebase.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand All @@ -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);
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/entries/firebase-gen-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
2 changes: 1 addition & 1 deletion src/runtime/entries/firebase-gen-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/entries/firebase-gen-default.ts
Original file line number Diff line number Diff line change
@@ -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";
7 changes: 7 additions & 0 deletions src/types/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down