Firebase 如何为拆分为单个文件的云函数延迟加载依赖项?

Firebase 如何为拆分为单个文件的云函数延迟加载依赖项?,firebase,google-cloud-functions,Firebase,Google Cloud Functions,我正在尝试改进firebase cloud函数的冷启动时间。正如firebase团队所建议的,可能不需要的依赖项可以放在函数本身中,以便仅在需要时加载它。但这与我已经将函数拆分为单个文件的情况有什么关系呢?我在那里导入的依赖项是否只有在调用函数时才会自动加载?这正是我要解决的问题 基本策略是在函数本身内部对TypeScript或JavaScript require使用异步导入,以便将每个函数脚本文件的加载推迟到执行。这可以防止为所有函数加载一个函数的依赖项 以下是TypeScript中的两个函数

我正在尝试改进firebase cloud函数的冷启动时间。正如firebase团队所建议的,可能不需要的依赖项可以放在函数本身中,以便仅在需要时加载它。但这与我已经将函数拆分为单个文件的情况有什么关系呢?我在那里导入的依赖项是否只有在调用函数时才会自动加载?

这正是我要解决的问题

基本策略是在函数本身内部对TypeScript或JavaScript require使用异步导入,以便将每个函数脚本文件的加载推迟到执行。这可以防止为所有函数加载一个函数的依赖项

以下是TypeScript中的两个函数:

import * as functions from 'firebase-functions'

export const httpFn =
functions.https.onRequest(async (request, response) => {
    // Move the implementation of this function to fn/httFn.ts
    await (await import('./fn/httpFn')).default(request, response)
})

export const firestoreFn =
functions.firestore.document("users/{id}")
.onCreate(async (snapshot, context) => {
    // Move the implementation of this function to fn/firestoreFn.ts
    await (await import('./fn/firestoreFn')).default(snapshot, context)
})
然后,对于HTTP函数,fn/httpFn.ts中的实现如下所示:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()

// Note: Need to be explicit about parameter types here for HTTP
// type functions, to support type safety and IDE assistance.
export default async (
    request: functions.https.Request,
    response: functions.Response
) => {
    // Here we can use the admin SDK to get a document
    const snapshot = await admin.firestore()
        .collection('users')
        .doc('uid')
        .get()
    const data = snapshot.data()
    response.send(data)
}

在JavaScript中,策略是相同的。您只需要使用require而不是wait import。

这正是我要解决的问题

基本策略是在函数本身内部对TypeScript或JavaScript require使用异步导入,以便将每个函数脚本文件的加载推迟到执行。这可以防止为所有函数加载一个函数的依赖项

以下是TypeScript中的两个函数:

import * as functions from 'firebase-functions'

export const httpFn =
functions.https.onRequest(async (request, response) => {
    // Move the implementation of this function to fn/httFn.ts
    await (await import('./fn/httpFn')).default(request, response)
})

export const firestoreFn =
functions.firestore.document("users/{id}")
.onCreate(async (snapshot, context) => {
    // Move the implementation of this function to fn/firestoreFn.ts
    await (await import('./fn/firestoreFn')).default(snapshot, context)
})
然后,对于HTTP函数,fn/httpFn.ts中的实现如下所示:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()

// Note: Need to be explicit about parameter types here for HTTP
// type functions, to support type safety and IDE assistance.
export default async (
    request: functions.https.Request,
    response: functions.Response
) => {
    // Here we can use the admin SDK to get a document
    const snapshot = await admin.firestore()
        .collection('users')
        .doc('uid')
        .get()
    const data = snapshot.data()
    response.send(data)
}

在JavaScript中,策略是相同的。您只需要使用require而不是等待导入。

太棒了,谢谢!所以当我在不同的文件中拆分函数时,这种行为也适用?这一部分我还不是100%清楚。上面的例子是将函数拆分成不同的文件。但是,您不能在索引中到处声明它们。需要这么多。太棒了,谢谢!所以当我在不同的文件中拆分函数时,这种行为也适用?这一部分我还不是100%清楚。上面的例子是将函数拆分成不同的文件。但是,您不能在索引中到处声明它们。需要这么多。