简体   繁体   English

捆绑和发布 NPM 库。 解析所有依赖项并将它们包含到包中是否很常见?

[英]bundling and publishing an NPM library. Is it common to resolve all dependencies and include them into the bundle?

I've been tasked with the development of a NPM package with a custom component (in this case a react component) that makes uses of other dependencies such as plate, slate, etc.我的任务是开发一个带有自定义组件(在本例中为反应组件)的 NPM 包,该组件使用其他依赖项,例如 plate、slate 等。

I'm in the process of preparing the output dist but it's not clear to me what the best practices are when doing so: Should all dependencies be resolved and bundled into a big.js file or this can be ignored?我正在准备输出dist但我不清楚这样做的最佳实践是什么:是否应该解析所有依赖项并将其捆绑到一个 big.js 文件中,或者可以忽略它? (I'm using rollup resolve here). (我在这里使用汇总解析)。 I'm afraid this would produce a huge file including the source of all the dependencies but as I stated I'm really not familiar with the process...恐怕这会产生一个巨大的文件,包括所有依赖项的来源,但正如我所说,我真的不熟悉这个过程......

In the other hand, is it common NOT to resolve such dependencies and let the final consumer of the component do so?另一方面,不解决此类依赖关系并让组件的最终使用者这样做是否很常见? (I'm only assuming here) (我只是在这里假设)

It's all about pros and cons... and what is possible.这完全是关于利弊……以及什么是可能的。 For example React itself can only exist in one version in an entire project so you should never include that.例如 React 本身在整个项目中只能存在于一个版本中,所以你永远不应该包含它。

Dependencies that are needed but not included should be added as peerDependencies in your package.json and it is the responsibility of the consumer to download them.需要但未包含的依赖项应作为peerDependencies添加到您的package.json中,消费者有责任下载它们。 The downside with including dependencies (as dependencies so that they will be downloaded automatically by the consumer) is that the bundle of the consumer might be bigger than it needs to be.包含依赖项(作为dependencies项以便消费者自动下载)的缺点是消费者的包可能比需要的更大。 Here you should take into account who will consume it;在这里你应该考虑谁会消费它; is it for internal use in your organization or public use?它是供您的组织内部使用还是公共使用? Do you know anything about the context it will be used in?你知道它将被使用的上下文吗? It's best not to include dependencies since it will contribute to smaller resulting bundle for the consumer but if it's unlikely that the depending dependencies are present in the build environment of the consumer, you might as well add it to your package.最好不要包含依赖项,因为它会为消费者生成更小的结果包,但如果依赖项不太可能出现在消费者的构建环境中,您不妨将它添加到您的包中。 The situation that you want to avoid is that your package includes a different version of the same package that the consumer is already using;您要避免的情况是您的包裹包含消费者已经在使用的同一包裹的不同版本; then the resulting bundle may contain two versions of a lot of code that could potentially be reduced to one version (if the version used by the consumer and by your package are compatible).那么生成的包可能包含许多代码的两个版本,这些代码可能会减少到一个版本(如果消费者使用的版本和您的包使用的版本兼容)。 Of course all of this potentially becomes worse and more likely with large common dependencies than with small uncommon dependencies.当然,与小的不常见依赖项相比,所有这一切都可能变得更糟,并且更可能出现大的常见依赖项。

An example: in my organization we use Material-UI.举个例子:在我的组织中,我们使用 Material-UI。 We have a package with React components using Material-UI which we consume in other projects.我们有一个包含使用 Material-UI 的 React 组件的包,我们在其他项目中使用它。 Since Material-UI will always be present in the projects, it's bad practice to include it in the package, even though it will place a higher responsibility on the consumers (us) to align different versions of the package with whatever version of Material-UI that we're using in the applicable project.由于 Material-UI 将始终存在于项目中,因此将其包含在包中是一种不好的做法,尽管这会给消费者(我们)带来更高的责任,使包的不同版本与任何版本的 Material-UI 保持一致我们在适用的项目中使用的。 Given another consumption context, including it in the package might have made more sense.给定另一个消费环境,将其包含在包中可能更有意义。

According to me, you should never bundle your package since it makes tree shaking more complicated for the consumer.根据我的说法,你永远不应该捆绑你的包裹,因为它会让 tree shaking 对消费者来说更加复杂。 This applies to esm packages (cjs is not tree-shakeable).这适用于 esm 包(cjs 不是 tree-shakeable)。 In cjs on the other hand it's devastating with bundled packages since it prevents the consumer from making more specific imports to avoid importing a lot of unused code, eg另一方面,在 cjs 中,捆绑包是毁灭性的,因为它阻止消费者进行更具体的导入以避免导入大量未使用的代码,例如

import Comp from "package/Component"

instead of代替

import { Comp } from "package"

There's almost never a good reason to embed dependencies inside library bundles.几乎没有充分的理由将依赖项嵌入到库包中。 That's how you end up with multiple copies of dependencies in web apps.这就是您最终在 Web 应用程序中获得多个依赖项副本的方式。 At best, web apps' bundles end up being unnecessarily bloated.充其量,网络应用程序的捆绑包最终会变得不必要地臃肿。 At worst, dependencies that break when they're duplicated (eg React) yield all sorts of unexpected behaviour.在最坏的情况下,依赖项在复制时会中断(例如 React),从而产生各种意想不到的行为。

The surefire way to prevent dependency duplication is to avoid bundling libraries at all.防止依赖重复的可靠方法是完全避免捆绑库。 If you inspect your node_modules folder in any of your web app projects, you'll likely find numerous third-party dependencies that aren't bundled.如果您检查任何 Web 应用程序项目中的node_modules文件夹,您可能会发现许多未捆绑的第三方依赖项。 And their lack of bundling doesn't have an impact on your web app.他们没有捆绑不会对您的网络应用程序产生影响。 That proves that library bundling is pointless.这证明库捆绑是毫无意义的。

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

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