|
| 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 `package.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 npm 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 `node_modules` and `vendor` folders. `bower_components` also still |
| 28 | +works, but is recommended against, unless you have no other choice. Even bower recommends not to use itself anymore. |
| 29 | + |
| 30 | +### Globals provided by JavaScript assets |
| 31 | + |
| 32 | +The globals provided by some assets (like `moment` in the below example) can be used in your application |
| 33 | +without the need to `import` them. |
| 34 | +Provide the asset path as the first and only argument. |
| 35 | + |
| 36 | +```javascript {data-filename=ember-cli-build.js} |
| 37 | +app.import('node_modules/moment/moment.js'); |
| 38 | +``` |
| 39 | + |
| 40 | +You will need to add `"moment"` to the `globals` section in `.eslintrc.js` to prevent ESLint errors |
| 41 | +about using an undefined variable. |
| 42 | + |
| 43 | +### Anonymous AMD JavaScript modules |
| 44 | + |
| 45 | +You can transform an anonymous AMD module to a named one by using the `amd` transformation. |
| 46 | + |
| 47 | +```javascript {data-filename=ember-cli-build.js} |
| 48 | +app.import('node_modules/moment/moment.js', { |
| 49 | + using: [ |
| 50 | + { transformation: 'amd', as: 'moment' } |
| 51 | + ] |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +This transformation allows you to `import` moment in your app. (e.g. `import moment from 'moment';`) |
| 56 | + |
| 57 | +### CommonJS JavaScript modules |
| 58 | + |
| 59 | +[ember-cli-cjs-transform](https://github.com/rwjblue/ember-cli-cjs-transform) allows us to import CommonJS modules into |
| 60 | +our Ember app. It also does auto-rollup and some nice caching, so it should pull in all the deps that are pulled in |
| 61 | +with `require` for you automatically. It is not yet included with ember-cli by default, so you will need to install it. |
| 62 | + |
| 63 | +```bash |
| 64 | +ember install ember-cli-cjs-transform |
| 65 | +``` |
| 66 | + |
| 67 | +```javascript {data-filename=ember-cli-build.js} |
| 68 | +app.import('node_modules/showdown/dist/showdown.js', { |
| 69 | + using: [ |
| 70 | + { transformation: 'cjs', as: 'showdown' } |
| 71 | + ] |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +You can now `import` them in your app. (e.g. `import showdown from 'showdown';`) |
| 76 | + |
| 77 | +### Environment-Specific Assets |
| 78 | + |
| 79 | +If you need to use different assets in different environments, specify an object as the first parameter. |
| 80 | +That object's key should be the environment name, and the value should be the asset to use in that environment. |
| 81 | + |
| 82 | +```javascript {data-filename=ember-cli-build.js} |
| 83 | +app.import({ |
| 84 | + development: 'node_modules/moment/moment.js', |
| 85 | + production: 'node_modules/moment/min/moment.min.js' |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +If you need to import an asset in only one environment you can wrap `app.import` in an `if` statement. |
| 90 | +For assets needed during testing, you should also use the `{type: 'test'}` option to make sure they |
| 91 | +are available in test mode. |
| 92 | + |
| 93 | +```javascript {data-filename=ember-cli-build.js} |
| 94 | +if (app.env === 'development') { |
| 95 | + // Only import when in development mode |
| 96 | + app.import('vendor/ember-renderspeed/ember-renderspeed.js'); |
| 97 | +} |
| 98 | +if (app.env === 'test') { |
| 99 | + // Only import in test mode and place in test-support.js |
| 100 | + app.import('node_modules/sinonjs/sinon.js', { type: 'test' }); |
| 101 | + app.import('node_modules/sinon-qunit/lib/sinon-qunit.js', { type: 'test' }); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### CSS |
| 106 | + |
| 107 | +Provide the asset path as the first argument: |
| 108 | + |
| 109 | +```javascript {data-filename=ember-cli-build.js} |
| 110 | +app.import('node_modules/foundation/css/foundation.css'); |
| 111 | +``` |
| 112 | + |
| 113 | +All style assets added this way will be concatenated and output as `/assets/vendor.css`. |
| 114 | + |
| 115 | +### Other Assets |
| 116 | + |
| 117 | +All assets located in the `public/` folder will be copied as is to the final output directory, `dist/`. |
| 118 | + |
| 119 | +For example, a `favicon` located at `public/images/favicon.ico` will be copied to `dist/images/favicon.ico`. |
| 120 | + |
| 121 | +All third-party assets, included either manually in `vendor/` or via a package manager like npm, must be added via `import()`. |
| 122 | + |
| 123 | +Third-party assets that are not added via `import()` will not be present in the final build. |
| 124 | + |
| 125 | +By default, `import`ed assets will be copied to `dist/` as they are, with the existing directory structure maintained. |
| 126 | + |
| 127 | +```javascript {data-filename=ember-cli-build.js} |
| 128 | +app.import('node_modules/font-awesome/fonts/fontawesome-webfont.ttf'); |
| 129 | +``` |
| 130 | + |
| 131 | +This example would create the font file in `dist/font-awesome/fonts/fontawesome-webfont.ttf`. |
| 132 | + |
| 133 | +You can also optionally tell `import()` to place the file at a different path. |
| 134 | +The following example will copy the file to `dist/assets/fontawesome-webfont.ttf`. |
| 135 | + |
| 136 | +```javascript {data-filename=ember-cli-build.js} |
| 137 | +app.import('node_modules/font-awesome/fonts/fontawesome-webfont.ttf', { |
| 138 | + destDir: 'assets' |
| 139 | +}); |
| 140 | +``` |
| 141 | + |
| 142 | +If you need to load certain dependencies before others, |
| 143 | +you can set the `prepend` property equal to `true` on the second argument of `import()`. |
| 144 | +This will prepend the dependency to the vendor file instead of appending it, which is the default behavior. |
| 145 | + |
| 146 | +```javascript {data-filename=ember-cli-build.js} |
| 147 | +app.import('node_modules/es5-shim/es5-shim.js', { |
| 148 | + type: 'vendor', |
| 149 | + prepend: true |
| 150 | +}); |
| 151 | +``` |
0 commit comments