如何使用pnpm工作区协议和部署Firebase函数?

7cjasjjr  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(176)

我有一个pnpm monorepo,我使用workspace:协议将我的shared文件夹添加为本地包。
部署函数时Firebase无法识别workspace:协议,我遇到以下错误:

Build failed: npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "workspace:": workspace:../shared
npm ERR! A complete log of this run can be found in:
npm ERR! /www-data-home/.npm/_logs/2023-03-19T21_44_16_828Z-debug-0.log; Error ID: b0ba1f57

如何使用pnpm工作区部署Firebase函数?

j8ag8udp

j8ag8udp1#

  1. functions/package.json
{
  "private": true,
  "name": "functions",
  "main": "dist/index.js",
  "scripts": {
    "dev": "tsc --watch",
    "build": "tsc",
    "pre-deploy": "node pre-deploy.js",
    "post-deploy": "node post-deploy.js"
  },
  "dependencies": {
    "firebase-admin": "11.5.0",
    "firebase-functions": "4.2.1",
    "shared": "workspace:../shared"
  },
  "devDependencies": {
    "typescript": "4.9.5"
  },
  "engines": {
    "node": "18",
    "pnpm": "7"
  }
}
  1. functions/pre-deploy.js
#!/usr/bin/env node

const { existsSync, copyFileSync, readFileSync, writeFileSync } = require('fs')

const packagePath = './package.json'
const packageCopyPath = './package-copy.json'
const pnpmWorkspaceRegex = /workspace:/gi

// Abort if "package-copy.json" exists
if (existsSync(packageCopyPath)) {
  console.error(`"${packageCopyPath}" exists, previous deployment probably failed.`)
  return
}

// Copy "package.json" file
copyFileSync(packagePath, packageCopyPath)

// Read "package.json" file and replace "workspace:" to "file:" protocol
const packageBuffer = readFileSync(packagePath)
const packageContent = packageBuffer.toString()
const packageNewContent = packageContent.replace(pnpmWorkspaceRegex, 'file:')

writeFileSync(packagePath, packageNewContent)
  1. functions/post-deploy.js
#!/usr/bin/env node

const { rmSync, renameSync } = require('fs')

const packagePath = './package.json'
const packageCopyPath = './package-copy.json'

// Restore original "package.json" file with "workspace:" protocol
rmSync(packagePath)
renameSync(packageCopyPath, packagePath)
  1. firebase.json
{
  "functions": {
    "source": "functions",
    "codebase": "default",
    "predeploy": [
      "pnpm --filter functions run build",
      "pnpm --filter functions run pre-deploy"
    ],
    "postdeploy": [
      "pnpm --filter functions run post-deploy"
    ],
    "ignore": [
      ".git",
      "node_modules",
      "firebase-debug.log",
      "firebase-debug.*.log"
    ]
  }
}

上述步骤假定pnpm-workspace.yaml文件包含以下信息:

packages:
  - 'functions'
  - 'shared'

相关问题