Skip to content

Commit

Permalink
fix(firebase): validate custom serverFunctionName (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsc authored Oct 8, 2024
1 parent 92519e0 commit 1bf47ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit 1bf47ae

Please sign in to comment.