unable to split Firebase functions in multiple files

前端 未结 4 1892
名媛妹妹
名媛妹妹 2021-02-05 10:17

I\'m working with firebase functions and arrived to hundreds of functions, and now it is very hard to manage it in single index.js file as shown in their lots of ex

相关标签:
4条回答
  • 2021-02-05 10:39

    Working code example:

    file structure:

    --firebase.json
    --functions
      --node_modules
      --index.js
      --package.json
      --src
        --groupFunctions.js
        --authFunctions.js
        --storageFunctions.js
    

    index.js file:

    require('./src/groupFunctions.js')(exports);
    require('./src/authFunctions.js')(exports);
    require('./src/storageFunctions.js')(exports);
    

    groupFunctions.js file:

    var functions = require('firebase-functions');
    
    module.exports = function (e) {
        e.onGroupCreate = functions.database.ref('/groups/{groupId}')
            .onWrite(event => {
                console.log(`A group is created in database named:${event.params.groupId}.`);
                // some logic...
                //...
            })
    }
    

    UPDATE: now I have better solution

    The full working code is located at https://github.com/malikasinger1/firebase-functions-with-typescript and it's written with cutting edge tech like typescript and webpack. You may use this as a boilerplate/starter.

    0 讨论(0)
  • 2021-02-05 10:49

    If you want support for multiple functions inside a file, this Gist show a real example of how to do it

    https://gist.github.com/saintplay/3f965e0aea933a1129cc2c9a823e74d7#file-index-js

    You can dynamically import functions for every *.function.js

    0 讨论(0)
  • 2021-02-05 11:02

    You can pass the exports object to the function in groupFunctions.js, like this:

    var functions = require('firebase-functions');
    
    module.exports = function (e) {
        e.onGroupCreate = functions.database.ref('/groups/{groupId}')
            .onWrite(event => {
                console.log(`A group is created in database named:${event.params.groupId}.`);
                // some logic...
                //...
            })
    }
    

    Now, in index.js:

    var functions = require('firebase-functions');
    
    require("./app/groupFunctions")(module.exports);
    

    The way it works is that modules.exports is a regular JavaScript object, so you can add new properties to that from wherever you want.

    0 讨论(0)
  • 2021-02-05 11:05
    //index.js
    const glob = require('glob')
    const files = glob.sync('./**/*.functions.js', { cwd: __dirname, 
    ignore: './node_modules/**' })
    
    files.forEach(file => {
    const functionModule = require(file)
    const functionNames = Object.keys(functionModule)
    
    functionNames.forEach(functionName => {
        if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME === 
        functionName) {
             exports[functionName] = functionModule[functionName]
         }
      })
     })
    

    Folders like so.. will work

     //Example Home.functions.js :
     exports.Home = functions.https..
    
    0 讨论(0)
提交回复
热议问题