Javascript 使用firebase函数的共享模块和typescript别名
我在使用Firebase函数的别名路径使共享typescript库正常工作时遇到问题 如果我使用相对路径引用代码,则代码会编译,但我的Javascript 使用firebase函数的共享模块和typescript别名,javascript,typescript,firebase,google-cloud-functions,Javascript,Typescript,Firebase,Google Cloud Functions,我在使用Firebase函数的别名路径使共享typescript库正常工作时遇到问题 如果我使用相对路径引用代码,则代码会编译,但我的函数/目录会上载到Firebase函数,并且不能使用其目录外的相对文件 目录结构 函数/ -tsconfig.json -src/*.ts -lib/*.js 共享/ -tsconfig.json -src/*.ts -lib/*.ts src/ -组件/*.vue tsconfig.json tsconfig-base.json 在我的函数文件中,我尝试引用我
函数/
目录会上载到Firebase函数,并且不能使用其目录外的相对文件
目录结构
函数/
-tsconfig.json
-src/*.ts
-lib/*.js
共享/
-tsconfig.json
-src/*.ts
-lib/*.ts
src/
-组件/*.vue
tsconfig.json
tsconfig-base.json
在我的函数文件中,我尝试引用我的一个共享模块,如下所示:
从'@shared/src/MyClass'导入{MyClass};//错误:找不到模块'@shared/src/MyClass'
从“../../shared/src/MyClass”导入{MyClass}//这将编译,但部署云函数失败
因为云函数需要在Functions
目录中具有所有依赖项,所以我无法部署这些函数,即使它们都使用相对路径进行编译
我的设置、结构或部署有什么问题
我还尝试将“@shared”:“file:../shared”
添加到functions/package.json
,如前所述
tsconfig-base.json
共享/tsconfig.json
函数/tsconfig.json
我不是云函数方面的专家,但是如果您需要在
functions/
文件夹中拥有所有依赖项,那么在我看来,即使您可以设置路径映射,这也行不通,因为文件仍然在物理上位于外部。我这里错了什么?“路径”应该在编译器选项中。谢谢@AlekseyL它修复了本地编译问题,但仍然无法使用Firebase Emulator进行编译,并且无法部署运行时需要的所有代码必须位于functions文件夹或node_模块中。如果是后者,您的package.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"
]
}
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"composite": true,
"baseUrl": "./",
"outDir": "./lib",
"rootDir": "./src",
},
"compileOnSave": true,
"include": [
"src"
],
"references": [],
"exclude": [
"lib",
"node_modules"
]
}
{
"extends": "../tsconfig-base.json",
"references": [
{
"path": "../shared"
}
],
"compilerOptions": {
"baseUrl": "./",
"outDir": "./lib",
"rootDir": "./src",
"paths": {
"@shared/*": [
"../shared/*"
]
}
},
"compileOnSave": true,
"include": [
"src"
],
"exclude": [
"lib",
"node_modules"
]
}