带纱线工作区的Firebase函数

时间:2019-04-21 15:16:14

标签: node.js firebase google-cloud-functions monorepo yarn-workspaces

我们开始采用使用毛线工作区的monorepo设置,并且希望在其中包含我们的firebase函数。回购结构类似于:

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

因此,此设置有两个问题:

  1. 函数的依赖项与函数的入口文件不在同一个文件夹中
  2. 功能依赖于存储库中的其他软件包,例如corecommom,因此从node_modules到存储库中的软件包进行符号链接。

反正我可以应付吗?

3 个答案:

答案 0 :(得分:1)

我为此找到的解决方案是根nohoist文件中Yarn的package.json选项。

默认情况下,Yarn将依赖关系提升到根目录,以便可以在您的程序包之间共享它们。不幸的是,这不适用于Firebase。这意味着您需要告诉Yarn不要提升Firebase函数使用的依赖项。

nohoist的文档并不理想,但是这里是有关此文档的官方博客文章: https://yarnpkg.com/blog/2018/02/15/nohoist/

您可能想要这样的东西:

{
  "workspaces": {
    "packages": [
      "packages/*"
    ],
    "nohoist": [
      "functions/core",
      "functions/common",
      "functions/**"
    ]
  }
}

请记住,这使用了每个工作区软件包的name文件中使用的package.json字段。因此,在此示例中,假设functions目录的package.json带有{functions“,因为它是name

functions/**告诉yarn不要提升packages/functions/package.json中指定的任何依赖项。但是,这不适用于您的共享纱线卷装,因此functions/corefunctions/common需要分别指定。

您还需要将工作空间作为依赖项包含在functions项目中,因此将它们添加到package.json

{
  "name": "functions",
  "dependencies": {
    "core": "*",
    "common": "*",
  }
}

添加完所有内容后,应删除packages/functions/node_modules目录并运行yarn install。完成此操作后,您应该看到packages/functions/node_modules中包含了所有依赖性(不是符号链接)。

答案 1 :(得分:1)

使用 Yarn 2 printComponent(cmpName) { var docHead = document.head.outerHTML; var printContents = document.getElementById('component1').outerHTML; var winAttr = "location=yes, statusbar=no, menubar=no, titlebar=no, toolbar=no,dependent=no, width=865, height=600, resizable=yes, screenX=200, screenY=200, personalbar=no, scrollbars=yes"; var newWin = window.open("", "_blank", winAttr); var writeDoc = newWin.document; writeDoc.open(); writeDoc.write('<!doctype html><html> <style>.fc-header-toolbar{display :none !important;} .fc-scroller.fc-scroller-liquid{overflow:visible !important;} .fc-view-harness.fc-view-harness-active{height:auto !important;}</style>' + docHead + '<body onLoad="window.print()">' + printContents + '</body></html>'); writeDoc.close(); newWin.focus(); } 不会被提取并放入各自的 node_modules 目录中(就像在functions 目录)。因此,在调用 npm i 时,functions 文件夹丢失,firebase 会对此进行投诉并中止部署过程并显示以下错误(或类似错误):

firebase deploy --project default --only function

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

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

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

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

上面的两个指令在 yarnrc configuration docs 中解释如下:

<块引用>

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

<块引用>

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

答案 2 :(得分:0)

我不确定我是否完全理解这个问题,但是我可以根据您对问题的了解和使用经验,在纱线工作区给我2美分。

Yarn工作区将所有依赖项合并到项目根目录中的 node_modules 中,以及单个 package-lock.json 中,以减少冲突并允许yarn优化安装过程为您提供了更快的yarn install。而且它的另一个优点是,只需一次通过yarn install就可以在工作空间下安装所有软件包的依赖项。

编辑:我认为由于某些原因yarn link未被调用,而仅运行yarn install,这将搜索npm注册表并引发在中提到的错误。注释,因为它无法在npm注册表中找到提到的软件包。因此,对于解决方案,请尝试在Firebase的package.json中创建一个条目,例如

"dependencies": {
  "a": "file:../dependency-package-name/",
}