/firebase-functions-typescript-starter

Create & test Firebase Cloud Functions in TypeScript

Primary LanguageTypeScriptMIT LicenseMIT

Firebase Functions TypeScript starter

Create & test Firebase Cloud Functions in TypeScript.

This starter allows you to create & test Firebase Cloud Functions in TypeScript.

Get the Changelog.

Contents

1 Project structure

  • functions:
    • src folder for the Functions
      • index.ts entry point for all your Firebase Functions
    • tests folder for the Mocha tests
    • package.json npm options
    • rollup.config.js Rollup configuration for building the cjs bundle
    • tsconfig.json tsc compiler options
    • tsconfig-build.json tsc compiler options for production
  • .firebaserc: Firebase projects

2 Customizing

  1. Update Firebase CLI.

  2. Update .firebaserc with your project-id.

  3. Add your Firebase Functions to index.ts, and create different files for each one.

  4. Update in rollup.config.js file external dependencies with those that actually you use to build the cjs bundle.

  5. Create unit tests in tests folder.

3 Testing

The following command runs unit tests using Mocha that are in the tests folder:

npm test 

4 Building

Development

The following command:

npm run serve:dev
  • starts tsc compiler with watch option
  • starts the emulator firebase emulators:start --only functions

For the other supported emulators, please refer to the official documentation: Run Functions Locally

Production

The following command:

npm run build
  • creates lib folder with the file of distribution:
└── functions
    └──lib
        └── index.js

5 Publishing

npm run deploy

6 What it is important to know

  1. commonjs modules

    NodeJs used by Firebase Cloud Functions only supports commonjs modules: so the tsconfig.js used by tsc compiler targets es2015 with commonjs modules

  2. Bundling with Rollup

    Firebase Cloud Functions does not require the deployment of a single bundle. In any case the building with Rollup offers some advantages:

    • Tree shaking of unused code
    • No request for other files at runtime

    Since Rollup requires the es2015 modules, tsc compilation is first performed using tsconfig-build.js with es2015 modules.

License

MIT