I'm just getting started with Cloud Functions. I really need to be able to build a bundle using jspm/Babel, from ES2016 sources (and hopefully will start using Typescript soon).
The process of deploying functions to the cloud is very weird.
I originally installed both firebase-admin and firebase-functions using jspm and tried to bundle them into my deployed executable. It looked something like this:
import functions from 'firebase-functions';
import admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
export let makeUppercase = functions.database.ref('/messages/{pushId}/original')
...
When I built that using jspm build --node and tried to firebase deploy --only functions, it became very clear that the deployment process really wants firebase-admin and firebase-functions to be installed with npm under functions/node_modules, and imported from there at runtime with require(). I don't remember what the error messages were, but I decided not to fight with it. My current code looks like this:
import * as functions from '@node/firebase-functions';
import * as admin from '@node/firebase-admin';
admin.default.initializeApp(functions.config().firebase);
export let makeUppercase = functions.database.ref('/messages/{pushId}/original')
...
This seems to get further in the deployment process, but now I get this output when deploying:
i deploying functions
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
+ runtimeconfig: all necessary APIs are enabled
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: There was an unknown problem while trying to parse function triggers. Please ensure you are using Node.js v6 or greater.
I'm using Node.js v6.9.3. The message is not very helpful.
I assume there's some kind of trickery going on, and the tricks played by the Babel-transpiled module are confusing the tricks played by the deployer. Instead of having an exports.makeUpperCase = statement at the top level of the file, it has a bunch of unreadable SystemJS boilerplate, and somewhere in a callback it does:
admin__default.initializeApp(config().firebase);
_export('makeUppercase', makeUppercase = database.ref('/messages/{pushId}/original')
where _export is a function that's defined elsewhere and presumably does something to assign to the module-level exports object. Other than that I'm not sure what's going on.
Next I'm planning to try making a generic JS Node module that requires the two libraries and exports the functions, but imports the implementations from a transpiled bundle. I don't know of any reason why that shouldn't work, but then, I don't know of any reason why my previous attempts didn't work either.
Has anyone succeeded in setting up an environment in which they can write and debug Firebase Cloud Functions in modern ES2016 or Typescript? (And preferably also be able to share some of that code with client-side app code and with traditional backend server code.) I would think Firebase would want to support that, given that a lot of their code seems to be written in Typescript.
I'm not necessarily going to insist on continuing to use jspm, but I don't know what to switch to if I don't.