functions-loader
is a utility package designed to automatically load Firebase Cloud Functions from files and folders within the functions
directory. This simplifies the process of organizing and deploying your cloud functions by dynamically importing them based on the file structure. It specifically loads files with the .f.js
extension.
To install functions-loader
, you can use npm or yarn:
To use functions-loader
, you need to require it in your index.js
or main.js
file where you initialize your Firebase functions. The package will automatically load all functions from the functions
directory that have the .f.js
extension.
-
Create your functions directory structure:
functions/ ├── index.js ├── helloWorld.f.js └── user/ └── createUser.f.js
-
Initialize
functions-loader
in yourindex.js
:// functions/index.js const { loadFunctions } = require("firebase-function-tools-preview"); // Load all functions from the 'functions' directory loadFunctions(__dirname, exports, true);
-
Define your cloud functions in separate files with the
.f.js
extension:// functions/helloWorld.f.js const functions = require("firebase-functions"); module.exports = { helloWorld: functions.https.onRequest((request, response) => { response.send("Hello, World!"); }), };
// functions/user/createUser.f.js const functions = require("firebase-functions"); module.exports = { createUser: functions.auth.user().onCreate((user) => { // Handle user creation }), };
You can find a complete example of how to use functions-loader
in the demo
folder of this repository. The demo includes a sample project structure and demonstrates how to set up and use the package to load Firebase Cloud Functions.
With this setup, functions-loader
will automatically import and register all the functions defined in the functions
directory and its subdirectories that have the .f.js
extension.
This project is licensed under the MIT License.