官术网_书友最值得收藏!

Asset compilation

In this recipe we'll take a look at how assets are added to a project.

How to do it...

In your application, at some point, you may want to add assets and minimize or fingerprint your project. This is done in the root folder of your project in the ember-cli-build.js file or in the asset folder.

CSS and assets

All the assets should be placed in the public/assets folder. The assets can be referred throughout the program at assets/images/{image file}. CSS files should be placed in the app/styles folder.

Minifying

By default, CSS and JavaScript files are minified during the production build process. There are ways to turn this functionality on and off. For example, let's say that you want to turn off the minification for both CSS and JavaScript. To do this, we can simply edit the ember-cli-build.js file, and under the // Add options here section, add the minifyCSS and minifyJS section:

module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
    // Add options here
    minifyCSS: {
      enabled: false
    },
    minifyJS: {
      enabled: false
    }
  });

This will tell the compiler not to minify JavaScript and CSS. To build the application in the production mode, simply use the --environment argument:

$ ember build --enviroment=production
Fingerprinting

All files by default will be fingerprinted during the production build process. This will include all js, css, png, jpg, and gif assets. During this process, all these files will have an md5 checksum appended at the end of their filenames. During this process, all HTML and css files will be rewritten to include these new names.

There are several options available when fingerprinting a file. This is all controlled in the ember-cli-build.js file. Let's suppose that you wanted to disable fingerprinting:

…
fingerprint: {
 enabled: false
}
…

Another useful option is to prepend a domain to all static files. This can be done using the prepend option. Once again, this needs to be added to the ember-cli-build.js file in the root of the application folder:

…
fingerprint: {
   prepend: 'http://www.example.com'
}

Now, all assets will include the www.example.com domain. For example, a normal JavaScript src file will look like this:

<script src="assets/script.js">

This will be transformed into the following:

<script src="http://www.example.com/script-12324adfasdf123234.js">

Another useful option is exclude. This accepts an array of strings. Any filename in the exclude array will not be fingerprinted:

fingerprint: {
  exclude: ['fonts/12424']
}

The ignore option also accepts an array of strings. Any filename that contains any item in the ignore array will not be processed or fingerprinted:

fingerprint: {
  ignore: ['fonts/12424']
}

The extension option defaults to 'js', 'css', 'png', 'jpg', 'gif', and 'map'. This option can be used to add other file types to get fingerprinted:

fingerprint: {
  extension: ['r3','html']
}

The replaceExtensions option defaults to 'html', 'css', and 'js'. If needed, new file types can be added to replace source code with new checksum file names:

fingerprint: {
  replaceExtensions: ['html','htm']
}

How it works...

The import process is done via the Broccoli asset pipeline library. This build tool performs all the fingerprinting, minifying, and importing of the assets. In addition, Broccoli handles all the preprocessors if the appropriate plugins are installed.

The asset manifest is located in the ember-cli-build.js file in the root of the project folder. You can only import assets that are in the bower_components or vendor directories.

主站蜘蛛池模板: 惠水县| 南溪县| 昌宁县| 如皋市| 义乌市| 文登市| 将乐县| 抚州市| 含山县| 鄂尔多斯市| 恩施市| 新化县| 南城县| 泊头市| 达孜县| 荔波县| 鸡东县| 武威市| 蓬莱市| 五原县| 且末县| 上杭县| 子长县| 武陟县| 繁峙县| 赤峰市| 广水市| 和政县| 许昌市| 简阳市| 保德县| 浦城县| 澄江县| 外汇| 易门县| 若尔盖县| 佛教| 葵青区| 雷山县| 江津市| 宁都县|