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

fix(firebase): validate custom serverFunctionName #2773

Merged
merged 7 commits into from
Oct 8, 2024
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
4 changes: 4 additions & 0 deletions docs/2.deploy/20.providers/firebase.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,7 @@ export default defineNuxtConfig({
```

::

::important
`firebase.serverFunctionName` must be a valid JS variable name and cannot include dashes (`-`).
::
13 changes: 12 additions & 1 deletion src/presets/firebase/preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineNitroPreset } from "nitropack/kit";
import { basename } from "pathe";
import type { Plugin } from "rollup";
import { genSafeVariableName } from "knitwork";
import { updatePackageJSON, writeFirebaseConfig } from "./utils";

export type { FirebaseOptions as PresetOptions } from "./types";
Expand Down Expand Up @@ -33,6 +34,16 @@ const firebase = defineNitroPreset(
nitro.options.appConfig.nitro = nitro.options.appConfig.nitro || {};
nitro.options.appConfig.nitro.firebase = nitro.options.firebase;

const { serverFunctionName } = nitro.options.firebase;
if (
serverFunctionName &&
serverFunctionName !== genSafeVariableName(serverFunctionName)
) {
throw new Error(
`\`firebase.serverFunctionName\` must be a valid JS variable name: \`${serverFunctionName}\``
);
}

// Replace __firebaseServerFunctionName__ to actual name in entries
(rollupConfig.plugins as Plugin[]).unshift({
name: "nitro:firebase",
Expand All @@ -41,7 +52,7 @@ const firebase = defineNitroPreset(
return {
code: code.replace(
/__firebaseServerFunctionName__/g,
nitro.options.firebase?.serverFunctionName || "server"
serverFunctionName || "server"
),
map: null,
};
Expand Down