- 论坛徽章:
- 0
|
Asset Pipeline in rails 3.1.0 (2).................
4.2 在线编译
某些环境可能需要用在线编译模式(即无预编译过程).所有的资源请求直接由sprockets调用对应的pipeline进行解析编译,可以通过以下选项启动在线编译:
<textarea readonly name="code" class="ruby">config.assets.compile= true</textarea>第一次请求时资源会被编译并缓存,同开发模式相同原理,同时manifest文件的资源会解析成带MD5的资源文件被页面引用.
Sprockets同时在http的header设置了cache-control为max-age=3153600.表示资源文件可以在服务器及客户端缓存1年.这样可以减少从服务器请求相同的资源,客户端直接从本地取得资源,速度更快.
这种模式将会耗费更多的内存资源,性能会比默认方式差.
在生产模式下,如果没有javascript运行时环境,可以用以下作为默认运行时环境:
<textarea readonly name="code" class="ruby">group :production do
gem'therubyracer'
end</textarea>[size=16px;]5. 自定义pipeline解释器
[size=16px;]5.1 css 压缩器
可以用YUI来压缩css文件.YUI扩展css语法,支持语法缩小.可以设置以下选项提供支持:
<textarea readonly name="code" class="ruby">config.assets.css_compressor= :yui</textarea>必须设置config.assets.compress= true以支持css压缩.
5.2 js压缩器
有三个选项支持js压缩,:closur, :uglifier, :yui, 分别需要closure-compiler,uglifier及yui-compressor插件的支持.
Rails3.1默认使用uglifier,些插件封装了UglifierJS (NodeJS)的ruby实现.压缩主要是去空格及魔术代码串,尽可能取代if和else语法峦三目操作符.可以定义如下选项:
<textarea readonly name="code" class="ruby">config.assets.js_compressor= :uglifier</textarea>必须设置config.assets.compress=true来启动js压缩.
注意:uglifier需要ExecJS运行时环境的支持.如果os环境是Mac或windows,则需要安装js的运行时环境.可以参考ExecJS官方相关的文档.
5.3 自定义压缩类
可以自定义一个类来支持css和js的压缩.此类必须实现compress方法,输入为一个串,输出为压缩后的串:- <textarea readonly name="code" class="ruby">class Transformer
- defcompress(string)
- do_something_returning_a_string(string)
- end
- end </textarea>可以在application.rb中启动自定义的压缩类:<textarea readonly name="code" class="ruby">config.assets.css_compressor= Transformer.new</textarea><br>[size=16px;]5.4 切换资源文件根路径[/size]
- 默认是/assets路径,可以配置换成其他路径:
- <textarea readonly name="code" class="ruby">config.assets.prefix= "/some_other_path"</textarea>主要是考虑到rails3.1以前的资源文件使用的路径不是assets,方便扩展.
- 5.5 X-Sendfile Headers
- X-Sendfileheader为web服务定义一些请求参数,以便web服务器忽略应用服务器的响应,而用请求头所指定的文件进行替代.这个选项默认是禁用的,如果web服务器支持,则可以启动些选项.一旦启动,web服务器可以直接从响应头中分析取出输入的资源文件,使响应更快.
- apache和nginx运载X-Sendfileheader,可以在config/environments/production.rb设置:
- <textarea readonly name="code" class="ruby">#config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
- #config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' #for nginx
- </textarea>注意:可以将这个选项设置在production.rb下,用于生产环境下才有效,而开发和测试环境忽略X-Sendfileheader,对开发有利(比如错误提示信息在开发环境可见,在生产环境不可见).
- 6. How Caching Works
- Sprocketsuses the default Rails cachestore to cache assets in development andproduction.
- TODO:Addmore about changing the default store.
- (Sprockets默认在开发和生产环境使用rails缓存机制来缓存assets资源.)
- 7.为你的Gem加入assets支持
- Assets资源可以从gem中获得.
- 比如jquery-rails插件提供了rails标准js库的支持.插件包含了rails引擎,继承自Rails::Engine,这样它里面的app/assets,lib/assets, vendor/assets目录便可以直接被rails辨识出来,sprockets可以直接读到到相关路径的资源文件.
- 8. Making Your Library or Gem a Pre-Processor
- TODO:Registering gems on Tilt enablingSprockets to find them.
- 9. 更新到新版本的rails
- 更新过程需要注意两个问题:
- 首先要将更变资源文件集的路径.不同类型的文件js,css等移动到不同的位置;其次要重新设置一系列的系统选项,以下是rails3.1.0会添加以下选项:
- config/application.rb:
- <textarea readonly name="code" class="ruby">#Enable the asset pipeline
- config.assets.enabled= true
- #Version of your assets, change this if you want to expire all yourassets
- config.assets.version= '1.0'
- #Change the path that assets are served from
- #config.assets.prefix = "/assets"</textarea>Config/environments/development.rb:<br>
- <textarea readonly name="code" class="ruby">#Do not compress assets
- config.assets.compress= false
- #Expands the lines which load the assets
- config.assets.debug= true</textarea>Config/environments/production.rb:
- <textarea readonly name="code" class="ruby">#Compress JavaScripts and CSS
- config.assets.compress= true
- #Choose the compressors to use
- #config.assets.js_compressor = :uglifier
- #config.assets.css_compressor = :yui
- #Don't fallback to assets pipeline if a precompiled asset is missed
- config.assets.compile= false
- #Generate digests for assets URLs.
- config.assets.digest= true
- #Defaults to Rails.root.join("public/assets")
- #config.assets.manifest = YOUR_PATH
- #Precompile additional assets (application.js, application.css, andall non-JS/CSS are already added)
- #config.assets.precompile += %w( search.js )</textarea>config/environment/test.rb:
- <textarea readonly name="code" class="ruby">#test 环境变化不大
- config.assets.compile= true
- config.assets.compress,config.assets.debug= false
- config.assets.digest= false</textarea>Gemfile:
- <textarea readonly name="code" class="ruby">#Gems used only for assets and not required
- #in production environments by default.
- group:assets do
- gem'sass-rails', "~> 3.1.0"
- gem'coffee-rails', "~> 3.1.0"
- gem'uglifier'
- end</textarea>
复制代码 要求bundler使用assets group, config/application.rb:- <textarea readonly name="code" class="ruby">if defined?(Bundler)
- #If you precompile assets before deploying to production, use thisline
- Bundler.require*Rails.groups(:assets=> %w(development test))
- #If you want your assets lazily compiled in production, use thisline
- #Bundler.require(:default, :assets, Rails.env)
- end</textarea>rails 3.0则为:<textarea readonly name="code" class="ruby">#If you have a Gemfile, require the gems listed there, includingany gems
- #you've limited to :test, :development, or :production.
- Bundler.require(:default,Rails.env) if defined?(Bundler)</textarea>[size=16px;]<br>[/size]
- [size=16px;]10. Feedback[/size]
- [size=16px;]<br>[/size]
- [size=16px;]<br>[/size]
- [size=16px;]<br>[/size]
复制代码 |
|