|
| 1 | +## Addon blueprints |
| 2 | + |
| 3 | +As in applications, custom blueprints are available in addons. Addon blueprints are used to generate code snippets in the client application. Addons can also have a default blueprint that will run automatically after the addon is installed. |
| 4 | + |
| 5 | +Addon blueprints have the same structure as regular blueprints. You should be familiar with [creating blueprints](../../advanced-use/blueprints/) in the `Advanced use` section to understand blueprints before using them in your addon. |
| 6 | + |
| 7 | + The default blueprint has extra hooks to install packages and/or install another Ember addon into the client app. |
| 8 | + |
| 9 | +To create the default blueprint, use `ember generate blueprint <addon-name>` |
| 10 | + |
| 11 | +```javascript {data-filename=my-addon-name/blueprints/my-addon-name/index.js} |
| 12 | +module.exports = { |
| 13 | + normalizeEntityName() {}, // no-op since we're just adding dependencies |
| 14 | +}; |
| 15 | +``` |
| 16 | +### Blueprint Hooks |
| 17 | + |
| 18 | +In addition to the standard blueprint hooks, the default blueprint can use these hooks. |
| 19 | + |
| 20 | +* `addAddonToProject` |
| 21 | +* `addBowerPackageToProject` |
| 22 | +* `addPackageToProject` |
| 23 | + |
| 24 | +#### addAddonToProject |
| 25 | + |
| 26 | +Installs another Ember addon in the client application |
| 27 | + |
| 28 | +#### addPackageToProject |
| 29 | + |
| 30 | +Installs an npm package or dependency into the client application |
| 31 | + |
| 32 | +#### addBowerPackageToProject |
| 33 | + |
| 34 | +Installs a Bower package or dependency into the client application. |
| 35 | +Bower is a package manager that is [no longer recommended for new projects](https://bower.io/), |
| 36 | +but you may find this hook used in older addons. |
| 37 | + |
| 38 | +Each of the hooks returns a promise, so they can all be chained with `.then()`. The following is an example of each of these: |
| 39 | + |
| 40 | +```javascript {data-filename=my-addon-name/blueprints/my-addon-name/index.js} |
| 41 | +module.exports = { |
| 42 | + normalizeEntityName() {}, // no-op since we're just adding dependencies |
| 43 | + |
| 44 | + afterInstall() { |
| 45 | + // Add addons to package.json and run defaultBlueprint |
| 46 | + return this.addAddonsToProject({ |
| 47 | + // a packages array defines the addons to install |
| 48 | + packages: [ |
| 49 | + // name is the addon name, and target (optional) is the version |
| 50 | + { name: 'ember-cli-code-coverage', target: '0.3.9' }, |
| 51 | + { name: 'ember-cli-sass' } |
| 52 | + ] |
| 53 | + }).then(() => { |
| 54 | + // Add npm packages to package.json |
| 55 | + return this.addPackagesToProject([ |
| 56 | + { name: 'babel-eslint' }, |
| 57 | + { name: 'eslint-plugin-ship-shape' } |
| 58 | + ]); |
| 59 | + }); |
| 60 | + } |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +### Blueprint Config |
| 65 | + |
| 66 | +The default blueprint is recognized because it normally has the same name as the addon. Optionally, you may specify a different name for the "defaultBlueprint" in `package.json`: |
| 67 | + |
| 68 | + |
| 69 | +```json {data-filename=my-addon-name/package.json} |
| 70 | +"ember-addon": { |
| 71 | + // addon configuration properties |
| 72 | + "configPath": "tests/dummy/config", |
| 73 | + "defaultBlueprint": "blueprint-that-isnt-package-name", |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Blueprints in development |
| 78 | + |
| 79 | +When developing and testing your addon using either `npm link` or `yarn link` your addon's blueprint will not automatically run. To manually run and test the blue print you would use the following command: |
| 80 | + |
| 81 | +```shell |
| 82 | +ember generate <your-blueprint-name> |
| 83 | +``` |
| 84 | + |
| 85 | +If you specified a different name for the default blueprint, use that name with `ember generate`. |
0 commit comments