How to use TypeScript references for Firebase Cloud Functions?

Viewed 163

My Firebase project is all TypeScript. I have a core lib that I use in order to share some of the code between my Cloud Functions and my Web App. Maintaining core as an npm package started to become really cumbersome because in order to make a small change in the code and test it in a deployment, I'd have to build the lib, bump versions, publish it, etc. For that reason I decided to migrate to TypeScript Project References.

My directory structure looks something like:

./functions
./webapp
./lib
  ./core

And then functions has a tsconfig.json file with:

"references": [
   {"path": "../lib/core"}
]

Building the functions project with tsc --build works, but the dependency on core in the built *.js function files ends up being:

const core_1 = require("../../../lib/core");

So, after firebase --deploy, firebase will complain:

⚠  functions[testFunction(us-central1)]: Deployment error.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.

I assume this is happening because of that require.

I've looked at using prepend: true on the references, but I don't know if this will be counterproductive, since my core project depends on firebase-functions and other firebase libs.

Is there a way to have tsc bring in the referenced projects into the structure of dist (the output directory)?

0 Answers
Related