手把手教你学习ROR-3.asset的那些事

手把手教你学习ROR-3.asset的那些事

1 What is asset pipeline?

Asset pipeline is the whole process of how Rails  handle with css,javascript and image...

2 How to use asset pipeline?

A enable/disable asset pipeline
The asset pipeline is a core feature pf Rails.So you can just use it.And you also can disabled it.
It can be disabled in config/application.rb by putting this line inside the application class definition:
config.assets.enabled = false

B Which folder to put images,javascript and styleshheets?
1)in subdirectories of public,that means static files by the application.2)In the directory of app/assets.
In production, Rails precompiles these files to public/assets by default.

C)Compress the file
Rails.application.config.assets.compress=true
或者
config/environments/development.rb
config.assets.debug = false

D)application.js and application.css
application.js
//= require jquery
//= require_tree .
require jquery: tell Sprockets the files that you wish to require jquery.js.
The require_tree:所有跟application.js同一个目录的js文件.
application.css
/*
*= require_self
*= require_tree .
*/

E)pipeline
app/assets/stylesheets/projects.css.scss.erb is first processed as ERB, then SCSS, and finally served as CSS.
app/assets/javascripts/projects.js.coffee.erb is processed as ERB, then CoffeeScript, and served as JavaScript.

3 What is the benefit of asset pipeline?

A 能把所有的javascript file打包成一个master.js,把所有的css file打包成master.css.冰球在执行cache:true的情况下,
rails能够通过fingerprint来判断是否需要更新文件。

B 通过去除空格,注释来压缩Js,CSS文件,从而达到最小化的效果。

C 更多的选择性。你不仅可以使用javascript,你还可以使用CoffeeScript。你也可以Sass等其他language。


4 Asset Organization

app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.

5 Some configuration

rails new appname --skip-sprockets

config.assets.digest : The fingerprinting behavior is controlled by the setting of config.assets.digest setting in Rails (which defaults to true for production and false for everything else)

原文地址:https://www.cnblogs.com/SoulSpirit/p/3337124.html