免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2307 | 回复: 1
打印 上一主题 下一主题

Asset Pipeline in rails 3.1.0 (2)................. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-07 14:54 |只看该作者 |倒序浏览
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方法,输入为一个串,输出为压缩后的串:
  1. <textarea readonly name="code" class="ruby">class Transformer
  2.   defcompress(string)
  3.     do_something_returning_a_string(string)
  4.   end
  5. end </textarea>可以在application.rb中启动自定义的压缩类:<textarea readonly name="code" class="ruby">config.assets.css_compressor= Transformer.new</textarea><br>[size=16px;]5.4 切换资源文件根路径[/size]

  6. 默认是/assets路径,可以配置换成其他路径:

  7. <textarea readonly name="code" class="ruby">config.assets.prefix= "/some_other_path"</textarea>主要是考虑到rails3.1以前的资源文件使用的路径不是assets,方便扩展.

  8. 5.5 X-Sendfile Headers

  9. X-Sendfileheader为web服务定义一些请求参数,以便web服务器忽略应用服务器的响应,而用请求头所指定的文件进行替代.这个选项默认是禁用的,如果web服务器支持,则可以启动些选项.一旦启动,web服务器可以直接从响应头中分析取出输入的资源文件,使响应更快.

  10. apache和nginx运载X-Sendfileheader,可以在config/environments/production.rb设置:

  11. <textarea readonly name="code" class="ruby">#config.action_dispatch.x_sendfile_header = "X-Sendfile"       # for apache
  12. #config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' #for nginx
  13. </textarea>注意:可以将这个选项设置在production.rb下,用于生产环境下才有效,而开发和测试环境忽略X-Sendfileheader,对开发有利(比如错误提示信息在开发环境可见,在生产环境不可见).

  14. 6. How Caching Works

  15. Sprocketsuses the default Rails cachestore to cache assets in development andproduction.

  16. TODO:Addmore about changing the default store.
  17. (Sprockets默认在开发和生产环境使用rails缓存机制来缓存assets资源.)

  18. 7.为你的Gem加入assets支持

  19. Assets资源可以从gem中获得.

  20. 比如jquery-rails插件提供了rails标准js库的支持.插件包含了rails引擎,继承自Rails::Engine,这样它里面的app/assets,lib/assets, vendor/assets目录便可以直接被rails辨识出来,sprockets可以直接读到到相关路径的资源文件.

  21. 8. Making Your Library or Gem a Pre-Processor

  22. TODO:Registering gems on Tilt enablingSprockets to find them.

  23. 9. 更新到新版本的rails

  24. 更新过程需要注意两个问题:

  25. 首先要将更变资源文件集的路径.不同类型的文件js,css等移动到不同的位置;其次要重新设置一系列的系统选项,以下是rails3.1.0会添加以下选项:
  26. config/application.rb:

  27. <textarea readonly name="code" class="ruby">#Enable the asset pipeline
  28. config.assets.enabled= true

  29. #Version of your assets, change this if you want to expire all yourassets
  30. config.assets.version= '1.0'

  31. #Change the path that assets are served from
  32. #config.assets.prefix = "/assets"</textarea>Config/environments/development.rb:<br>

  33. <textarea readonly name="code" class="ruby">#Do not compress assets
  34. config.assets.compress= false

  35. #Expands the lines which load the assets
  36. config.assets.debug= true</textarea>Config/environments/production.rb:

  37. <textarea readonly name="code" class="ruby">#Compress JavaScripts and CSS
  38. config.assets.compress= true

  39. #Choose the compressors to use
  40. #config.assets.js_compressor = :uglifier
  41. #config.assets.css_compressor = :yui

  42. #Don't fallback to assets pipeline if a precompiled asset is missed
  43. config.assets.compile= false

  44. #Generate digests for assets URLs.
  45. config.assets.digest= true

  46. #Defaults to Rails.root.join("public/assets")
  47. #config.assets.manifest = YOUR_PATH

  48. #Precompile additional assets (application.js, application.css, andall non-JS/CSS are already added)
  49. #config.assets.precompile += %w( search.js )</textarea>config/environment/test.rb:


  50. <textarea readonly name="code" class="ruby">#test 环境变化不大
  51. config.assets.compile= true
  52. config.assets.compress,config.assets.debug= false
  53. config.assets.digest= false</textarea>Gemfile:

  54. <textarea readonly name="code" class="ruby">#Gems used only for assets and not required
  55. #in production environments by default.
  56. group:assets do
  57.     gem'sass-rails', "~> 3.1.0"
  58.     gem'coffee-rails', "~> 3.1.0"
  59.     gem'uglifier'
  60. end</textarea>
复制代码
要求bundler使用assets group, config/application.rb:
  1. <textarea readonly name="code" class="ruby">if defined?(Bundler)
  2.     #If you precompile assets before deploying to production, use thisline
  3.     Bundler.require*Rails.groups(:assets=> %w(development test))
  4.     #If you want your assets lazily compiled in production, use thisline
  5.     #Bundler.require(:default, :assets, Rails.env)
  6. end</textarea>rails 3.0则为:<textarea readonly name="code" class="ruby">#If you have a Gemfile, require the gems listed there, includingany gems
  7. #you've limited to :test, :development, or :production.
  8. Bundler.require(:default,Rails.env) if defined?(Bundler)</textarea>[size=16px;]<br>[/size]
  9. [size=16px;]10. Feedback[/size]

  10. [size=16px;]<br>[/size]

  11. [size=16px;]<br>[/size]

  12. [size=16px;]<br>[/size]
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-23 20:13 |只看该作者
谢谢分享   学习鸟
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP