A utility that quickly exports all TypeScript/JavaScript Firebase Functions from all of a directory's subdirectories.
- Install the package in your Firebase Functions directory (usually
/functions
):- With Yarn:
yarn add firebase-functions-exporter
- With NPM:
npm i --save firebase-functions-exporter
- With Yarn:
- Write your Firebase Functions in their own files, following the pattern
[NAME].function.ts
or[NAME].function.js
, grouped in directories of your choosing. For example:- TypeScript (with functions grouped by type):
functions src index.ts callable updateUser.function.ts createUser.function.ts restful posts.function.ts event onUserCreate.function.ts
functions/src/event/onUserCreate.function.ts
:import * as functions from 'firebase-functions'; export const onUserCreate = functions.auth.user().onCreate((user, context) => { functions.logger().log(`User created: ${JSON.stringify(user)}`); });
- JavaScript (with functions grouped by category):
functions index.js users createUser.function.js updateUser.function.js onUserCreate.function.js posts onPostUpdate.function.js
functions/posts/onPostUpdate.function.js
const functions = require('firebase-functions'); exports.onPostUpdate = functions.firestore.document('/post/{postId}') .onUpdate((snap, context) => { functions.logger.log(`Post Updated: ${JSON.stringify(snap)}`); })
- TypeScript (with functions grouped by type):
- Define
exportFunctions
as the export in the functions index file:- TypeScript:
functions/src/index.ts
:import { exportFunctions } from 'firebase-functions-exporter'; module.exports = exportFunctions();
- JavaScript:
functions/index.js
:const { exportFunctions } = require('firebase-functions-exporter'); module.exports = exportFunctions()
- TypeScript:
- Deploy to Firebase Functions:
firebase deploy --only functions
.
- If using default exports, the function will take the name of the file, i.e. the name of a default exported function in the file
onUserCreate.function.ts
will beonUserCreate
. - Deploying to Firebase Functions will fail if two or more functions have the same name.
MIT