Firebase-tools: Firebase提供-仅功能,不能热重载

创建于 2018-05-01  ·  43评论  ·  资料来源: firebase/firebase-tools

版本信息

火力基地--version
3.18.4

平台资讯

Ubuntu 18.04 x64

重现步骤

  1. 使用yarn的TypeScript与Firebase云功能项目
  2. 纱线成型-观察
  3. 在浏览器中查看HTTPS功能
  4. 更新功能响应
  5. 在浏览器中刷新页面

预期行为

浏览器显示更新的内容

实际行为

该页面仅在重新启动firebase serve命令后更新。 我已确认lib中的输出已更新。

> firebase serve --only functions --debug                                                                                                                                                             [17:29:27]
[2018-05-01T00:29:30.975Z] ----------------------------------------------------------------------
[2018-05-01T00:29:30.978Z] Command:       /home/shane/.nvm/versions/node/v6.11.5/bin/node /home/shane/.yarn/bin/firebase serve --only functions --debug
[2018-05-01T00:29:30.978Z] CLI Version:   3.18.4
[2018-05-01T00:29:30.979Z] Platform:      linux
[2018-05-01T00:29:30.979Z] Node Version:  v6.11.5
[2018-05-01T00:29:30.979Z] Time:          Mon Apr 30 2018 17:29:30 GMT-0700 (PDT)
[2018-05-01T00:29:30.979Z] ----------------------------------------------------------------------

[2018-05-01T00:29:30.986Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2018-05-01T00:29:30.986Z] > authorizing via signed-in user
[2018-05-01T00:29:30.988Z] >>> HTTP REQUEST GET https://admin.firebase.com/v1/projects/nativ-dev  

 Mon Apr 30 2018 17:29:30 GMT-0700 (PDT)
[2018-05-01T00:29:31.433Z] <<< HTTP RESPONSE 200 server=nginx, date=Tue, 01 May 2018 00:29:31 GMT, content-type=application/json; charset=utf-8, content-length=128, connection=close, x-content-type-options=nosniff, strict-transport-security=max-age=31536000; includeSubdomains, cache-control=no-cache, no-store
[2018-05-01T00:29:31.437Z] >>> HTTP REQUEST GET https://admin.firebase.com/v1/database/nativ-dev/tokens  

 Mon Apr 30 2018 17:29:31 GMT-0700 (PDT)
[2018-05-01T00:29:31.926Z] <<< HTTP RESPONSE 200 server=nginx, date=Tue, 01 May 2018 00:29:31 GMT, content-type=application/json; charset=utf-8, content-length=256, connection=close, x-content-type-options=nosniff, strict-transport-security=max-age=31536000; includeSubdomains, cache-control=no-cache, no-store
[2018-05-01T00:29:31.927Z] >>> HTTP REQUEST GET https://cloudresourcemanager.googleapis.com/v1/projects/nativ-dev  

 Mon Apr 30 2018 17:29:31 GMT-0700 (PDT)
