对于跨平台的 web/server npm 包,如何根据环境控制加载/构建的 npm 依赖项?

问题描述 投票:0回答:0

我制作了一个本地 npm 包来处理 Angular 和 Firebase 函数(来自 Google 的简单无服务器函数)的 firestore 数据库的写入和读取

作为依赖项,它同时具有 firebase-admin@angular/fire.

//package.json of my local package
{
  "name": "inqompass-repositories",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@angular/fire": "^7.4.1",
    "firebase-admin": "^9.12.0"
  }
}

我将 2 个包组合起来创建联合类型。所以我的包可以同时用于 Angular 和 Serverless 函数:

//Example FirestoreRepository.ts

type CollectionReference<TDocumentData> = firebase.firestore.CollectionReference<TDocumentData> | admin.firestore.CollectionReference<TDocumentData> 
type Batch = firebase.firestore.WriteBatch | admin.firestore.WriteBatch

export class FirestoreRepository<TDocumentData> {

  protected readonly collection: CollectionReference<TDocumentData>
  constructor(
    collection: CollectionReference<TDocumentData>,
  ) {
    this.collection = collection
  }

  async add(documentData: TDocumentData, batch?: Batch) {
      const document = this.collection.doc()
      batch.set(document, documentData)
      return this.collection.add(documentData)
    }
  }

问题是,firebase-admin 需要一些在浏览器中显然不可用的 nodejs 核心包(根据我的理解)。 firestore-admin 应该只用于服务器。

所以,当我尝试构建我的角度项目时。我收到这样的错误:

../shared/repositories/node_modules/teeny-request/build/src/index.js:26:17-34 - Error: Module not found: Error: Can't resolve 'stream' in '/home/ender/WGMM/InQompass/shared/repositories/node_modules/teeny-request/build/src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
        - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "stream": false }

../shared/repositories/node_modules/teeny-request/build/src/index.js:83:15-37 - Error: Module not found: Error: Can't resolve 'querystring' in '/home/ender/WGMM/InQompass/shared/repositories/node_modules/teeny-request/build/src'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "querystring": require.resolve("querystring-es3") }'
        - install 'querystring-es3'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "querystring": false }

我看到的最干净的解决方案是,当我的包用于 Angular 项目时,不加载或构建它的 firebase-admin 依赖项。

firebase-admin 仅用于服务器端,我不想将它用于我的角度项目。它在我的包中的原因仅适用于我的包用于服务器中运行的 firebase 函数的情况。

只需加载一个包即可完美运行包

所以我的问题是:

我如何告诉我的 Angular 项目,在使用我的本地包时不要尝试加载或构建 firebase-admin 依赖项?

附加信息:这个包的目标是集中代码库与 firestore 数据库进行通信。因为写和读都可以从前端和后端完成。因此,例如,用户在我的整个解决方案中都是以相同的方式创建的。

typescript npm angularfire firebase-admin npm-package
© www.soinside.com 2019 - 2024. All rights reserved.