Deploying a ReasonML-compiled JavaScript module to Google Cloud Functions is much the same as deploying an EcmaScript module, so a good tutorial to understand the basics is https://cloud.google.com/functions/docs/tutorials/http#functions-update-install-gcloud-node8
The main thing you need to ensure is that Reason outputs a transpiled JavaScript module that looks like what GCF expects. I do foresee a problem using Webpack, however, because it will want to pack your local version of Express into the output JS and we don't want that–we want to use the Express that's provided by GCF.
To get around this problem, I would probably not use a JavaScript bundler and instead list bs-platform
in the package.json
dependencies
section so that it's installed during the function deploy, and express
in the peerDependencies
section so that the deploy uses the provided Express version.
As per the reference, you may name the module file however you like as long as you list the name in package.json
, with the line "main": "src/App.bs.js"
. In this example you may name your Reason source file src/App.re
.
The next step is to make sure that your function conforms to what an Express routing handler must look like. If you'll notice, the tutorial I linked to shows this function shape in JavaScript: exports.foo = (req, res) => ...;
. To compile a Reason function to this shape, we need an uncurried Reason function:
let helloworld2 = (. _req, res) => {
open Express;
res
|> Response.status(Response.StatusCode.Ok)
|> Response.sendString("Hello world!")
};
(The dot after the function parameter left parenthesis indicates that it's an uncurried function. Btw I also rearranged the function to a little more idiomatic Reason style.)
Finally, you deploy:
gcloud beta functions deploy helloworld2 --runtime nodejs8 --trigger-http
The helloworld2
tells the deploy command which function to run in the deployed module.