开发模式下的慢资产编译

时间:2023-01-15 23:05:59

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:

有时当我在coffecript文件中做一个微小的修改或者我切换分支时,所有的资产都被预先编译,我不得不等待很长时间才能加载页面:

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:

我使用以下资产配置/开发。

# 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. The question is: how to find the golden mean? How to optimize assets configuration in the development mode for the large app?

当我设置config. asset. debug = false时,我必须等待很长时间才能加载数百个js文件。问题是:如何找到黄金的平均值?如何优化大应用开发模式下的资产配置?

3 个解决方案

#1


2  

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. Take a look at item 2 from here1.
  2. 看看下面的第2项。
  3. Break your assets in many files. This will imply in fewer lines being processed when changes occur.
  4. 在许多文件中破坏你的资产。这将意味着在发生更改时处理的行更少。
  5. Prefer css/js, they may not be as cool but require no compilation.
  6. 喜欢css/js,它们可能不那么酷,但不需要编译。
  7. Find something interesting to do while assets precompile. It may lower productivity but sure kills the pain.
  8. 在资产预编译时找到一些有趣的事情。它可能降低生产力,但却能消除痛苦。

#2


7  

Take a look at this middleware from the Discourse team. We've had great success with it in our rails 4 app -- took reload times down from a minute to 5 seconds in development.

请查看来自Discourse team的这个中间件。我们已经在rails 4应用程序中取得了巨大的成功——在开发过程中从一分钟到五秒的时间内重新加载。

#3


0  

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? 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.

正如其他人指出的,优化您的资产是提高编译速度的第一种方法(消除不必要的文件和预处理程序,小心导入,小心使用部分)。但是,为什么要在开发模式中使用config.assets.debug = false呢?当它为false时,spro佝偻病将把所有文件连接在一起,如果您有大量的文件,这可能需要相当长的时间。

Instead, turn config.assets.debug = true, so assets are compiled and cached on the first request. Sprockets sets a Cache-Control HTTP header to reduce request overhead on subsequent requests. The browser gets a 304 (Not Modified) response. See the Rails Asset Pipeline documentation.

相反,将config.assets.debug = true改为true,以便在第一个请求上编译和缓存资产。spro佝偻病设置了缓存控制的HTTP头,以减少后续请求的请求开销。浏览器会得到304(未修改)响应。请参阅Rails资产管道文档。

#1


2  

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. Take a look at item 2 from here1.
  2. 看看下面的第2项。
  3. Break your assets in many files. This will imply in fewer lines being processed when changes occur.
  4. 在许多文件中破坏你的资产。这将意味着在发生更改时处理的行更少。
  5. Prefer css/js, they may not be as cool but require no compilation.
  6. 喜欢css/js,它们可能不那么酷,但不需要编译。
  7. Find something interesting to do while assets precompile. It may lower productivity but sure kills the pain.
  8. 在资产预编译时找到一些有趣的事情。它可能降低生产力,但却能消除痛苦。

#2


7  

Take a look at this middleware from the Discourse team. We've had great success with it in our rails 4 app -- took reload times down from a minute to 5 seconds in development.

请查看来自Discourse team的这个中间件。我们已经在rails 4应用程序中取得了巨大的成功——在开发过程中从一分钟到五秒的时间内重新加载。

#3


0  

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? 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.

正如其他人指出的,优化您的资产是提高编译速度的第一种方法(消除不必要的文件和预处理程序,小心导入,小心使用部分)。但是,为什么要在开发模式中使用config.assets.debug = false呢?当它为false时,spro佝偻病将把所有文件连接在一起,如果您有大量的文件,这可能需要相当长的时间。

Instead, turn config.assets.debug = true, so assets are compiled and cached on the first request. Sprockets sets a Cache-Control HTTP header to reduce request overhead on subsequent requests. The browser gets a 304 (Not Modified) response. See the Rails Asset Pipeline documentation.

相反,将config.assets.debug = true改为true,以便在第一个请求上编译和缓存资产。spro佝偻病设置了缓存控制的HTTP头,以减少后续请求的请求开销。浏览器会得到304(未修改)响应。请参阅Rails资产管道文档。