|
| 1 | +As you're developing your Ember app, you'll likely run into common scenarios that aren't addressed by Ember itself, |
| 2 | +such as authentication or using SASS for your stylesheets. |
| 3 | +Ember CLI provides a common format called [Ember Addons](#toc_addons) for distributing reusable libraries |
| 4 | +to solve these problems. |
| 5 | +Additionally, you may want to make use of front-end dependencies like a CSS framework |
| 6 | +or a JavaScript datepicker that aren't specific to Ember apps. |
| 7 | + |
| 8 | +## Addons |
| 9 | + |
| 10 | +Ember Addons can be installed using [Ember CLI](http://ember-cli.com/extending/#developing-addons-and-blueprints) |
| 11 | +(e.g. `ember install ember-cli-sass`). |
| 12 | +Addons may bring in other dependencies by modifying your project's `bower.json` file automatically. |
| 13 | + |
| 14 | +You can find listings of addons on [Ember Observer](http://emberobserver.com). |
| 15 | + |
| 16 | +## Other assets |
| 17 | + |
| 18 | +Third-party JavaScript not available as an addon or Bower package should be placed in the `vendor/` folder in your project. |
| 19 | + |
| 20 | +Your own assets (such as `robots.txt`, `favicon`, custom fonts, etc) should be placed in the `public/` folder in your project. |
| 21 | + |
| 22 | +## Compiling Assets |
| 23 | + |
| 24 | +When you're using dependencies that are not included in an addon, |
| 25 | +you will have to instruct Ember CLI to include your assets in the build. |
| 26 | +This is done using the asset manifest file `ember-cli-build.js`. |
| 27 | +You should only try to import assets located in the `bower_components` and `vendor` folders. |
| 28 | + |
| 29 | +### Globals provided by JavaScript assets |
| 30 | + |
| 31 | +The globals provided by some assets (like `moment` in the below example) can be used in your application |
| 32 | +without the need to `import` them. |
| 33 | +Provide the asset path as the first and only argument. |
| 34 | + |
| 35 | +```ember-cli-build.js |
| 36 | +app.import('bower_components/moment/moment.js'); |
| 37 | +``` |
| 38 | + |
| 39 | +You will need to add `"moment"` to the `globals` section in `.eslintrc.js` to prevent ESLint errors |
| 40 | +about using an undefined variable. |
| 41 | + |
| 42 | +### AMD JavaScript modules |
| 43 | + |
| 44 | +Provide the asset path as the first argument, and the list of modules and exports as the second. |
| 45 | + |
| 46 | +```ember-cli-build.js |
| 47 | +app.import('bower_components/ic-ajax/dist/named-amd/main.js', { |
| 48 | + exports: { |
| 49 | + 'ic-ajax': [ |
| 50 | + 'default', |
| 51 | + 'defineFixture', |
| 52 | + 'lookupFixture', |
| 53 | + 'raw', |
| 54 | + 'request' |
| 55 | + ] |
| 56 | + } |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +You can now `import` them in your app. (e.g. `import { raw as icAjaxRaw } from 'ic-ajax';`) |
| 61 | + |
| 62 | +### Environment-Specific Assets |
| 63 | + |
| 64 | +If you need to use different assets in different environments, specify an object as the first parameter. |
| 65 | +That object's key should be the environment name, and the value should be the asset to use in that environment. |
| 66 | + |
| 67 | +```ember-cli-build.js |
| 68 | +app.import({ |
| 69 | + development: 'bower_components/ember/ember.js', |
| 70 | + production: 'bower_components/ember/ember.prod.js' |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +If you need to import an asset in only one environment you can wrap `app.import` in an `if` statement. |
| 75 | +For assets needed during testing, you should also use the `{type: 'test'}` option to make sure they |
| 76 | +are available in test mode. |
| 77 | + |
| 78 | +```ember-cli-build.js |
| 79 | +if (app.env === 'development') { |
| 80 | + // Only import when in development mode |
| 81 | + app.import('vendor/ember-renderspeed/ember-renderspeed.js'); |
| 82 | +} |
| 83 | +if (app.env === 'test') { |
| 84 | + // Only import in test mode and place in test-support.js |
| 85 | + app.import(app.bowerDirectory + '/sinonjs/sinon.js', { type: 'test' }); |
| 86 | + app.import(app.bowerDirectory + '/sinon-qunit/lib/sinon-qunit.js', { type: 'test' }); |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### CSS |
| 91 | + |
| 92 | +Provide the asset path as the first argument: |
| 93 | + |
| 94 | +```ember-cli-build.js |
| 95 | +app.import('bower_components/foundation/css/foundation.css'); |
| 96 | +``` |
| 97 | + |
| 98 | +All style assets added this way will be concatenated and output as `/assets/vendor.css`. |
| 99 | + |
| 100 | +### Other Assets |
| 101 | + |
| 102 | +All assets located in the `public/` folder will be copied as is to the final output directory, `dist/`. |
| 103 | + |
| 104 | +For example, a `favicon` located at `public/images/favicon.ico` will be copied to `dist/images/favicon.ico`. |
| 105 | + |
| 106 | +All third-party assets, included either manually in `vendor/` or via a package manager like Bower, must be added via `import()`. |
| 107 | + |
| 108 | +Third-party assets that are not added via `import()` will not be present in the final build. |
| 109 | + |
| 110 | +By default, `import`ed assets will be copied to `dist/` as they are, with the existing directory structure maintained. |
| 111 | + |
| 112 | +```ember-cli-build.js |
| 113 | +app.import('bower_components/font-awesome/fonts/fontawesome-webfont.ttf'); |
| 114 | +``` |
| 115 | + |
| 116 | +This example would create the font file in `dist/font-awesome/fonts/fontawesome-webfont.ttf`. |
| 117 | + |
| 118 | +You can also optionally tell `import()` to place the file at a different path. |
| 119 | +The following example will copy the file to `dist/assets/fontawesome-webfont.ttf`. |
| 120 | + |
| 121 | +```ember-cli-build.js |
| 122 | +app.import('bower_components/font-awesome/fonts/fontawesome-webfont.ttf', { |
| 123 | + destDir: 'assets' |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +If you need to load certain dependencies before others, |
| 128 | +you can set the `prepend` property equal to `true` on the second argument of `import()`. |
| 129 | +This will prepend the dependency to the vendor file instead of appending it, which is the default behavior. |
| 130 | + |
| 131 | +```ember-cli-build.js |
| 132 | +app.import('bower_components/es5-shim/es5-shim.js', { |
| 133 | + type: 'vendor', |
| 134 | + prepend: true |
| 135 | +}); |
| 136 | +``` |
0 commit comments