'Firebase emulator: add build steps to hosting

I'm trying to programmatically build my index.html file from a template, using a custom script/pre-deploy hook.

Is it possible to have the Firebase emulator run my predeploy script either (1) on start, or (2) on page refresh, so that I can see in the emulator what will happen on actual deploy?

My script works in actual deployment, but never fires with the emulator.

firebase.json (relevant part)

"hosting": {
  "public": "public",
  "ignore": [
    "firebase.json",
    "**/.*",
    "**/node_modules/**"
  ],
  "predeploy": "node ./preDeploy.js",
  "rewrites": [
    {
      "source": "**",
      "destination": "/index.html"
    }
  ]
},

preDeploy.js (oversimplified minimal example)

const fs = require('fs');
const testString = 'hello';
fs.writeFileSync('./preDeployTest.txt', testString);

I would expect the test file to be generated (1), ideally every time I refresh my site while running the emulator, or (2) at least when I first start the emulator -- but the script only runs on actual deploy.

To be clear, I'd be using this script to generate the file that I want Firebase hosting to use as its index.html, but I want to be able to emulate the results.



Solution 1:[1]

This may be possible with the default lifecycle scripts block:

"scripts": {
    "prepare": "node ./prepare.js"
}

There's also preprepare and a few other hooks; you may have to toy with it to find the correct one. The point is, that one isn't limited to Firebase specific hooks, but can use those defined by NodeJS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Zeitler