Fireship · @fireship

Use TypeScript with Firebase Cloud Functions

Uploaded August 10, 2017 · Length 4:12

Use TypeScript with Firebase Cloud Functions

Summary

Set up Firebase Cloud Functions with TypeScript and Webpack for easier debugging and maintenance.

  • firebase cloud functions
  • typescript
  • webpack
  • javascript
  • cloud functions

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    when we write firebase cloud functions

  2. 0:01

    we normally do it in plain JavaScript

  3. 0:03

    but this tends to make them hard to

  4. 0:05

    debug what if we could actually write

  5. 0:07

    them in typescript and also throw on web

  6. 0:09

    pack to make them a little more modular

  7. 0:11

    in this quick lesson I'm going to show

  8. 0:14

    you how to set up your cloud function

  9. 0:15

    environment for typescript and web pack

  10. 0:17

    and then we'll deploy a few cloud

  11. 0:19

    functions to firebase I first want to

  12. 0:22

    point out that there's an article on

  13. 0:23

    medium that was the main inspiration for

  14. 0:25

    this video so I'll add a link to that

  15. 0:27

    the description and if you're brand new

  16. 0:29

    to cloud functions I recommend checking

  17. 0:30

    out any of the blue videos on my channel

  18. 0:32

    which are specifically focused on

  19. 0:34

    firebase cloud functions so to get

  20. 0:36

    started I'm assuming you have an

  21. 0:37

    existing project that you're working on

  22. 0:39

    and you'll bring up the command line and

  23. 0:41

    run firebase and knit functions then

  24. 0:44

    select all the default options during

  25. 0:46

    that project setup this is going to

  26. 0:48

    create a new directory in the root of

  27. 0:50

    the project called functions and that's

  28. 0:52

    an isolated node environment where we

  29. 0:54

    can build our cloud functions so the

  30. 0:57

    next step is to CD into that directory

  31. 0:59

    and then we'll create a new directory

  32. 1:02

    called source and we'll put a file in

  33. 1:05

    there called index TS instead of index j

  34. 1:08

    s then we can go ahead and remove the

  35. 1:11

    index j s file for now web pack will

  36. 1:14

    actually generate the index.js file

  37. 1:16

    automatically based on our underlying

  38. 1:17

    typescript code and that's the file that

  39. 1:20

    actually gets deployed the firebase now

  40. 1:22

    let's open up the package JSON file then

  41. 1:26

    we can install typescript and web pack

  42. 1:27

    and we'll specify them only for that

  43. 1:30

    development environment so your final

  44. 1:32

    node environment should look something

  45. 1:34

    like this

  46. 1:34

    and we can add a TS config file nothing

  47. 1:38

    really special here you can go with the

  48. 1:40

    default options and customize it any way

  49. 1:42

    you want then the last step is to create

  50. 1:44

    the web pack config this will tell web

  51. 1:47

    pack how to compile our type script to a

  52. 1:49

    single index.js file that can be

  53. 1:51

    deployed to firebase check out the link

  54. 1:54

    in the description for more detail on

  55. 1:55

    this code so now we can actually start

  56. 1:57

    building cloud functions we can do this

  57. 1:59

    by creating directories inside the