wil*_*iam 5 firebase google-cloud-functions
如何将TS路径映射与Firebase Cloud函数一起使用?我尝试没有成功:
"baseUrl": ".",
"paths": {
"@custom-path/*": ["src/utils/*"],
"@other-path/*": ["../other/path/*"]
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*osa 15
最简单2024的方法是使用tsc-alias。
上tsconfig.json,添加baseUrl并添加您的paths下compilerOptions。就像是:
{\n "compilerOptions": {\n ...\n "baseUrl": "./src",\n "paths": {\n "@constants/*": ["api/constants/*"],\n "@interfaces/*": ["api/interfaces/*"],\n "@middlewares/*": ["api/middlewares/*"],\n "@modules/*": ["api/modules/*"],\n "@services/*": ["api/services/*"]\n },\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n然后,更改下的serve和脚本。喜欢:buildpackage.json
...\n "scripts": {\n "build": "tsc && tsc-alias",\n "build:watch": "concurrently \\"tsc -w\\" \\"tsc-alias -w\\"",\n "serve": "concurrently --kill-others \\"firebase emulators:start --only functions\\" \\"npm run build:watch\\"",\n ...\n },\n...\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x98\x9d\xef\xb8\x8f 这里我使用的是concurrently,但随意使用你喜欢的任何东西。
就是这样。您现在可以使用定义的路径导入内容,例如:
\nimport { messages } from \'@constants/responses\'\nimport CandidatesService from \'@modules/candidates/candidates.service\'\nimport { IModule } from \'@interfaces/module.interface\'\nRun Code Online (Sandbox Code Playgroud)\nETC...
\n小智 9
最后我能够用module-aliasNPM 包做到这一点。
yarn add module-alias @types/module-aliasfixTsPaths.ts或任何内容如下:import * as ModuleAlias from 'module-alias';
ModuleAlias.addAliases({
'common': __dirname + '/../../../common',
});
Run Code Online (Sandbox Code Playgroud)
这是有关路径的技巧/../../../common:在我的情况下,此文件夹在外部functions,并且 Typescript 在构建期间复制文件夹结构,因此这可能是https://github.com/dividab/tsconfig-paths无法正常工作的原因盒子。因此,在每种情况下都需要检查此路径并找到合适的“..”计数:)
index.ts在最顶部导入这个文件:import './fixTsPaths';
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
328 次 |
| 最近记录: |