带有 Yarn 工作区的 Firebase 函数 [英] Firebase Functions with Yarn workspaces
问题描述
我们开始采用使用 yarn 工作区的 monorepo 设置,我们希望在其中包含我们的 firebase 功能.回购结构类似于:
回购node_modules <- 所有依赖包核公用函数 <- firebase 函数
所以,这个设置有两个问题:
- 函数的依赖项与函数的入口文件不在同一个文件夹中
- 这些函数依赖于 repo 中的其他包,例如
core
和commom
,因此 yarn 符号链接从 node_modules 到 repo 中的包.
有什么我可以处理的吗?
使用 Yarn 2 node_modules
不会被提取并放入各自的 functions
目录(就像在 functions
目录中调用 npm i
一样).因此,当调用 firebase deploy --project default --only function
时,node_modules
文件夹丢失,firebase 会对此抱怨并中止部署过程并出现以下错误(或类似错误)):
错误解析触发器:找不到模块 [...]尝试运行npm install"在部署之前在您的函数目录中.
目前有两个 github 问题正在跟踪此问题:
在上述两个问题中,firebase 用户提出了几个巧妙的解决方法,例如使用 webpack 创建包含发布中所有本地包的构建,或使用 rsync 或其他工具在发布前重新连接包.
如果可能的话,另一个解决方案是不提升您的项目包.您可以这样做,将以下两个指令添加到您的 .yarnrc.yml
文件中.
# yarnrc.yml# 禁用 yarn 的 plugnplay 风格并使用 node_modules 代替nodeLinker:节点模块# 确保 node_modules 没有被提升到(monorepo)项目根目录nmHoistingLimits:依赖项";
上述两个指令在yarnrc 配置文档中解释如下:
<块引用>nmHoistingLimits 定义可以吊起包裹的最高点.工作区之一(不要将包提升到依赖于它们的工作区)、依赖项(不将包提升到每个工作区的直接依赖项之外)或无(默认情况下,尽可能多地提升包).可以通过 installConfig.hoistingLimits 字段覆盖每个工作区的此设置.
<块引用>
nodeLinker 定义应该使用什么链接器来安装 Node 包(用于启用 node-modules 插件),其中之一是:pnp、node-modules.
We're starting to adopt a monorepo setup using yarn workspaces and we'd like to have our firebase functions inside it. The repo structure is something like:
repo
node_modules <- all dependencies
packages
core
commom
functions <- firebase functions
So, I have 2 problems with this setup:
- The dependencies of the functions don't live on the same folder as the entry file from functions
- The functions depends on other packages such as
core
andcommom
that are in the repo so yarn symlinks from node_modules to the packages in the repo.
Is there anyway I can handle this?
With Yarn 2 node_modules
aren't fetched and placed into in the respective functions
directory (as it would be the case with calling npm i
in the functions
directory). So when calling firebase deploy --project default --only function
the node_modules
folder is missing and firebase will complain about this and abort the deployment process with the following error (or similar):
Error parsing triggers: Cannot find module [...]
Try running "npm install" in your functions directory before deploying.
There are two github issues that are tracking this issue at the moment:
- Support mono-repos in deployment
- Functions deployment fails when firebase-functions has been hoisted by a monorepo manager like yarn/workspaces or lerna
In the two issues above, several clever workarounds are presented by firebase users, e.g. using webpack to create a build that contains all the local packages in the release or using rsync or other tools that rewire the packages before release.
Another solution is not hoisting your project packages, if that is possible. You can do this, be adding the following two directives to your .yarnrc.yml
file.
# 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"
The two directives above are explained in the yarnrc configuration docs as follows:
nmHoistingLimits Defines the highest point where packages can be hoisted. One of workspaces (don't hoist packages past the workspace that depends on them), dependencies (packages aren't hoisted past the direct dependencies for each workspace), or none (the default, packages are hoisted as much as possible). This setting can be overriden per-workspace through the installConfig.hoistingLimits field.
nodeLinker Defines what linker should be used for installing Node packages (useful to enable the node-modules plugin), one of: pnp, node-modules.
这篇关于带有 Yarn 工作区的 Firebase 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!