问题描述
有哪些方法可以加快 Rails Asset Pipeline 预编译过程?
1.Capistrano 部署加速
(1) 使用 capistrano 内置任务 'deploy/assets' 进行部署.
Capistrano 有自己的内置任务部署/资产".它会自动为您完成任务.
你自己手工任务的区别在于它只是加载assets
组来预编译资产,而不是整个环境.
cd/home/apps/APP_NAME/releases/20120708184757 &&bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile
(2) 当资产没有改变时跳过预编译过程.
https://gist.github.com/3072362
如果
- 应用/资产
- 库/资产
- 供应商/资产
- Gemfile.lock
- 确认/routes.rb
被改变了,它会重新编译资产.否则,它将跳过pecompile过程,节省大量时间.
2.小心使用@import.
(1) 避免直接使用@import "compass";
.
当你使用它时它会起作用
@import "compass";
或 @import "compass/typography/links/link-colors";
在 SCSS 中.
但是 @import "compass/typography/links/link-colors";
在编译资源时比 @import "compass";
快 9 倍.>
那是因为当 @import "compass";
时,它会编译整个罗盘资产.不仅仅是 link-colors
部分.
(2) 避免使用partials
在 SCSS 中,我们喜欢使用 partial
来组织我们的资产.
但前提是需要共享变量,或者有必要的依赖,否则
//= 需要重置"//= 需要基础"//= 需要产品"
比
快@import "reset";@import "基础";@import "产品";
3.不需要 .scss &.咖啡无缘无故
(1) 避免使用 require_tree
当我们使用 Rails 生成器生成控制器时.Rails 也会生成这样的资产
- product.css.scss
- product.js.coffee
并使用此技术在 application.js 中挂载资产:
//= require_tree
但是空资产(不输出任何内容)只包含以下几行:
# 将所有与匹配控制器相关的行为和钩子放在这里.# 所有这些逻辑将自动在 application.js 中可用.# 你可以在这个文件中使用 CoffeeScript:http://jashkenas.github.com/coffee-script/
编译每个文件大约需要 250 毫秒.如果您有 10 个空资源,则为 2.5 秒.
将它们从您的项目中移除,或者像这样在 application.js 中单独安装它们:
//= 需要产品//= 需要用户//= 需要专辑
(2) 如果没有必要,不要使用 css.scss
或 js.coffee
.
- 编译 jquery-ui-1.8.16.custom.css (0ms) (pid 19108)
- 编译 jquery.ui.1.8.16.ie.css (0ms) (pid 19108)
- 编译 jquery.js (5ms) (pid 19108)
- 编译 jquery_ujs.js (0ms) (pid 19108)
- 编译 custom.css (14ms) (pid 19108)
custom.css
是 custom.css.scss
编译纯 CSS 和纯 JS 很快(花费几乎 0 毫秒).但是编译 .scss 和 .coffee 还是需要一些时间的.
总结
- 替换 deploy.rb 资产任务.
检查日志/production.log
- 寻找缓慢的资产
- 删除@import "compass";使用替代解决方案.
- 使用 require 代替@import;(真正需要时使用@import)
- 移除require_tree,单独挂载资产
- 删除空的 .scss 和 .coffeescript
- 当资源是纯 CSS 时使用 .css.
What are the ways that you can speed up the Rails Asset Pipeline precompile process?
1. Capistrano deployment speedup
(1) use capistrano built-in task 'deploy/assets' to deploy.
Capistrano has its own built-in task 'deploy/assets'. It will automatically do task for you.
The difference between your own handcraft task is it only load assets
group to precompile assets, not whole environment.
cd /home/apps/APP_NAME/releases/20120708184757 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile
(2) skip precompile process when assets aren't changed.
https://gist.github.com/3072362
If
- app/assets
- lib/assets
- vendor/assets
- Gemfile.lock
- confir/routes.rb
are changed, it will recompile assets. Otherwise, it will skip the pecompile process, save a lot of time.
2. Use @import carefully.
(1) avoid using @import "compass";
directly.
It will both work when you
@import "compass";
or @import "compass/typography/links/link-colors";
in SCSS.
But @import "compass/typography/links/link-colors";
is 9 times faster than @import "compass";
when you compile assets.
That is because when @import "compass";
, it compile whole compass assets. not only just link-colors
part.
(2) avoid using partials
In SCSS, we like to use partial
to organize our assets.
But only if you need to share variables, or there are necessary dependencies, otherwise
//= require "reset"
//= require "base"
//= require "product"
is faster than
@import "reset";
@import "base";
@import "product";
3. don’t require .scss & .coffee for no reason
(1) avoid using require_tree
When we use Rails generator to generate controllers. Rails will also generate assets likes this
- product.css.scss
- product.js.coffee
and mount assets in application.js using this techniques:
//= require_tree
But the empty assets (output nothing) which only contain this lines:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
It will cost you about 250ms to compile each of them. If you have 10 empty assets, it will be 2.5 seconds .
Remove them from your project, or mount them individually in application.js like this:
//= require prodcuts
//= require users
//= require albums
(2) Don't use css.scss
or js.coffee
if unnecessary.
- Compiled jquery-ui-1.8.16.custom.css (0ms) (pid 19108)
- Compiled jquery.ui.1.8.16.ie.css (0ms) (pid 19108)
- Compiled jquery.js (5ms) (pid 19108)
- Compiled jquery_ujs.js (0ms) (pid 19108)
- Compiled custom.css (14ms) (pid 19108)
custom.css
is custom.css.scss
Compile pure CSS and pure JS is fast ( cost almost 0 ms). But compile .scss and .coffee still cost some time.
Summarize
- replace deploy.rb assets task.
check logs/production.log
- find slow assets
- remove @import "compass"; use alternative solution.
- use require instead @import; ( use @import when it is really necessary )
- remove require_tree, mount assets individually
- remove empty .scss and .coffeescript
- use .css when assets are pure CSS.
这篇关于如何加快 Rails Asset Pipeline 预编译过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!