Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular library bundle dependencies

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

ng g library MyLibrary

ng build MyLibrary

This gives me the my-libary.umd.js bundle that I need.

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?

How can I do that?

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"
  ]
}
like image 397
Thomas Schneiter Avatar asked Feb 20 '19 08:02

Thomas Schneiter


People also ask

What are bundle dependencies?

If you're not familiar with the idea, module bundling is: [The] process of stitching together a group of modules (and their dependencies) into a single file (or group of files) in the correct order.

What are peer dependencies in Angular library?

Peer dependencieslink This ensures that when modules ask for Angular, they all get the exact same module. If a library lists @angular/core in dependencies instead of peerDependencies , it might get a different Angular module instead, which would cause your application to break.

What is allowedNonPeerDependencies?

"allowedNonPeerDependencies": { 27. "description": "A list of dependencies that are allowed in the 'dependencies' and 'devDependencies' section of package.json.


1 Answers

What you want is to add it to bundledDependencies:

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: The user of your library has to install the dependency (adding it to his own package.json)

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.

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. Remove "ngx-spinner" from the "dependencies"-section of your library-package.json
  2. Remove the node_modules-folder inside your library (if you have one there)
  3. Add "ngx-spinner" as a dependency to your root-package.json
  4. Build the lib
like image 130
dummdidumm Avatar answered Sep 18 '22 16:09

dummdidumm