simondotm / nx-firebase

Firebase plugin for Nx Monorepos
https://www.npmjs.com/package/@simondotm/nx-firebase
MIT License
175 stars 31 forks source link

How to use .env files? #55

Closed jimlloyd closed 10 months ago

jimlloyd commented 1 year ago

I have been doing all of my development with a single development Firebase project and am now trying to use multiple Firebase projects. Firebase best practices recommends four types of projects: dev, test, staging, and prod. I am the sole developer for my application so I can probably get away for now with two projects, call them dev and alpha, where I am treating alpha as a prod project that I will likely delete when I get closer to launching my product to the world.

So lets's say I have two aliases: dev and alpha pointing to two projects. I assume my .firebaserc file should probably look like this:

{
  "projects": {
    "dev": "my-app-dev",
    "alpha": "my-app-alpha"
  },
  "targets": {}
}

Firebase supports .env files as described here: https://firebase.google.com/docs/functions/config-env

So I should have three files .env, .env.dev and .env.alpha. But where should these files be placed in an nx-firebase project tree?

I currently have provided them in the two obvious places: The root of the project, where the firebase.json and firebase.functions.json are located, and inside the apps/functions directory. The Firebase doc above claims that I should see a message when deploying:

$ firebase use prod
$ firebase deploy --only functions
i functions: Loaded environment variables from .env, .env.prod.

I don't see the Loaded environment variables message when I deploy. It might be doing the right thing anyway, because my deployed dev and alpha projects are partly functional. Maybe the message is no longer logged?

My deployed apps are now usable with Anonymous but not Google authentication -- I'll debug that separately.

While researching all of this I was reminded that Firebase supports local "demo" projects which are purely local, served by the emulators. These demo projects cannot be added to .firebaserc and used via firebase use <alias>, bit require that the --project demo-test flag be passed to all firebase CLI command. Does nx-firebase provide any ways to make demo projects usable with the --project option injected automatically?

wnuczek commented 1 year ago

Hi, I've just managed to get it to work. The only thing required is to copy .env files to dist/app-name directory.

You can do this by modifying predeploy section of firebase.app-name.json as shown below.

...
"functions": {
    "predeploy": [
      "npx nx build app-name --with-deps",
      "npx nx lint app-name",
      "cp apps/app-name/src/.env dist/apps/app-name/.env",
      "cp apps/app-name/src/.env.test dist/apps/app-name/.env.test",
      "cp apps/app-name/src/.env.prod dist/apps/app-name/.env.prod"
    ],
    "source": "dist/apps/app-name"
  },
...
jimlloyd commented 1 year ago

Thanks @wnuczek! In hindsight that should have occurred to me.

simondotm commented 10 months ago

Also fixed in PR #137