简体   繁体   English

如何将 package 分成模块? (如 Firebase SDK)

[英]How to separate a package into modules? (Like Firebase SDK)

If you download Firebase JS SDK you only need to add one dependency: firebase如果下载Firebase JS SDK只需要添加一个依赖: firebase

From there you can access all different modules based on your import attributes:从那里您可以根据您的导入属性访问所有不同的模块:

import { initializeApp } from 'firebase/app';
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';

And then any library that won't be used is automatically removed if you use any tree-shaking tool.然后,如果您使用任何摇树工具,任何不会使用的库都会被自动删除。

How can I achieve something similar?我怎样才能实现类似的目标? I'm looking to build a collection of packages for our organization so that, instead of having everyone working and updating 15 packages, having a single package that can be divided into modules/imports so a project only imports the necessary modules.我正在为我们的组织构建一个包集合,这样,不是让每个人都工作和更新 15 个包,而是拥有一个可以分为模块/导入的 package,因此项目只导入必要的模块。

I have been looking for some days and I don't even know how to name this approach.我一直在寻找几天,我什至不知道如何命名这种方法。

If anyone could link me to a tutorial, a document or anything to be able to start my investigation, I would appreciate it a lot.如果有人可以将我链接到教程、文档或任何能够开始我的调查的内容,我将不胜感激。

Thank you.谢谢你。

There's nothing particular to do: it's just natural.没有什么特别的事情要做:这很自然。

For example, the following structure:例如下面的结构:

foo
├── bar
│   ├── baz
│   │   └── index.js
│   └── index.js
├── fum
│   ├── index.js
│   └── qux
│       └── index.js
├── index.js
└── package.json

Allows the following usage:允许以下用法:

import foo from './foo';
import foobar from './foo/bar';
import foobarbaz from './foo/bar/baz';
import foofum from './foo/fum';
import foofumqux from './foo/fum/qux';

For testing purposes, consider each js file exports a string containing the concatenated value of all its parent folder names, eg ./foo/bar/baz/index.js contains export default 'foobarbaz';出于测试目的,考虑每个js文件导出一个包含其所有父文件夹名称的串联值的字符串,例如./foo/bar/baz/index.js包含export default 'foobarbaz'; , while package.json contains { "type": "module" } . ,而package.json包含{ "type": "module" }

Then, the following code:然后,下面的代码:

console.log({
    foo,
    foobar,
    foobarbaz,
    foofum,
    foofumqux
});

Outputs:输出:

{
  foo: 'foo',
  foobar: 'foobar',
  foobarbaz: 'foobarbaz',
  foofum: 'foofum',
  foofumqux: 'foofumqux'
}

If you check out the Firebase JS SDK repository, you can see that they use a monorepo to manage all of this, and they are actually all separate packages.如果您查看 Firebase JS SDK 存储库,您可以看到他们使用 monorepo 来管理所有这些,它们实际上都是单独的包。 They use Lerna to manage their monorepo - but there are several monorepo tools out there (npm workspaces, yarn).他们使用Lerna来管理他们的 monorepo - 但是那里有几个 monorepo 工具(npm 工作区,yarn)。 I like npm workspaces myself since it's native.我自己喜欢 npm 工作区,因为它是原生的。

What they do is they make heavy use of exported modules in npm.他们所做的是大量使用 npm 中的导出模块。 You can see the documentation for that here: https://nodejs.org/api/packages.html#exports您可以在此处查看相关文档: https://nodejs.org/api/packages.html#exports

So they have a repository that looks vaguely like this:所以他们有一个看起来像这样模糊的存储库:

my-project
├── packages
│   ├── pkg-1
│   │   ├── package.json
│   │   └── index.js
│.  ├── pkg-2
│   │   ├── package.json
│   │   └── index.js
│   └── all
│.      ├── pkg-1
│       │   └── index.js
│.      ├── pkg-2
│       │   └── index.js
│       ├── package.json
│       └── index.js
└── package.json

