简体   繁体   English

使用 firebase 函数的共享模块和 typescript 别名

[英]Shared modules and typescript aliases using firebase functions

I'm having trouble getting my shared typescript library to work using an alias path with Firebase Functions.我无法让我的共享 typescript 库使用具有 Firebase 函数的别名路径工作。

The code compiles if I reference it using relative paths, but my functions/ directory uploads to Firebase Functions and can't use relative files outside of its directory.如果我使用相对路径引用它,代码将编译,但我的functions/目录上传到 Firebase 函数并且不能使用其目录之外的相对文件。

Directory Structure目录结构

functions/
- tsconfig.json
- src/*.ts
- lib/*.js

shared/
- tsconfig.json
- src/*.ts
- lib/*.ts

src/
- components/*.vue

tsconfig.json
tsconfig-base.json

In my function file, I try and reference one of my shared modules like so:在我的 function 文件中,我尝试引用我的一个共享模块,如下所示:

import { MyClass } from '@shared/src/MyClass'; // Error: Cannot find module '@shared/src/MyClass'

import { MyClass } from '../../shared/src/MyClass' // This Compiles, but fails deploying Cloud Functions

Because Cloud Functions needs to have all dependencies within the functions directory, I'm unable to deploy the functions, even though it all compiles using relative paths.因为 Cloud Functions 需要在functions目录中拥有所有依赖项,所以我无法部署这些函数,即使它们都使用相对路径进行编译。

What am I doing wrong with my setup, structure, or deployment?我的设置、结构或部署有什么问题?

I've also tried adding "@shared": "file:../shared" to functions/package.json as described here我还尝试将"@shared": "file:../shared"添加到functions/package.json中,如此 所述

tsconfig-base.json tsconfig-base.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "sourceMap": true,
        "strict": true,
        "declaration": true,
        "declarationMap": true,
        "lib": [
            "es2018"
        ],
        "target": "es2018",
        "types": [
            "node"
        ]
    },
    "compileOnSave": true,
    "files": [],
    "include": [],
    "exclude": [
        "lib",
        "node_modules"
    ]
}

shared/tsconfig.json共享/tsconfig.json

{
  "extends": "../tsconfig-base.json",
  "compilerOptions": {
    "composite": true,
    "baseUrl": "./",
    "outDir": "./lib",
    "rootDir": "./src",
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
  "references": [],
  "exclude": [
    "lib",
    "node_modules"
  ]
}

functions/tsconfig.json函数/tsconfig.json

{
  "extends": "../tsconfig-base.json",
  "references": [
    {
      "path": "../shared"
    }
  ],
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./lib",
    "rootDir": "./src",
    "paths": {
      "@shared/*": [
        "../shared/*"
      ]
    }
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
  "exclude": [
    "lib",
    "node_modules"
  ]
}

My current workaround for anyone interested is to use webpack (or vue.config.js in my case) to copy shared files into the functions directory, and add functions/shared to my .gitignore .对于任何感兴趣的人,我目前的解决方法是使用vue.config.js (或在我的情况下为 vue.config.js )将共享文件复制到functions目录中,并将functions/shared添加到我的.gitignore

Not the best approach, but it works.不是最好的方法,但它有效。

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

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