[英]local test firebase functions with typescript
I have a problem with local test functions in typescript. Now, I used npm run build && firebase serve --only functions
to run local functions, but every change I made on the code, I need to run this command again to see the result.我在 typescript 中的本地测试功能有问题。现在,我使用npm run build && firebase serve --only functions
来运行本地功能,但是我对代码所做的每一个更改,我都需要再次运行此命令才能看到结果. I want to view real-time change without having to build the typescript code every time.我想查看实时变化,而不必每次都构建 typescript 代码。 Is there a solution to this problem?这个问题有解决办法吗?
The Firebase emulator will pick up changes to JavaScript automatically while it's running. Firebase 仿真器将在运行时自动获取对 JavaScript 的更改。 Since you're using TypeScript, you will have to compile to JavaScript in order for the emulator to pick up the change.由于您使用的是 TypeScript,因此您必须编译为 JavaScript 才能让模拟器接受更改。 You have two main choices:您有两个主要选择:
npm run build
again after each change每次更改后再次运行npm run build
npx tsc --watch
.你可以用npx tsc --watch
做到这一点。This is how I run both command together for my typescript functions这就是我为我的 typescript 函数同时运行两个命令的方式
tsc --watch & firebase serve --only functions
That is my package.json and an npm scripts so I can invoke it easily with one command and have it update whenever I make changes.那是我的 package.json 和 npm 脚本,所以我可以用一个命令轻松调用它,并在我进行更改时更新它。
Stop using firebase serve
and use npm run serve
after you cd to your functions folder.停止使用firebase serve
并使用npm run serve
在您 cd 到您的函数文件夹后。
npm run serve
This will automatically update changes on your typescript before serving这将在服务之前自动更新您的 typescript 上的更改
run tsc --watch
in the same time you run the emulator to transpile ts build files to js on every change.在运行模拟器的同时运行tsc --watch
以在每次更改时将 ts 构建文件转换为 js。
replace:代替:
"serve": "npm run build && firebase emulators:start --only functions",
with:和:
"serve": "npm run build && firebase emulators:start --only functions | npm run build:watch",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.