问题描述
您可以通过哪些方法来加快Rails Asset Pipeline的预编译过程?
What are the ways that you can speed up the Rails Asset Pipeline precompile process?
推荐答案
1. Capistrano部署加速
(1)使用capistrano内置任务"deploy/assets"进行部署.
Capistrano具有自己的内置任务部署/资产".它将自动为您完成任务.
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.
您自己的手工任务之间的区别在于,它仅加载assets
组来预编译资产,而不是整个环境.
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
https://gist.github.com/3072362
如果
- 应用/资产
- lib/资产
- 供应商/资产
- Gemfile.lock
- confir/routes.rb
被更改,它将重新编译资产.否则,它将跳过编译过程,节省大量时间.
are changed, it will recompile assets. Otherwise, it will skip the pecompile process, save a lot of time.
您都可以使用
@import "compass";
或@import "compass/typography/links/link-colors";
.
但是,编译资产时,@import "compass/typography/links/link-colors";
比@import "compass";
快9倍.
But @import "compass/typography/links/link-colors";
is 9 times faster than @import "compass";
when you compile assets.
那是因为当@import "compass";
时,它将编译整个指南针资产.不仅是link-colors
部分.
That is because when @import "compass";
, it compile whole compass assets. not only just link-colors
part.
在SCSS中,我们喜欢使用partial
来组织资产.
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"
比
@import "reset";
@import "base";
@import "product";
3.不需要.scss& .coffee无缘无故
(1)避免使用require_tree
当我们使用Rails生成器生成控制器时. Rails也将生成这样的资产
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
并使用以下技术在application.js中挂载资产:
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/
编译它们每个都将花费您大约250毫秒.如果您有10个空资产,则将是2.5秒.
It will cost you about 250ms to compile each of them. If you have 10 empty assets, it will be 2.5 seconds .
将其从您的项目中删除,或将它们单独安装在application.js中,如下所示:
Remove them from your project, or mount them individually in application.js like this:
//= require prodcuts
//= require users
//= require albums
(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)
- 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)
- 替换deploy.rb资产任务.
-
检查日志/production.log
- replace deploy.rb assets task.
check logs/production.log
- 找到缓慢的资产
- 删除@import指南针";使用替代解决方案.
- 使用require代替@import; (在确实需要时使用@import)
- 删除require_tree,分别装入资产
- 删除空的.scss和.coffeescript
- 当资产是纯CSS时,请使用.css.
(2) Don't use css.scss
or js.coffee
if unnecessary.
custom.css
是custom.css.scss
编译纯CSS和纯JS速度很快(成本几乎为0毫秒).但是,编译.scss和.coffee仍然要花费一些时间.
Compile pure CSS and pure JS is fast ( cost almost 0 ms). But compile .scss and .coffee still cost some time.
这篇关于您如何加快Rails Asset Pipeline的预编译过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!