简体   繁体   English

开发模式下的资产编译速度慢

[英]Slow assets compilation in development mode

I have a large rails app with hundreds of coffee script files. 我有一个包含数百个咖啡脚本文件的大型rails应用程序。

Sometimes when I make a tiny change in a coffeescript file or I switch the branch the whole assets are being precompiled and I have to wait a long time for load the page: 有时当我在coffeescript文件中进行微小更改或切换分支时,整个资产都被预编译,我必须等待很长时间才能加载页面:

Started GET "/assets/application.js" for 127.0.0.1 at 2013-01-11 19:39:45 +0100
Compiled sprockets/commonjs.js  (0ms)  (pid 18142)
Compiled jquery.js  (2ms)  (pid 18142)
Compiled jquery_ujs.js  (0ms)  (pid 18142)
Compiled underscore.js  (0ms)  (pid 18142)
Compiled backbone.js  (0ms)  (pid 18142)
Compiled backbone_rails_sync.js  (0ms)  (pid 18142)
Compiled handlebars.runtime.js  (0ms)  (pid 18142)
Compiled moment.js  (0ms)  (pid 18142)
...and so on

I use the following assets configuration config/development.rb : 我使用以下资产配置config/development.rb

# Do not compress assets
config.assets.compress = false

# Expands the lines which load the assets
config.assets.debug = false

When I set config.assets.debug = false I have to wait quite long time for load hundreds of js files. 当我设置config.assets.debug = false我必须等待很长时间才能加载数百个js文件。 The question is: how to find the golden mean? 问题是:如何找到中庸之道? How to optimize assets configuration in the development mode for the large app? 如何在大型应用程序的开发模式中优化资产配置?

Take a look at this middleware from the Discourse team. 看一下Discourse团队的这个中间件 We've had great success with it in our rails 4 app -- took reload times down from a minute to 5 seconds in development. 我们在rails 4应用程序中取得了巨大的成功 - 将重新加载时间从一分钟缩短到5秒。

It's a sad truth, but you don't. 这是一个可悲的事实,但你没有。 There is not a clean way to solve this. 没有一种干净的方法来解决这个问题。

However, there are some patterns you could follow to minimize your pain which if I understand correctly is having to wait a lot in development to see the changes. 但是,你可以遵循一些模式来减少你的痛苦,如果我理解正确的话,必须等待很多开发才能看到变化。

As said these have been seen here1 and here2 . 如上所述,这里已经看到1这里2

  1. Take a look at item 2 from here1 . 这里看第2
  2. Break your assets in many files. 在许多文件中打破您的资产。 This will imply in fewer lines being processed when changes occur. 这将意味着在发生更改时处理的行数更少。
  3. Prefer css/js, they may not be as cool but require no compilation. 喜欢css / js,它们可能不那么酷但不需要编译。
  4. Find something interesting to do while assets precompile. 在资源预编译时找到有趣的事情。 It may lower productivity but sure kills the pain. 它可能会降低生产力,但肯定可以消除痛苦。

As others have pointed out, optimizing your assets is the #1 way to increase compilation speed (eliminate unnecessary files and pre-processors, import carefully, and use partials with caution). 正如其他人所指出的那样,优化资产是提高编译速度的第一种方法(消除不必要的文件和预处理器,谨慎导入,谨慎使用部分)。 However, why are you turning config.assets.debug = false in development mode anyway? 但是,为什么在开发模式下转换config.assets.debug = false呢? When it's false, sprockets will concatenate all your files together, and this can take quite a while if you have a large number of files. 当它为假时,链接器会将所有文件连接在一起,如果你有大量文件,这可能需要很长时间。

Instead, turn config.assets.debug = true , so assets are compiled and cached on the first request. 相反,请转动config.assets.debug = true ,以便在第一个请求中编译和缓存资产。 Sprockets sets a Cache-Control HTTP header to reduce request overhead on subsequent requests. Sprockets设置Cache-Control HTTP标头以减少后续请求的请求开销。 The browser gets a 304 (Not Modified) response. 浏览器获得304(未修改)响应。 See the Rails Asset Pipeline documentation. 请参阅Rails Asset Pipeline文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM