首页 > 解决方案 > 带有 Yarn 工作区的 Firebase 函数

问题描述

我们开始采用使用 yarn 工作区的 monorepo 设置,我们希望在其中包含我们的 firebase 功能。回购结构类似于:

repo
    node_modules <- all dependencies
    packages
        core
        commom
        functions <- firebase functions

所以,这个设置我有两个问题:

  1. 函数的依赖项与函数的入口文件不在同一个文件夹中
  2. 这些功能取决于 repo 中的其他包,例如corecommom,因此 yarn 符号链接从 node_modules 到 repo 中的包。

无论如何我可以处理这个吗?

标签: node.jsfirebasegoogle-cloud-functionsmonorepoyarn-workspaces

解决方案


使用Yarn 2 node_modules不会被提取并放入相应的functions目录中(就像npm ifunctions目录中调用的情况一样)。因此,当调用firebase deploy --project default --only functionnode_modules文件夹时,firebase 会抱怨这一点并中止部署过程并出现以下错误(或类似错误):

Error parsing triggers: Cannot find module [...]
Try running "npm install" in your functions directory before deploying.

目前有两个 github 问题正在跟踪此问题:

在上面的两个问题中,firebase 用户提出了几个巧妙的解决方法,例如使用 webpack 创建包含发布中所有本地包的构建,或者使用 rsync 或其他工具在发布前重新连接包。

如果可能的话,另一个解决方案是不提升您的项目包。您可以这样做,将以下两个指令添加到您的.yarnrc.yml文件中。

# yarnrc.yml

# disables yarn's plugnplay style and uses node_modules instead
nodeLinker: node-modules
# makes sure the node_modules are not hoisted to the (monorepo) project root
nmHoistingLimits: "dependencies"

上面的两个指令在yarnrc 配置文档中解释如下:

nmHoistingLimits定义可以吊起包裹的最高点。工作区之一(不要将包提升到依赖于它们的工作区)、依赖项(不将包提升到每个工作区的直接依赖项之外)或无(默认情况下,尽可能多地提升包)。可以通过 installConfig.hoistingLimits 字段覆盖每个工作区的此设置。

nodeLinker定义应该使用什么链接器来安装 Node 包(用于启用 node-modules 插件),其中之一是:pnp、node-modules。


推荐阅读