简体   繁体   English

Firebase部署云函数前如何复制资产文件

[英]How to copy asset files before deploying cloud functions to Firebase

I have a firebase project where I write typescript functions to be deployed into Google Node Cloud functions.我有一个 firebase 项目,我在其中编写了 typescript 个要部署到 Google 节点云函数中的函数。 When I run the firebase deploy --only functions command, it transpiles my code into javascript and put the js output into a folder called lib next to src folder where my typescript functions are.当我运行firebase deploy --only functions命令时,它将我的代码转换为 javascript 并将 js output 放入我的 typescript 函数所在的src文件夹旁边的名为lib的文件夹中。 However, some of my functions need access to local files such as.ttf files or some other file types.但是,我的一些函数需要访问本地文件,例如 .ttf 文件或其他一些文件类型。 Those don't get copied over to the lib folder and therefore, I get errors in runtime Error: ENOENT: no such file or directory, open path/to/file那些没有被复制到 lib 文件夹,因此,我在运行时遇到错误Error: ENOENT: no such file or directory, open path/to/file

Question 1: How do I get the deploy command to copy assets files to the output folder?问题一:如何获取deploy命令将assets文件复制到output文件夹中?

Question 2: Given that all my functions live in separate files, in separate folders, and so do my assets, how should I reference my assets so that they can be found?问题 2:鉴于我的所有功能都位于单独的文件和文件夹中,我的资产也是如此,我应该如何引用我的资产以便找到它们? Should I give the path to the asset file relative to the lib folder or relative to where the function lives?我应该给出资产文件相对于lib文件夹的路径还是相对于 function 所在位置的路径?

EDIT -1编辑-1

See the project structure here: the code that need the font lives in the some-function.ts file.请在此处查看项目结构:需要字体的代码位于 some-function.ts 文件中。 And it uses the pdfmake library that needs fonts to work.它使用需要 fonts 才能工作的 pdfmake 库。 Here is how I do it in the some-function.ts file:以下是我在 some-function.ts 文件中的做法:

const fonts: TFontDictionary = {
    Poppins: {
      normal: './fonts/Poppins/Poppins-Regular.ttf',
      bold: './fonts/Poppins/Poppins-Bold.ttf',
      italics: './fonts/Poppins/Poppins-Medium.ttf',
      bolditalics: './fonts/Poppins/Poppins-Thin.ttf',
    }
  };
const pdfmake = require('pdfmake');
const printer = new pdfmake(fonts);

So how do I reference such fonts given that they are located in the fonts folder.那么我如何引用这样的 fonts ,因为它们位于 fonts 文件夹中。 or event if I put them in a separate folder at the root or src?或者如果我将它们放在根目录或 src 的单独文件夹中?

在此处输入图像描述

After some digging I came to a solution that I would like to share here.经过一番挖掘,我找到了一个解决方案,我想在这里分享。

1- First thing, as suggested by @Dharmaraj, I added a script that removes the lib folder, before building the project, and copy my assets files before deploying. 1- 首先,正如@Dharmaraj 所建议的,我在构建项目之前添加了一个删除 lib 文件夹的脚本,并在部署之前复制了我的资产文件。 So in the package.json under functions folder, I updated the build command as follow所以在函数文件夹下的 package.json 中,我更新了构建命令如下

"remove-lib":"rm -rf lib",
"copy-assets":"cp -rf src/path/to/your-folder/fonts lib/path/to/your-folder",
"do-build":"tsc",
"build": "npm run remove-lib && npm run do-build && npm run copy-assets",

Actually, adding the remove lib command solved another issue I had with deploying functions.实际上,添加 remove lib 命令解决了我在部署函数时遇到的另一个问题。 I think this should be the default behaviour because when you rename function files, old ones stick in lib folder causing all sort of issues.我认为这应该是默认行为,因为当您重命名 function 文件时,旧文件会留在 lib 文件夹中,从而导致各种问题。

Anyway, now in order to correctly reference your asset, just construct the path as ${__dirname}/fonts/some-font.ttf provided that the fonts folder lives in the same folder as the file your are editing.无论如何,现在为了正确引用您的资产,只需将路径构造为${__dirname}/fonts/some-font.ttf ,前提是 fonts 文件夹与您正在编辑的文件位于同一文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM