Git Product home page Git Product logo

nx-firebase's Introduction

@simondotm/nx-firebase actions nx npm downloads

A plugin for Nx v16.8.1+ that integrates Firebase workflows in an Nx monorepo workspace.

  • Easily generate Firebase applications and functions
  • Uses esbuild for fast Firebase function builds so you can easily create & import shared Nx libraries with the benefits of tree-shaking
  • Supports function environment variables and secrets
  • Supports single or multiple firebase projects/apps within an Nx workspace
  • Full support for the Firebase Emulator suite for local development, with watch mode for functions
  • Keeps your firebase.json configurations in sync when renaming or deleting Firebase apps & functions
  • Only very lightly opinionated about your Firebase configurations and workspace layouts; you can use Nx or the Firebase CLI

See CHANGELOG for release notes.

Install Plugin

npm install @simondotm/nx-firebase --save-dev

  • Installs this plugin into your Nx workspace
  • This will also install @nx/node and firebase SDK's to your root workspace package.json if they are not already installed

Generate Firebase Application

nx g @simondotm/nx-firebase:app my-new-firebase-app [--directory=dir] [--project=proj]

  • Generates a new Nx Firebase application project in the workspace
  • The app generator will also create a Firebase configuration file in the root of your workspace (along with a default .firebaserc and firebase.json if they don't already exist)
  • For the first firebase application you create, the project firebase configuration will be firebase.json
  • If you create additional firebase applications, the project firebase configuration will be firebase.<app-project-name>.json
  • Use --project to link your Firebase App to a Firebase project name in your .firebaserc file

Generate Firebase Function

nx g @simondotm/nx-firebase:function my-new-firebase-function --app=my-new-firebase-app [--directory=dir]

  • Generates a new Nx Firebase function application project in the workspace
  • Firebase Function projects must be linked to a Firebase application project with the --app option
  • Firebase Function projects can contain one or more firebase functions
  • You can generate as many Firebase Function projects as you need for your application

Build

nx build my-new-firebase-app

  • Compiles & builds all Firebase function applications linked to the Nx Firebase application or an individual function

nx build my-new-firebase-function

  • Compiles & builds an individual function

Serve

nx serve my-new-firebase-app

  • Builds & Watches all Firebase functions apps linked to the Firebase application
  • Starts the Firebase emulators

Deploy

Firebase Application

nx deploy my-new-firebase-app [--only ...]

  • By default, deploys ALL of your cloud resources associated with your Firebase application (eg. sites, functions, database rules etc.)
  • Use the --only option to selectively deploy (same as Firebase CLI)

For initial deployment:

  • firebase login if not already authenticated
  • firebase use --add to add your Firebase Project(s) to the .firebaserc file in your workspace. This step must be completed before you can deploy anything to Firebase.

Note that you can also use the firebase CLI directly if you prefer:

  • firebase deploy --config=firebase.<appname>.json --only functions

Firebase Function

nx deploy my-new-firebase-function

  • Deploys only a specific Firebase function

Test

nx test my-new-firebase-app

  • Runs unit tests for all Firebase functions apps linked to the Firebase application

nx test my-new-firebase-function

  • Runs unit tests for an individual function

Lint

nx lint my-new-firebase-app

  • Runs linter for all Firebase functions apps linked to the Firebase application or an individual function

nx lint my-new-firebase-function

  • Runs linter for an individual function

Sync Workspace

nx g @simondotm/nx-firebase:sync

  • Ensures that your firebase.json configurations are kept up to date with your workspace
    • If you rename or move firebase application or firebase function projects
    • If you delete firebase function projects

Further Information

See the full plugin User Guide for more details.

nx-firebase's People

Contributors

braunreuthera avatar dependabot[bot] avatar jaytavares avatar lucastnr avatar mcarriere avatar rosslavery avatar shipley-dcc avatar sibirius avatar simondotm avatar whatsthatitspat avatar ykrx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nx-firebase's Issues

Build command is broken with @nrwl/workspace >= 13.10.0

When running the build command through NX when @nrwl/workspace is installed with a version of 13.10.0 or greater, the following error occurs:

assets_1.copyAssetFiles is not a function

It seems as though the copyAssetFiles function was removed from @nrwl/workspace/src/utilities/assets in 13.10.0, looking at this commit. I'm not sure if this is an intentional change on their part; I can't seem to find any notes on it.

Tested with both 13.10.0 and 13.10.1 (the latest at time of writing). This doesn't appear to be addressed as part of #45, so I figured I would open a separate issue for this.

Support nx migrations?

New Nx releases are frequent, and also Nxfirebase plugin changes may require updates of previous installations (such as modifying/updating nxfirebase app targets etc.)
Look into the Nx migration process for this.

v15 rewrite issues

I've rewritten the plugin from scratch to support Nx 13.10.0 to 15.3.0 (#59).

Things left to do here:

  • Check that firebase deploy works with the latest firebase SDK
  • Check that @nrwl/js:tsc executor is going to work for this plugin. After e2e testing I have doubts on this
  • Check that the targets created for application generator are all correct
  • Check that serve works
  • Update .gitignore
  • Clean up run-commands in project config
  • Support runtime property in firebase config
  • Support codebase property in firebase config
  • Support functions array in firebase config
  • Support clean emulator port closure for serve target, given that Nx does not terminate emulator properly
  • #75
  • #72
  • Support firebase.json as the default configuration for the first firebase app created in the workspace
  • #67
  • #69
  • #83
  • Check that the app generator observes workspace layout settings in nx.json
  • #81
  • #86

There are other new features that can be supported but I'd like to release an initial new version first that at least supports the same functionality as the older version of the plugin.

Support `--function` as a generator option

Syntax will be nx g @simondotm/nx-firebase:function <name> --project=<projectname> [--alias=<projectname>] [--directory=<dir>]
It will generate an application which is:

  • based on @nrwl/node:webpack, so that the code is fully
  • linked to project <projectname>
  • added to the firebase.json config file functions array for that project
  • assigned a codebase with the <projectname>
  • it will be separately buildable and deployable
  • it will have its own deploy target, which will deploy only this codebase

function applications will only work with Firebase CLI v10.7.1 and above.

Copy any `.runtimeconfig` file in the app folder to `dist` when building

When running firebase emulator, functions that use the runtime config need to have the .runtimeconfig file in the dist/apps/functions directory

build target executor should check for this file existing in the app folder and copy it to dist if it exists.

users can call firebase functions:config:get > .runtimeconfig.json locally

`.runtimeconfig.json` does not get copied on incremental builds

Since .runtimeconfig.json is added to .gitignore, nx does not class it as a dependency, so even though it is listed as an asset for the firebase app, incremental builds will not detect any changes to this file and therefore re-run the build target and copy the asset.

if .runtimeconfig.json is removed from .gitignore it will be added to node_modules/.cache/nx/nxdeps.json as a dependency

but we cant have it as a non-ignored file, since it might get checked into version control.
can't put it into dist either, because that folder gets cleaned prior to build.

possibly, emulate may need to have a pre-step that runs getconfig target for every emulation session.

tricky one this.

New npm release?

It seems like since v0.3.3, nx-firebase now supports nx v13.

Could a maintainer please publish this to npm?

Support -P option in deploy targets

Multi-project workspaces need extra care when using deploy without switching to the correct firebase project target first (firebase use <target> or firebase deploy -P <target>
Worst case scenario is that the wrong things might be deployed to the wrong target.

We can improve this by adding -P to the deploy target.

Either document suggestion to add -P manually, or support some way to set the firebase target on an apps' deploy target

Support `npm ci` with a `package-lock.json`

We don't currently npm install prior to deployment so there is no package-lock.json generated in the dist folder.

It's not ideal to run npm install prior to every build/deploy, but the package-lock.json would be useful for the following reason (comment from another repo)

From my understanding, when deploying to Cloud Functions, Cloud Build installs your dependencies with npm ci, but your apps/api directory doesn't have a package-lock.json file, so your dependencies are installed in a non-deterministic way, which isn't desirable. If any dependency (e.g. firebase-functions) were introduced a bug in a minor release, this bug would suddenly appear on production.

appname:emulate not working

I like this library and I really plan to use it for future projects, but now I have a problem with the commands.

nx emulate and nx build
Every time I try to use it I get
Screen Shot 2022-06-27 at 14 02 28

Screen Shot 2022-06-27 at 14 10 02

and then, I decided to change the json and only leave the emulators that I want to use and also remove the other commands and it works, but I get that the functions are not loaded

Screen Shot 2022-06-27 at 14 03 14

Screen Shot 2022-06-27 at 14 03 59

and I don't know if in some step I skip any usage requirement because I still tried

Firebase build executor should check that dependent libraries are buildable

In the scenario where a non-buildable node library has been created in the workspace, and it is referenced by a firebase functions application, the builder should check this dependent library is buildable, because if it isn't we will not be able to proceed (since we cannot copy the compiled JS lib to the dist/ folder, since it isn't buildable)

Firebase Functions deployment issue

Not a problem with this package, but I wanted to surface it to others who may run into deployment errors since it affects mono-repos in general.

[debug] [2022-09-05T11:21:08.962Z] Building nodejs source
[debug] [2022-09-05T11:21:08.963Z] Could not find functions.yaml. Must use http discovery
[debug] [2022-09-05T11:21:08.972Z] Error: spawn ./node_modules/.bin/firebase-functions ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:476:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
[error]
[error] Error: An unexpected error has occurred.
[debug] [2022-09-05T11:21:09.063Z] Serving at port 9005

When deploying after upgrading firebase tools, I ran into this issue. I was able to fix deployments by reverting the firebase-tools version.

Build with --watch flag

Hello. Great plugin.
Is there a way to run the builder in watch mode for hot reloading purpose?

I have tried
nx build myapp --watch=true

It starts compilation in watch mode but it exits after first execution.

Failed to upload functions - checkFunctionsSDKVersion encountered error:Invalid Version: {}

Hi so I've run into an issue, it's my first time using this package but when I try to deploy none of my functions were getting deployed. It would fail somewhat silently but I finally noticed this error. I am using shared modules in here but not sure what caused it exactly.

checkFunctionsSDKVersion encountered error:Invalid Version: {}
Could not parse firebase-functions version '' into semver. Falling back to parseTriggers.

The fix was running (yarn in my case) install in the distribution directory, as it seems to actually be looking for the firebase-functions library in dist/app/[app_name]/node_modules not just the version set in the package.json.

Updating my pre-deploy script to
"npx nx build enginebay --with-deps && cd dist/apps/[APP_NAME] && yarn install"

Seemed to fix it if anyone else runs into it.

firebase_functions 4.0.1
firebase cli 11.15.0
nx 15.0.0

package.json build issue for shared lib

Not sure if this is a problem on Firebase side or some nx config, but after being able to deploy functions with a shared library I'm now gettings this error message in the Firebase logs:

Build failed: npm ERR! cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with npm install before continuing

Collaborators requested

Hi all. Apologies for the radio silence, lets just say that last year was a rough ride for me and I had to be away from development for a while.

I'm now easing back into things, but first I'd like to delegate ownership of this plugin to the community (something I should have done a while ago, but I didn't realise it was being used by so many folks).

As a start, I've invited @BraunreutherA - author of the recent PR for nx13 compatibility- to be a collaborator on this repo which I hope will allow you all to make progress with a new release of this plugin.

If anyone else would like to be a contributor to this project or you would like to recommend a contributor, please make suggestions below and I will do my best to provide that permission.

I believe everything is setup on github actions for release to npm, I seem to remember it just needs a version tag on master to trigger a publish.

Lastly, I am hoping to continue some some of my own Firebase dev soon and will join you all back in the fray then.

Best,
Simon

Rename package to `nx-firebase`

Bit of a pain to do this since already published on npm, but renaming to nx-firebase would better follow community plugin conventions

Emulator shutdowns are not "clean" when using executors

After using firebase emulators:start in the terminal and entering ctrl-c to end the process, I get lengthy output telling me about the shutdown process:

i  emulators: Received SIGINT (Ctrl-C) for the first time. Starting a clean shutdown.
i  emulators: Please wait for a clean shutdown or send the SIGINT (Ctrl-C) signal again to stop right now.

# ...and another dozen or more lines about shutting down the various emulators and exporting data

But after running nx serve functions or nx emulate functions, ending the process with ctrl-c doesn't show any of the output and I don't think everything is being shut down properly.

When I restart the emulators I get warnings about "multiple instances" or specific ports that are still in use:

emulators: It seems that you are running multiple instances of the emulator suite for project demo-local. This may result in unexpected behavior.

# or

firestore: Port 8080 is not open on localhost, could not start Firestore Emulator.

# or

ui: Emulator UI unable to start on port 4000, starting on 4001 instead.

Additionally, when I try to import & export emulator data, the data isn't exported to the correct place and extra firebase-export-{something} directories are created.

I've adjusted the command in the emulate executor to look like this:

firebase emulators:start --project=demo-local --import=./seed/demo-local --export-on-exit

When I run this directly in the terminal, everything shuts down fine and works as expected.

I'm on macOS, my firebase tools version is 9.16.6, and node is 16.8.0.

Firebase Executor for emulator suite

Hi, awesome plugin, been using it a bit for experimenting with sharing code between firebase functions and angular apps.

Wanted to know if the firebase executor can be documented or configured to support emulators parameters.

Right now I am doing something like below at angular.json for serve the CF locally with emulators.

{

        "serve": {
          "builder": "@nrwl/workspace:run-commands",
          "options": {
            "commands": [
              {
                "command": "nx run appName:build"
              },
              {
                "command": "firebase use projectIdOrAlias"
              },
              {
                "command": "firebase emulators:start --config firebase.appName.json"
              }
            ],
            "parallel": false
          }
        }
}

Cheers!

Generate nx command for executing tests with emulators running

Possible enhancement for nx command in the workspace.json that runs the test command within the firebase emulators:exec command - similar to below?

"testWithEmulatorsRunning": {
    "executor": "@nrwl/workspace:run-commands",
    "options": {
        "command": "nx functions-app:build --with-deps && firebase emulators:exec --project=test 'nx run functions-app:test' --config firebase.functions-app.json"
    }
}

Replace use of `join`

Some parts of plugin use join - replace with joinPathFragments to ensure full compatibility

How to use .env files?

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?

Missing dependency for @angular-devkit/schematics

I added nx-firebase to an existing Nx repo with a react app in it. When trying to run the build command for the firebase app it fails with the error:

Cannot find module '@angular-devkit/schematics'

If I manually add the package to my projects dev-dependencies the build script works.

Nx 13.0.1 release breaks nx-firebase use of createProjectGraph()

In recent release of Nx 13.0.1, createProjectGraph() is no longer a public API.

As a result the build executor fails for Firebase functions:

nx run functions:build
project_graph_1.createProjectGraph is not a function

Looks like createProjectGraphAsync() is still available. There may be more changes needed as well.

Any advice on working around this with Nx 13.0.1 is much appreciated.

Add CHANGELOG to repo

Nx are moving fast with releases and bug fixes.
Since new functionality in new versions of plugin will only affect newly generated nxfirebase apps, we will need to add release notes/changelog to explain any firebase configuration updates that may need to be manually updated by existing users.

build executor may need to respect node version

I've come up against this possibility with Firebase + Nx, where Nx workspace/user environment uses Node version 15 (for example) but our Firebase functions package engine is set to 10.
For CI builds of the functions, we need to be confident that building functions via nx using a node version other than the one specified in the functions package.json will generate output JS that is compatible with firebase functions when deployed.

Cannot find module '@nrwl/workspace/src/utilities/assets'

This plugin looks very promising. I followed the quick steps and ran into

> nx run functions:build 
Cannot find module '@nrwl/workspace/src/utilities/assets'
Require stack:
- {PROJECT_ROOT}/node_modules/@simondotm/nxfirebase/src/executors/build/build.js
- {PROJECT_ROOT}/node_modules/@nrwl/tao/src/shared/workspace.js
- {PROJECT_ROOT}/node_modules/@nrwl/tao/src/commands/run.js
- {PROJECT_ROOT}/node_modules/@nrwl/tao/index.js
- {PROJECT_ROOT}/node_modules/@nrwl/cli/lib/run-cli.js

Am using nx 12.0.8 and v0.2.2 of your plugin.

I figured out that your plugin at least needs nx 12.3.x, so after upgrading all worked fine. Maybe its worth mentioning the Nx version or maybe there's a way to disallow using it before nx version 12.3?

I also had to add import "firebase-admin"; to your example "Hello Logs" function, otherwise the npm package did not get written into the functions package.json which results in an error during the function load: Detailed stack trace: Error: Cannot find module 'firebase-admin'

--watch seems to have no effect

This lib is great!

I have created an app named functions.

nx run functions:build --watch

Is building but finishing up without watching. Am I doing something wrong? The functions:emulate behaves the same. It builds fine and launches the emulator suite without issue but it doesn't watch for changes.

Thanks

Failed to deploy because of missing dependencies

I have a package.json in the apps/myapp repo.

I had to add these dependencies there into the empty dependency section:

  "dependencies": {
    // this was empty before
    "axios": "^0.27.2",
    "countries-list": "^2.6.1",
    "randomstring": "^1.2.2"
    // ... <- here go most(!) of the dependencies I installed in the top level, but not all.
  },

All of these dependencies are already specified in the top level package.json.

Is this expected behavior or a bug? This previously worked (I was using a patched nx-firebase package though).

Some dependencies are copied into the dist/myapp/package.json file properly, for example "klaviyo-api", which is definitely not on some sort of whitelist.

Make initial application generator use `firebase.json`

Its not obvious that the empty firebase.json config is just a dummy file to keep the Firebase CLI from moaning.
Perhaps a better solution is for the nxfirebase app generator to create firebase.json for the first firebase application in the workspace, since its more intuitive this way.
Any subsequent nxfirebase app generators should then generate the named firebase.appname.json configs.
Users can rename the original firebase.json if they like.

Firebase Function deploy failed on loading user code due to Node12

Hi,

After creating an Nx workspace, and using Create Firebase Application to link with my existing firebase app, I encountered a few errors.

First I fixed with the patch in ##44
But then on my function deploys I had the following error:

Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

GCP journal logs:

SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/workspace/node_modules/firebase-admin/lib/default-namespace.js:19:30)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

The logs were not very descriptive but I figured out it was probably due to the node12 engine not being supported anymore ny latest versions.

The fix was to replace the engine version in my generated package.json

  "engines": {
    "node": "12"
  },

  "engines": {
    "node": "16"
  },

As Nx now generates workspaces with node 16^, I figured the generation script at @simondotm\nx-firebase\src\generators\application\files\package.json__template__ could also use a newer version

PS: great work thanks for the pluggin!

Match plugin version to Nx Version?

Just leaving this here as an idea. Might be useful to present the plugin (once stable) with versioning to match the Nx version it was built against.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.