Firebase Functions with Yarn workspaces

后端 未结 2 1866
耶瑟儿~
耶瑟儿~ 2021-02-05 14:33

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:

re         


        
2条回答
  •  抹茶落季
    2021-02-05 15:00

    The solution I found for this is Yarn's nohoist option in your root package.json file.

    By default Yarn hoists dependencies to the root directory so they can be shared between your packages. Unfortunately this will not work with Firebase. This means you need to tell Yarn not to hoist the dependencies used by your Firebase functions.

    The documentation for nohoist is less than ideal, but here is an official blog post about it here: https://yarnpkg.com/blog/2018/02/15/nohoist/

    You probably want something like this:

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

    Keep in mind that this uses the name field used in the package.json files of each workspace package. So in this example, it is assume that the functions directory has a package.json with "functions" as it's name.

    functions/** tells yarn not to hoist any of the dependencies specified in packages/functions/package.json. This doesn't work for your shared yarn packages though, so functions/core and functions/common need to be specified separately.

    You also need to include your workspaces as dependencies in your functions project, so add them to your package.json:

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

    Once you have added all that, you should delete your packages/functions/node_modules directory and run yarn install. After doing this, you should see all your dependencies included in packages/functions/node_modules (not symlinks).

提交回复
热议问题