如何解决无法从导出的Firebase函数中引用未导出的Firebase函数
最近,我构造了Firebase函数,以便通过引用another SO post来使目录具有更模块化的方法。
因此,firebase目录结构为
index.js
|
modules/
├── enterprise
│ ├── getCompanyLicenseUsers.js
│ ├── index.js
│ └── revokeLicense.js
├── premium
│ ├── applyLicense.js
│ ├── getCouponDiscount.js
│ ├── index.js
│ ├── processPaypalPayment.js
│ └── unlockPremium.js
└── utils
├── index.js
└── utilityFunctions.js
不同模块(除了utils 以外的所有其他文件)中的所有文件均作为最高级index.js中的firebase函数导出。每个模块的index.js会全局导出到每个函数。
示例功能 getCompanyLicenseUsers
require('../utils');
const functions = require("firebase-functions");
module.exports.getCompanyLicenseUsers = () => {
return functions.https.onCall((data,context) => {
// function code
})
}
该功能所属的模块的index.js
global.getCompanyLicenseUsers = require('./getCompanyLicenseUsers').getCompanyLicenseUsers
global.revokeLicense = require('./revokeLicense').revokeLicense
主要(最外面) index.js
// Module imports
require('./modules/enterprise')
require('./modules/notifications')
require('./modules/premium')
require('./modules/jobs')
// exporting firebase functions
exports.getCompanyLicenseUsers = getCompanyLicenseUsers();
exports.revokeLicense = revokeLicense();
exports.applyLicense = applyLicense();
exports.getCouponDiscount = getCouponDiscount();
exports.processPaypalPayment = processPaypalPayment();
exports.unlockPremium = unlockPremium();
现在,我想添加另一个模块,但是不需要导出该模块的所有功能。这些未导出的函数将导入到将从该模块导出的函数中。
我能够成功部署该功能,但是每当在Firebase上执行此导出的功能时,在导出的功能内部被调用但未显式导出到firebase的功能存在参考错误(但属于一部分导出功能的模块)。
我想我可以将这些单独的功能作为自己的模块来使用,但是我不想这样做,因为这样一来,代码看起来会显得过于井井有条。
感谢您甚至阅读了整个问题。任何帮助都非常感激
解决方法
我的代码库中有将近30多个函数,并且在一个项目中进行了组织(还改进了冷启动性能)。
这是最简化的形式https://stackoverflow.com/a/47361132/1212903
请参考(尽管它使用打字稿,但它将使您更好地处理它) https://medium.com/firebase-developers/organize-cloud-functions-for-max-cold-start-performance-and-readability-with-typescript-and-9261ee8450f0