Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TS Path Mapping with Firebase Cloud Functions

How do I use TS Path Mapping with Firebase Cloud Functions? I tried without success:

"baseUrl": ".",
  "paths": {
    "@custom-path/*": ["src/utils/*"],
    "@other-path/*": ["../other/path/*"]
  }
like image 319
william Avatar asked Jun 25 '18 10:06

william


People also ask

How do I use TypeScript with Firebase?

In the project directory, run firebase init functions and select TypeScript when prompted for a language for writing functions. When prompted whether to overwrite the existing package. json file, select No unless you are sure you don't want to keep the existing file. Delete index.

Are firebase functions async?

Most of Firebase's APIs are asynchronous. This might be confusing at first: you're making a call, for example to fetch some data from Firestore, but you don't get a result back.


2 Answers

Finally I was able to do this with module-alias NPM package.

  1. Install it as non-dev dependency: yarn add module-alias @types/module-alias
  2. Create a file fixTsPaths.ts or whatever with content like this:
import * as ModuleAlias from 'module-alias';

ModuleAlias.addAliases({
    'common': __dirname + '/../../../common',
});

Here's the trick about the path /../../../common: in my case this folder is outside functions, and Typescript replicates folders structure during the build, so that's could be the reason why https://github.com/dividab/tsconfig-paths was not working out of the box. So in every case one needs to check this path and find appropriate '..' count :)

  1. And finally import this file in your index.ts at the very top:
import './fixTsPaths';

Hope this helps!

like image 178
ivandz Avatar answered Nov 16 '22 01:11

ivandz


The problem is the rule no-implicit-dependencies: true on the tslint.json. You can pass additional params to whitelist your custom paths:

"no-implicit-dependencies": [true, ["@custom-path", "@other-path"]],
like image 27
Alexander Baron Avatar answered Nov 16 '22 01:11

Alexander Baron