|
| 1 | +# 3rd party libraries |
| 2 | + |
| 3 | +### Table of Contents |
| 4 | + |
| 5 | +* [A. Consumer Guide: Using `ng install` to prepare your application to use a 3rd party library](#a-consumer-guide-installing-a-3rd-party-library) |
| 6 | +* [B. Publisher Guide: Preparing your library for use by angular-cli](#b-publisher-guide-preparing-your-library) |
| 7 | + |
| 8 | +## A. Consumer Guide: Installing a 3rd party library |
| 9 | + |
| 10 | +Prerequisites: **You will first want to make sure the library adheres to the Publisher Guide** |
| 11 | + |
| 12 | +How do I find this out? |
| 13 | + |
| 14 | +1. Find the root file in the 3rd party library repo you are interested in |
| 15 | +2. Take a look at the root file (should be the same name as the repo itself in most cases) |
| 16 | +3. If you see `export default { ...some angular2 metadata... }` at the bottom, then the chances are high it's compatible |
| 17 | +4. If you don't see it, [click here](#suggest-a-repo-adhere-to-the-publisher-guide) to copy/paste a feature request to post on the library repo |
| 18 | + |
| 19 | +### Ok I'm ready, let's do this! |
| 20 | + |
| 21 | +For example purposes, we will use [ng2-cli-test-lib](https://github.com/NathanWalker/ng2-cli-test-lib) here, however you would substitute the name of the library you want to install. |
| 22 | + |
| 23 | +``` |
| 24 | +$ ng install ng2-cli-test-lib |
| 25 | +Installing 3rd party package: ng2-cli-test-lib... |
| 26 | +Package successfully installed. |
| 27 | +[?] Inject the installed package into your app? (Y/n) Y |
| 28 | +[?] Customize the injection of ng2-cli-test-lib? (Y/n) Y |
| 29 | +[?] What would you like to inject from ng2-cli-test-lib? |
| 30 | +1 Directive |
| 31 | +2 Pipe |
| 32 | +3 Provider |
| 33 | +4 styleUrl |
| 34 | +----------- |
| 35 | +q Quit |
| 36 | +
|
| 37 | +Enter value: 1 |
| 38 | +[?] Which Directive would you like to inject? |
| 39 | +1 TestDirective |
| 40 | +Enter value: 1 |
| 41 | +
|
| 42 | +[?] Where would you like to inject it? |
| 43 | +1 ~/project/src/app.ts |
| 44 | +2 ~/project/src/app/project.spec.ts |
| 45 | +3 ~/project/src/app/project.ts |
| 46 | +Enter value: 3 |
| 47 | +Injecting Directive (TestDirective) to ~/project/src/app/project.ts |
| 48 | +Successfully injected. |
| 49 | +
|
| 50 | +[?] What would you like to inject from ng2-cli-test-lib? |
| 51 | +1 Directive |
| 52 | +2 Pipe |
| 53 | +3 Provider |
| 54 | +4 styleUrl |
| 55 | +----------- |
| 56 | +q Quit |
| 57 | +
|
| 58 | +Enter value: q |
| 59 | +
|
| 60 | +[?] Inject providers into bootstrap script? (Y/n) Y |
| 61 | +[?] Path to the file which bootstraps your app? ~/project/src/app.ts |
| 62 | +Providers imported in ~/project/src/app.ts |
| 63 | +Done. |
| 64 | +``` |
| 65 | + |
| 66 | +You library is now successfully installed and injected into your project. |
| 67 | + |
| 68 | +In this example we chose to inject a `Directive`. |
| 69 | +Specifically we chose `TestDirective` to be injected into `~/project/src/app/project.ts`. |
| 70 | +Upon quitting, we were given the opportunity to inject providers into our bootstrap script. We chose `Y(es)` and specified `~/project/src/app.ts`. Providers were then injected into our bootstrap script. |
| 71 | + |
| 72 | +However, if we don't want that the library to auto-inject anything, we can just answer `N(o)` to the first question. |
| 73 | + |
| 74 | +Example: |
| 75 | + |
| 76 | +````shell |
| 77 | +$ ng install ng2-cli-test-lib |
| 78 | +Installing 3rd party package: ng2-cli-test-lib... |
| 79 | +Package successfully installed. |
| 80 | +[?] Inject the installed package into your app? (Y/n) n |
| 81 | +Done. |
| 82 | +```` |
| 83 | + |
| 84 | +## B. Publisher Guide: Preparing your library |
| 85 | + |
| 86 | +A few advantages to preparing your library for use by angular-cli: |
| 87 | + |
| 88 | +* It does not affect how your library would be consumed manually |
| 89 | +* Provides a standard format in which your library can be consumed |
| 90 | +* Creates a sense of familiarity and ease of use for developers |
| 91 | +* Allows for rapid development with your library for projects that are under very tight time constraints |
| 92 | +* Provides a way for developers to easily uninstall your library when the need arises |
| 93 | + |
| 94 | +#### Step 1 |
| 95 | + |
| 96 | +Create a file in the root of the library project to represent the entry point, usually named to match the repo/library name. |
| 97 | + |
| 98 | +#### Step 2 |
| 99 | + |
| 100 | +Add a `default` export at the bottom with the angular2 metadata that your library provides |
| 101 | +For example: |
| 102 | +[https://github.com/NathanWalker/ng2-cli-test-lib/blob/master/ng2-cli-test-lib.ts](https://github.com/NathanWalker/ng2-cli-test-lib/blob/master/ng2-cli-test-lib.ts) |
| 103 | + |
| 104 | +#### Step 3 |
| 105 | + |
| 106 | +At the time of writing, we still need a bundler script to create bundles that will then be included into our app. |
| 107 | +Here is an example of a bundler script which uses `systemjs-builder`, which we tested and works well. |
| 108 | +````js |
| 109 | +var pkg = require('./package.json'); |
| 110 | +var path = require('path'); |
| 111 | +var Builder = require('systemjs-builder'); |
| 112 | +var name = pkg.name; |
| 113 | + |
| 114 | +var builder = new Builder(); |
| 115 | +var config = { |
| 116 | + baseURL: '.', |
| 117 | + transpiler: 'typescript', |
| 118 | + typescriptOptions: { |
| 119 | + module: 'cjs' |
| 120 | + }, |
| 121 | + map: { |
| 122 | + typescript: './node_modules/typescript/lib/typescript.js', |
| 123 | + angular2: path.resolve('node_modules/angular2'), |
| 124 | + rxjs: path.resolve('node_modules/rxjs') |
| 125 | + }, |
| 126 | + paths: { |
| 127 | + '*': '*.js' |
| 128 | + }, |
| 129 | + meta: { |
| 130 | + 'node_modules/angular2/*': { build: false }, |
| 131 | + 'node_modules/rxjs/*': { build: false } |
| 132 | + }, |
| 133 | +}; |
| 134 | + |
| 135 | +builder.config(config); |
| 136 | + |
| 137 | +builder |
| 138 | +.bundle(name, path.resolve(__dirname, 'bundles/', name + '.js')) |
| 139 | +.then(function() { |
| 140 | + console.log('Build complete.'); |
| 141 | +}) |
| 142 | +.catch(function(err) { |
| 143 | + console.log('Error', err); |
| 144 | +}); |
| 145 | +```` |
| 146 | + |
| 147 | +Just include this script in the root of your app named like `bundler-script.js` and run it before publishing your library to `npm`. |
| 148 | + |
| 149 | +Note that when TypeScript version 1.8 is out of `beta`, this script will no longer be needed and this documentation will be updated. |
| 150 | + |
| 151 | +#### Step 4 |
| 152 | + |
| 153 | +Bump version, compile and republish. |
| 154 | + |
| 155 | +#### Step 5 |
| 156 | + |
| 157 | +There is no step 5. It's that easy. Ok wait a second, what?! |
| 158 | +Let's break down the entry file mentioned above. |
| 159 | + |
| 160 | +``` |
| 161 | +// We import these so we can reference them in the 'default' export below |
| 162 | +import {TestDirective} from './src/app/directives/test.directive'; |
| 163 | +import {TestService} from './src/app/providers/test.provider'; |
| 164 | +import {TestService2} from './src/app/providers/test2.provider'; |
| 165 | +import {TestPipe} from './src/app/pipes/test.pipe'; |
| 166 | +import {TestStyles} from './src/app/test.styles'; |
| 167 | +
|
| 168 | +// We export everything we want developers to be able to use manually |
| 169 | +export * from './src/app/directives/test.directive'; |
| 170 | +export * from './src/app/providers/test.provider'; |
| 171 | +export * from './src/app/providers/test2.provider'; |
| 172 | +export * from './src/app/pipes/test.pipe'; |
| 173 | +export * from './src/app/test.styles'; |
| 174 | +
|
| 175 | +// This is the magic. |
| 176 | +// Provides a standard way to export your library to allow angular-cli to help developers setup your library |
| 177 | +// Please note: keys are optional. Your library can provide any metadata is provides. |
| 178 | +export default { |
| 179 | + directives: [TestDirective], |
| 180 | + pipes: [TestPipe], |
| 181 | + providers: [TestService, TestService2], |
| 182 | + styles: TestStyles.styles(), |
| 183 | + styleUrls: ['src/app/css/test.css'] |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +This would allow angular-cli to auto-annotate all relevant angular metadata that your library provides if the consumer chose to auto-inject. |
| 188 | +You can study this example library which provides this complete example: |
| 189 | +https://github.com/NathanWalker/ng2-cli-test-lib |
| 190 | + |
| 191 | +### Suggest a repo adhere to the Publisher Guide |
| 192 | + |
| 193 | +1. Create a new issue on the lib repo |
| 194 | +2. Copy/paste this subject: `Please add a default export to support angular-cli auto install feature` |
| 195 | +3. Copy/paste this message for the body: |
| 196 | + |
| 197 | +> It would be great if this library provided a `default` export object as described [here in the angular-cli Publisher's Guide](https://github.com/angular/angular-cli/blob/master/docs/ng-install.md#b-publisher-guide-preparing-your-library). Our project would greatly benefit from it and it's very easy to do. Please comment back if that would be possible. If you won't have time in the next couple days, I will try to submit a PR to provide this compatibility soon. Thank you! |
| 198 | +
|
| 199 | +## CREDITS |
| 200 | + |
| 201 | +Using the `default` export was a brilliant idea from an angular core team member, @robwormald, so huge thanks to him. More background here: https://github.com/angular/angular-cli/issues/96 |
0 commit comments