[2018-05-01T00:29:32.335Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Tue, 01 May 2018 00:29:32 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35", accept-ranges=none, connection=close

=== Serving from '/home/shane/source/firebase'...

i  functions: Preparing to emulate functions.
[2018-05-01T00:29:32.733Z] Fetching environment
[2018-05-01T00:29:32.736Z] >>> HTTP REQUEST GET https://mobilesdk-pa.googleapis.com/v1/projects/766196581577:getServerAppConfig

 Mon Apr 30 2018 17:29:32 GMT-0700 (PDT)
[2018-05-01T00:29:33.255Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Tue, 01 May 2018 00:29:33 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35", accept-ranges=none, connection=close
[2018-05-01T00:29:33.256Z] Starting @google-cloud/functions-emulator
[2018-05-01T00:29:34.318Z] Parsing function triggers
i  functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
i  functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
✔  functions: createCustomerOnCall: http://localhost:5000/nativ-dev/us-central1/createCustomerOnCall
✔  functions: createCustomerHttps: http://localhost:5000/nativ-dev/us-central1/createCustomerHttps
info: User function triggered, starting execution
info: Execution took 19 ms, user function completed successfully
info: User function triggered, starting execution
info: Execution took 1 ms, user function completed successfully
functions bug

最有用的评论

如果其他人最终在这里,实际上是在functions/package.json进行设置的方法,因此tsc -wfirebase serve分开运行:

{
  "name": "functions",
  "scripts": {
...
    "serve": "tsc -w | firebase serve --only functions",
...
  },

换句话说,运行tsc -w并将输出通过管道传递到firebase serve 。 我认为管道实际上没有发送任何内容。 我认为,当源发生更改时,tsc将重新运行,并且firebase serve正在监视tsc的输出目录( lib ),以便重新加载。 因此,管道实际上仅用于过程管理。

所有43条评论

我刚刚在具有新项目(firebase初始化)的另一台机器(Debian Stretch)上尝试了此操作,并且hello world函数发生了相同的事情。 我确实注意到,如果在向函数发送请求之前更改了源,但是在该函数准备好服务之后,我可以使其一次热重新加载,然后将再次停止工作。

我尝试在Mac和Linux机器上创建一个新项目(firebase init),它可以在Mac上运行,而不能在Linux上运行。

我做了一些试验,可以通过JavaScript获得Hello World示例进行热重载,但没有获得TypeScript示例。 似乎firebase serve只监视`firebase.json中配置的目录中的index.js

要为TypeScript重新加载项目(并且我假设更复杂的JavaScript布局不仅包含index.js ),我必须使用Webpack输出单个文件functions/lib/index.js并配置Firebase查找我的函数代码在functions/list 。 然后,这将使firebase functions:shellfirebase serve都可以进行热重装。

当我使用tsc (按照firebase init模板)时,仅当我修改了生成的index.js文件时,代码才会重新加载。 我当前的工作流程是在一个终端中运行webpack --watch在另一个终端中运行firebase serve --only functions 。 我现在得到热装弹。 如果可以从firebase init返回默认值,那将是很好的。

更新了firebase.json

{    
  "functions": {
    "predeploy": "npm --prefix functions run build",
    "source": "functions/list"
  }
}

@safarmer您的functions / package.json是什么样的? 它有一个main字段吗?

我已经开始使用由firebase init生成的默认值

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc --project tsconfig.json",
    "serve": "yarn run build && firebase serve --only functions",
    "shell": "yarn run build && firebase functions:shell",
    "start": "yarn run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "private": true
}

我必须复制问题的最简单方法:

  1. firebase init && cd functions && yarn
  2. 取消注释helloWorld函数
  3. mkdir src/handlers && mv src/index.ts src/handlers/helloworld.ts
  4. echo "export * from './handlers/helloworld';" > src/index.ts
  5. 在航站楼1: yarn build --watch
  6. 在航站楼2: firebase serve --only functions

在浏览器中访问URL,您会看到世界问候消息。 如果更改从handler.ts中的处理程序返回的字符串,则更改结果的唯一方法是重新启动firebase serve命令。

如果在functions/lib任何更改,则将"source": "functions/lib"firebase.json会使代码热重载,但较低的杠杆目录不会触发重载。 可以运行touch lib/injex.js触发重新加载。 例如,在src/index.ts更改处理程序将触发重新加载,但更改src/handlers/helloworld.ts则不会。

到目前为止,我最好的是firebase init的默认结果)。 这样,我仍然需要运行touch lib/index.js 。 与此相关的另一个问题是firebase deploy不再起作用。

firebase.json

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions/lib"
  }
}

函数/package.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

@laurenzlong我有一个简单的示例,您可以克隆以在Linux上重现该问题:
https://github.com/safarmer/firebase-functions

感谢您的复制! 下周我去看看。

同样的问题在这里:)
在我看来, firebase serve目前暂不支持Typescript ...

我已将您请求的文件与@safarmer发送的文件进行了比较,并且我具有相同的配置。 令我惊讶的是,这是用于TypeScript的官方配置

我认为问题在于firebase serve不监听函数“ source”目录中的更改,并且不会运行firebase.json定义的预部署脚本。

如果将其添加,那就太好了。 :D

嗯,如果我可以为firebase服务定义其他预部署脚本,那就太好了,这样我就不必在测试中运行tslint了:)

@IchordeDionysos我不认为它只是TypeScript。 我试图做与我的JavaScript示例相同的事情,而且似乎也做同样的事情。 我认为问题在于服务器仅查看入口点,而不是其余代码。 希望火力基地团队能够尽快阐明这一问题。

@laurenzlong手动安装值班员似乎可以使一切正常(感谢@tstirrat的建议)

感谢详细的repro @safarmer ,非常有帮助。 我能够找到问题的根源,并提交了https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/207。 请关注该问题。

对于其他人,请从源代码Linuxbrew安装watchman而不是npm软件包

@laurenzlong我可以在Windows中重现此问题,尤其是index.ts更改未检测到。 我必须手动运行tsc。 然后检测到更改,然后重新加载该功能。
我应该另开一个问题吗?

快速说明:有时在ts上检测到info: Worker for helloWorld closed due to file changes. ,但没有刷新lib。 其他时候根本没有检测到。 手动运行tsc总是会重新加载该功能。

@tstirrat按照指示在Windows上安装watchman似乎不起作用

安装watchman时,您特别遇到什么错误?

安装守卫时没有错误。 但是服务功能并不热门
重新加载。 每次更改后,我都必须停止服务,构建并再次提供服务。

在2018年11月21日星期三凌晨1:01,Kevin Jian [email protected]写道:

安装watchman时,您特别遇到什么错误?

-
您收到此邮件是因为您发表了评论。
直接回复此电子邮件,在GitHub上查看
https://github.com/firebase/firebase-tools/issues/758#issuecomment-440460196
或使线程静音
https://github.com/notifications/unsubscribe-auth/AHgHbeA_iECsIMH3V9P7NQypsmudF_IQks5uxInggaJpZM4Tta12

由于多个报告而重新打开,但仍无法在Windows上使用

仍然无法在带有Node.js v10.15.0的Ubuntu 18.04上运行

大家好,谢谢您的详细报告。 @afuggini单击该链接时得到404,是否在该回购上正确设置了权限以供公众查看? 还有另一个与此类似的问题: https :

我已在内部跟踪器中提交了一个错误:123266946。我无法承诺何时可以解决此问题,但知道正在跟踪此问题。 PR是最欢迎的!

我也遇到了这个错误。 我无法创建任何解决方法,因此每次更改后都必须重新启动服务器。 如果您知道可靠的解决方法,请告诉我。

@ondratra您能否提供当前的CLI版本,OS平台,并让我们知道您是否正在使用npm以外的其他工具(例如yarn )?

@bkendall我在Debian(sid)上。 通过tsc -w查看和编译Typescript源文件,并通过firebase --only functions serve提供函数。 package.json包含正确的路径
js输出文件{"main": "dist/src/index.js", ...} 。 我什至尝试使用tsc-watch包和显式的touch索引文件,但没有成功。

$ firebase --version
6.3.1

伙计们,请检查我以前的评论。 该问题与cloud-functions-emulator 。 它使用fs监视功能,在Linux上存在一个已知问题。 因此,不要期望直接从firebase-tools.获得解决方案

@ribizli thx,您共享的链接肯定可以说明问题的本质。 但是我仍然没有找到任何解决方法。 是否有可能通过自定义脚本监视文件并强制Firebase重新加载?

@ondratra fork @google-cloud/functions-emulator ,添加node-watch如我在链接注释中提到的, npm link包。 重新安装firebase-tools :它具有@google-cloud/functions-emulator作为可选依赖项,因此将使用您的链接版本。

最后:您可以对https://github.com/GoogleCloudPlatform/cloud-functions-emulator进行公关,希望他们会尽快接受并发布。 (我可以使用我的解决方法,所以我没有贡献)

功能-> index.js保存此文件后,如何启动“ firebase serve”自动运行

来自https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/196的解决方法(感谢@dirkjot):

这对我有用,这里有完整的说明和

  • node_modules找到您的@google-cloud目录(有关Firebase,请参见下文)
  • cd functions-emulator
  • 添加节点监视: npm install --save node-watch
  • 编辑文件src/supervisor/worker.js
  • 在第180行附近,您会发现fs.watch(localdir, { 。 将其替换为require('node-watch')(localdir, {

使用带有Typescript的Firebase,我要做的就是在单独的终端中运行tsc -wnpm run build的watch变体)。 现在,我可以更改源代码,仿真器将注意到这些更改。

如果您在全球范围内安装了firebase ,则可以通过导航到cd $(npm root -g)/firebase-tools/node_modules/@google-cloud找到这些文件。

嘿伙计 -

此错误与旧版Cloud Functions模拟器的热重装功能有关。 从19年5月开始,我们不再在firebase-tools使用google-cloud/functions-emulator ,因此这改变了错误的性质(我相信新的性质是它不存在)。

我感谢每个人都在努力工作并找出解决方法,最后,只要您分别运行观察器(例如tsc -w )和firebase serve我们的新仿真器应允许重新编译任何已翻译的语言。

有了新的模拟器后端,我们已经解决了这个问题,现在该线程中的所有解决方法都已过时,因此,我将关闭此线程以避免混淆。

如果您仍然看到任何意外的行为,请打开一个新的错误。

再次感谢!

如果其他人最终在这里,实际上是在functions/package.json进行设置的方法,因此tsc -wfirebase serve分开运行:

{
  "name": "functions",
  "scripts": {
...
    "serve": "tsc -w | firebase serve --only functions",
...
  },

换句话说,运行tsc -w并将输出通过管道传递到firebase serve 。 我认为管道实际上没有发送任何内容。 我认为,当源发生更改时,tsc将重新运行,并且firebase serve正在监视tsc的输出目录( lib ),以便重新加载。 因此,管道实际上仅用于过程管理。

并发安装对我有用:

"build:watch": "tsc -w && cp src/*.json lib",
"serve": "firebase serve --only functions",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",

对我来说, [email protected]热重装无法立即使用。 我将build中的functions/package.jsontsc -w作为

{
  "name": "functions",
  "scripts": {
   ...
      "build": "tsc -w",
   ...
  },

当我运行npm run build ,然后从another terminal运行firebase emulators:start时,这将使手表的功能保持活动状态。

@ rami-alloush也是我使用的工作流程。 模拟器不会重新构建您的代码,但会热重新加载已构建的代码。

另一个版本

{
    "build": "tsc -w &>/dev/null &",
    "shell": "npm run build && firebase functions:shell",
}

这会在后台安静地运行tsc ,因此您不需要其他终端。

感谢您的复制! 下周我去看看。

是吗?

遇到与打字稿相同的问题,热重装无法正常工作。

一样...热重装不起作用,并且很难让仿真器完全识别出更改...

@oluckyman解决了问题。 非常好

https://github.com/firebase/firebase-tools/issues/758#issuecomment -620096052

[更新]

  • firebase-tools => 8.7.0

我的解决方案:

package.json

...
 "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc -w --preserveWatchOutput",
    "serve": "tsc && firebase emulators:exec --ui --only functions,firestore 'yarn build'",
    "shell": "yarn run build && firebase functions:shell",
    "start": "yarn run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
...

运行yarn serve

演示版

Aug-06-2020 14-05-38

目前,在使用typescript的firebase-tools版本8.8.1上,这对我来说效果很好。 谢谢你的提示伙计们@moifort @garyo @safarmer @abeisgoat :1st_place_medal::

"scripts": {
    ...
    ...
    "serve": "yarn build | firebase emulators:start --only functions",
    "build": "./node_modules/.bin/tslint --project tsconfig.json && ./node_modules/.bin/tsc --watch --preserveWatchOutput"
  },
此页面是否有帮助?
0 / 5 - 0 等级