You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Use TypeScript and WebPack With Firebase Cloud Functions
Episode 38 written by Jeff DelaneyFirebase Cloud Functions provide an incredible way to build and deploy microservices and background tasks. But there’s one major annoyance for Angular develpoers… They are written in plain JavaScript. Fortunately, we can fix this problem by modifying our functions environment to run on TypeScript and WebPack - just like most Angular 2/4 apps.
If you have a complex set of cloud functions managing your backend, I highly recommend using TypeScript for debugging and Webpack for modularity. Cloud Functions are already difficult to debug, so this will just make your life easier. Hopefully this will help you avoid deploying broken functions, then sifting through the logs to find the problem. The main benefits of this lesson include:
- Improved Firebase Cloud Function debugging
- Reliable strong-typed server-side JS code
- Modular file structure with Webpack
Starting Cloud Functions from Scratch
I am going to assume you have never used cloud functions before and that you have an existing Angular project (although it doesn’t have to be Angular).
Initialize Functions
firebase init functions |
This creates a functions directory with a default NodeJS environment.
Updating the Node Environment for TypeScript
We need to reorganize the functions directory to make it modular. In this example, we have a root index.ts file that serves as a manifest of all functions we want deployed. Below that, we have subdirectories for each function type, such as email or image functions.
Replace index.js with src/index.js
Create a new directory /src
inside the functions environment. Then create a new index.ts
file there.
mkdir src |
The index.js
that was removed will now be generated automatically with Webpack. This is the file that ultimately gets deployed to Firebase, but you should never need to alter it.
Update package.json
Now we need to install the ts-loader and webpack. We can do this with the following command (you may need to prefix with sudo
).
npm install --save-dev ts-loader webpack-node-externals |
Our final package.json should look something like this:
{ |
Configure TypeScript
First, we need to create a tsconfig.json file to configure TypeScript settings.
tsconfig.json
{ |
Configure Webpack
Now we need to tell Webpack how to piece together this code into in a single index.js file that can be deployed to Firebase. This code was taken from this helpful medium article discussing the process.
webpack.config.js
'use strict'; |
Adding functions
The goal is to keep our functions in a logical format. You can create a new directory for each function, or organize them by a common role. In this example we are doing the latter.
Organize Functions by Role
mkdir src/images |
At this point our functions directory should look like this:
functions/ |
Creating a Couple Functions
Here we are creating three different empty functions. First, we add one to the email index.
import * as functions from 'firebase-functions' |
Then add a couple more to the images index.
import * as functions from 'firebase-functions' |
Import Functions in src/index.ts
Now we need to add our functions to the main index.ts manifest.
import * as functions from 'firebase-functions' |
Deploying Functions
You can compile the TS code into a single index.js file by running
webpack |
Then deploy the functions to Firebase
firebase deploy --only functions |
That’s it for TypeScript/Webpack powered Firebase Cloud Functions. Let me know what you think in the comments.