jofftiquez
jofftiquez

Reputation: 7708

How to rename firebase functions in nuxt 3 using nitro?

Here's my nuxt.config.js

// https://nuxt.com/docs/api/configuration/nuxt-config
// eslint-disable-next-line no-undef
export default defineNuxtConfig({
  preset: 'node-server',
  modules: [
    '@nuxtjs/tailwindcss',
  ],
  nitro: {
    preset: 'firebase',
  },
  srcDir: './src',
});

I want to change the functions name which is server by default to something else. There seems no option in nitro or nuxt config to do that.

In the .output/server/index.mjs in line 4, the default export name is server

// line 4
export { s as server } from './chunks/nitro/firebase.mjs';

Upvotes: 1

Views: 616

Answers (1)

Wolfgang Siegert
Wolfgang Siegert

Reputation: 11

Just BTW I am having the same problem I think. I want to deploy multiple nuxt3 apps on firebase hosting as websites under one common project. But I need one "server" function to be setup for each nuxt app. Supposedly with a unique name to match with my hosting config. Now the "server" cloud-function is overwritten each time I deploy any nuxt hosting. @jofftiquez is that the same problem you are trying to solve?

I was able to change the function name patching the output/server/index.mjs file generated by the build comand (actually I use a predeploy script with regex to do that): from

export { s as server } from './chunks/nitro/firebase.mjs'

to

export { s as custom-server-name } from './chunks/nitro/firebase.mjs'

then in firebase.json I configured my custom function name:

  "functions": {
    "source": ".output/server",
    "runtime": "nodejs16"
  },
  "hosting": [
    {
      "target": "dev",
      "public": ".output/public",
      "cleanUrls": true,
      "rewrites": [
        {
          "source": "**",
          "function": "custom-server-name"
        }
      ]
    },

And I was able to deploy the function "custom-server-name" with:

firebase deploy --only functions:custom-server-name --project=default

It's the only approach I found so far. You can investigate in the codebase key under the functions in firebase.json. If you deploy from seperate repos it's usefull.

my script stub:

//something like that...
import { readFile, readFileSync, writeFileSync } from 'node:fs'

const env = process.env
// Read deployment config from firebase.json and extract custom function name
const firebaseJSON = JSON.parse(readFileSync('firebase.json'))

//... I extract the rewrites server name from firebase.json based on the target name I extract from env variables

// Read file into a string
readFile('.output/server/index.mjs', 'utf-8', (errRead, contents) => {
  if (errRead) {
    return console.error(errRead)
  }
  console.log('Found index.mjs file contents: ', contents)

  const updated = contents.replace(
    /{ s as server }/gi,
    `{ s as ${serverFunctionName} }`
  )

  // Write back to file
  console.log(
    `Replacing nuxt server function name with':  '${serverFunctionName}'`
  )
  writeFileSync('.output/server/index.mjs', updated, 'utf-8', (errWrite) => {
    if (err) {
      console.log(err)
    }
  })

Currently I moved the script above into my pipeline script and run it before firebase deploy

The function: predeploy key in firebase.json or any manual command calling the script should do it as well. Depends on your setup.

Upvotes: 3

Related Questions