Thursday, August 4, 2022

Firebase Functions emulator: exclude additional directories from change-detection watcher

Because the functions directory of your Firebase project already contains all dependencies that would be available in your server-side environment, you may be tempted to write your PoC/draft code snippets inside functions/ itself (maybe to avoid having to install/set-up a separate set-up just for PoCs, and also to automatically keep the PoC code in sync with any dependency/version changes made your main project) - possibly under clear-cut subdirectories that would be excluded from the deployment (e.g. my.test.js/, to automatically match an existing exclude like *.test.js).

One downside of this approach is that, every time you make a change in your PoC code, it will also reload the actual functions code in the emulator - because the functions change-detector is watching all subdirs of functions/. It seems that .gitignore, .gcloudignore etc are not honored by the emulator's change-detector process; so even if you have your PoC folder excluded, the emulator problem persists.

To solve this, you can patch lib/emulator/functionsEmulator.js from the firebase-tools installation directory (usu. {global NodeJS installation/lib dir}/node_modules/firebase-tools/) to add your custom dir (or a more generic matching pattern) to the watcher's excludes:

class FunctionsEmulator {
...
    async connect() {
...
        for (const backend of this.args.emulatableBackends) {
            this.logger.logLabeled("BULLET", "functions", `Watching "${backend.functionsDir}" for Cloud Functions...`);
            const watcher = chokidar.watch(backend.functionsDir, {
// this appears around line 228, on Emulator v11.1.0
                ignored: [
                    /.+?[\\\/]node_modules[\\\/].+?/,
                    /(^|[\/\\])\../,
                    /.+\.log/,
// new entry covering custom dir
                    /.+\.test\.js/,
                ],
                persistent: true,
            });

No comments:

Post a Comment

Thanks for commenting! But if you sound spammy, I will hunt you down, kill you, and dance on your grave!