本文介绍了Angular库捆绑包依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用CLI创建并捆绑了一个Angular(7.2.0)库:

I've created and bundled an Angular (7.2.0) Library using the CLI:

ng构建MyLibrary

ng build MyLibrary

这给了我所需的 my-libary.umd.js 包.

当前,所有依赖项都作为peerDependencies添加到了package.json库中.我想做的是实际上将某些依赖项与库(.umd)捆绑在一起.将它们添加为"依赖项"而不是" peerDependencies "似乎并没有解决问题,我真的看不出有什么区别吗?

Currently, all dependencies are added as peerDependencies in the library package.json. What I would like to do is to actually bundle some dependencies with the library (.umd). Adding them as "dependencies" instead of "peerDependencies" does not seem to do the trick, I don't really see what the difference is?

我该怎么做?

其中应捆绑ngx-spinner的package.json示例

Example of package.json where ngx-spinner should be bundled

{
  "name": "demo-plugin",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^7.1.0",
    "@angular/core": "^7.1.0"
  },
  "dependecies": {
    "ngx-spinner": "^7.1.4"
  },
  "bundledDependencies": [
    "ngx-spinner"
  ]
}

推荐答案

您想要的是将其添加到 bundledDependencies :

What you want is to add it to bundledDependencies:

依赖项:当其他人使用您的库时,NPM会自动安装它们.此处列出的依赖项也需要在ng-package.json("whitelistedNonPeerDependencies")内列入白名单.

dependencies: NPM automatically installs them when someone else uses your library. Dependencies listed here also need to be whitelisted inside ng-package.json ("whitelistedNonPeerDependencies")

peerDependencies :您的库用户必须安装依赖项(将其添加到自己的package.json中)

peerDependencies: The user of your library has to install the dependency (adding it to his own package.json)

bundledDependencies :构建依赖项时,它将与您的库捆绑在一起.这也将捆绑所有传递依赖项.如果要停止此链,则需要添加不应捆绑到 peerDependencies 的依赖项.因此,例如,如果您要捆绑对B有依赖关系的依赖项A,则会得到一个与A和B捆绑在一起的捆绑包.如果不想捆绑B,则将其添加到 peerDependencies 中.

bundledDependencies: The dependency will be bundled together with your library when building it. This will also bundle all transitive dependencies. If you want to stop this chain, you need to add the dependency which should not be bundled to peerDependencies. So for example if you want to bundle dependency A, which has a dependency on B, you get a bundle with A and B. If you don't want B bundled, you add it to the peerDependencies.

您具有的每个依赖项只能同时出现在其中之一中.要捆绑一个依赖项,因此您需要将此依赖项添加到根目录的package.json(而不是library-package.json)中.您不应该执行的是在库文件夹中运行 npm install .如果您在库文件夹中进行 npm install ,并且在根package.json中没有捆绑的依赖项(因此根node_modules文件夹中没有此依赖项),您将获得成功的构建,但是依赖项将不包含在构建中(cli应该在此暗示一下..).

Each dependency you have should only appear in one of these at the same time. To bundle a dependency, you therefore need to add this dependency to the package.json of the root (not the library-package.json). What you should NOT do is run npm install inside the library folder. If you do npm install inside the library folder and don't have the bundled dependencies in the root package.json (and it's therefore missing from the root node_modules folder), you get a successful build, but the dependencies will not be included in the build (the cli should maybe hint at this ..).

长话短说,针对您的特定问题:

Long story short, for your specific problem:

  1. 从library-package.json的"dependencies"部分中删除"ngx-spinner"
  2. 删除库中的node_modules-folder(如果有的话)
  3. 在您的root-package.json中添加"ngx-spinner"作为依赖项
  4. 构建库

这篇关于Angular库捆绑包依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:11