Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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:

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.

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

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

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

functions/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"
  ]
}
like image 289
d-_-b Avatar asked Apr 26 '20 16:04

d-_-b


1 Answers

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.

Not the best approach, but it works.

like image 99
d-_-b Avatar answered Oct 26 '22 20:10

d-_-b