The key part of the root package.json is the declaration of packages: root package.json 的关键部分是包的声明:

{
  ...
  packages: [
    "packages/*",
  ],
  ...
}

Sample package.json from a package:样品 package.json 来自 package:

{
  "name": "@my-project/pkg-1",
  "main": "index.js",
  ...
}

The trick here is where they have an extra package which just exports all of the other packages in the repo:这里的诀窍是他们有一个额外的 package ,它只是导出 repo 中的所有其他包:

my-project/packages/all/package.json:我的项目/包/所有/package.json:

{
  "name": "my-project"
  "exports": {
    "./pkg-1": "./pkg-1/index.js",
    "./pkg-2": "./pkg-2/index.js",
  }
  "dependencies": [
    "@my-project/pkg-1": "*",
    "@my-project/pkg-2": "*",
  ],
  ...
}

example /my-project/packages/all/pkg-1/index.js:例如 /my-project/packages/all/pkg-1/index.js:

export * from "@my-project/pkg-1"

Now when someone tries to install your package all , they will get all of the other packages aliased.现在,当有人尝试安装您的 package all时,他们将获得所有其他包的别名。

import * as pkg1 from "all/pkg-1"

They also bundle together ES modules and CommonJS so you can use "import" or "require".它们还将 ES 模块和 CommonJS 捆绑在一起,因此您可以使用“import”或“require”。 Take a long hard look at their package.json files in their repository, it's a work of art!仔细查看他们存储库中的 package.json 文件,这是一件艺术品! https://github.com/firebase/firebase-js-sdk https://github.com/firebase/firebase-js-sdk

TLDR; TLDR; In your package.json add the files property.在您的 package.json 添加文件属性。 This will expose the directories that you specify.这将公开您指定的目录。 This will also make them accessible by path as you seek.这也将使它们在您寻找时可以通过路径访问。

{
  "name": "mybase",
  "main": "index.js",
  "files": ["app/**", "store/**"]
}

With a config like above you would be able to access a file like this:使用上面的配置,您将能够访问这样的文件:

import { initializeApp } from 'mybase/app';
import { getFirestore, collection, getDocs } from 'mybase/store/lite';

Just make sure that the files exist as @KaKi87 mentioned.只需确保文件如@KaKi87 所述存在即可。 Specifying the files property in your package.json is key to exposing your file structure to client apps.在 package.json 中指定 files 属性是向客户端应用程序公开文件结构的关键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 flutter firebase/firestore 实现和提供程序分离到一个独立的 package 或库中? - How can I separate my flutter firebase/firestore implementation and providers to a independent package or library? 使用 firebase unity sdk firestore package 时 android 设备出错 - Error on android device when using firebase unity sdk firestore package 如何使用 Node.js 和“firebase-admin”SDK 查询 Firebase? - How to query Firebase with Node.js and "firebase-admin" SDK? 如何使用 Firebase 9(模块化 sdk)更新 Firebase 实时数据库中的数据 - How to update data in Firebase realtime database using Firebase 9 (modular sdk) 无法在 node.js admin sdk 中使用 firebase documentId()(firebase-admin npm package 语法问题) - Unable to use firebase documentId() in node js admin sdk (firebase-admin npm package grammar issue) 如何在 Flutter 中使用 Firebase Admin SDK? - How to use Firebase Admin SDK in Flutter? Swift Package 未版本化包的管理器问题(例如:firebase-ios-sdk) - Swift Package Manager issues with unversioned Packages (example: firebase-ios-sdk) 如何处理 Go 的 firebase admin sdk 错误? - How to handle errors with firebase admin sdk for Go? 如何使用 firebase 管理员 SDK 创建具有社交提供者的租户? - How can I create a tenant with social provider with firebase admin SDK? 如何将firebase sdk添加到最新的android studio version 2022? - How to add firebase sdk to latest android studio version 2022?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM