简体   繁体   中英

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"
  ]
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM