javascript - 使用 firebase 函数的共享模块和打字稿别名
问题描述
我无法让我的共享打字稿库使用带有 Firebase 函数的别名路径工作。
如果我使用相对路径引用它,则代码会编译,但我的functions/
目录会上传到 Firebase Functions 并且不能使用其目录之外的相对文件。
目录结构
functions/
- tsconfig.json
- src/*.ts
- lib/*.js
shared/
- tsconfig.json
- src/*.ts
- lib/*.ts
src/
- components/*.vue
tsconfig.json
tsconfig-base.json
在我的函数文件中,我尝试引用我的一个共享模块,如下所示:
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
因为 Cloud Functions 需要在functions
目录中拥有所有依赖项,所以我无法部署这些函数,即使它们都使用相对路径进行编译。
我的设置、结构或部署有什么问题?
我也尝试过"@shared": "file:../shared"
按照functions/package.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"
]
}
共享/tsconfig.json
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"composite": true,
"baseUrl": "./",
"outDir": "./lib",
"rootDir": "./src",
},
"compileOnSave": true,
"include": [
"src"
],
"references": [],
"exclude": [
"lib",
"node_modules"
]
}
函数/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"
]
}
解决方案
对于任何感兴趣的人,我目前的解决方法是使用 webpack(或vue.config.js
在我的情况下)将共享文件复制到functions
目录中,然后添加functions/shared
到我的.gitignore
.
不是最好的方法,但它有效。
推荐阅读
- c# - 我正在制作一个 synapse x custom ui,但我不断收到此错误
- fonts - 有没有办法链接到谷歌驱动字体文件并将其用作字体?
- spring - 如何使用 Apache Solr 在 Spring-Boot 应用程序中实现模糊搜索?
- java - 检查 HashSet 中的多个关键字,并据此生成和输出
- r - R:使用贝叶斯优化对多类目标变量进行 XGBoost 的超参数调整
- python - Numpy矢量化元素是元组
- javascript - Nextjs router.push 在第一次点击时页面转换缓慢
- sql - 无法在 oracle 11g 中运行大表连接
- neo4j - Neo4j:如何使数据库重新联机
- java - 使用 Java 和 MySQL 更改密码代码错误