如何在 ruby on rails 的开发环境中预编译资产?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32629811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How assets precompile in development environment on ruby on rails?
提问by Alexander Shlenchack
Why can't we precompile assets in a development environment? I know that sprockets basically compile all assets.
为什么我们不能在开发环境中预编译资产?我知道链轮基本上编译所有资产。
When we go in production environment then we run the command:
当我们进入生产环境时,我们运行以下命令:
rake assets:precompile
But in the development env we can't do anything for it to compile itself. What are difference in behavior of assets compilation in both environment?
但是在开发环境中,我们不能为它自己编译做任何事情。两种环境中资产编译的行为有何不同?
采纳答案by praaveen
Every web application contains some images and CSS files to make it look pretty, along with some JavaScript files to handle user interaction and behavior. If assets are loading faster, the web application ought to perform better. There are many strategies to make assets load fast such as minifying, compressing (gzipping), caching etc.
每个 Web 应用程序都包含一些图像和 CSS 文件以使其看起来很漂亮,以及一些用于处理用户交互和行为的 JavaScript 文件。如果资产加载速度更快,则 Web 应用程序的性能应该会更好。有许多策略可以使资产快速加载,例如缩小、压缩(gzipping)、缓存等。
In development mode, assets are served as separate files in the order they are specified in the manifest file.
在开发模式下,资产按照它们在清单文件中指定的顺序作为单独的文件提供。
This manifest app/assets/javascripts/application.js:
这个清单 app/assets/javascripts/application.js:
//= require core
//= require projects
//= require tickets
In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.
在生产环境中,Sprockets 使用上述指纹识别方案。默认情况下,Rails 假定资产已经预编译,并且将由您的 Web 服务器作为静态资产提供。
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.
在预编译阶段,MD5 从编译文件的内容中生成,并在写入磁盘时插入到文件名中。Rails 助手使用这些指纹名称代替清单名称。
For example this:
例如这个:
<%= javascript_include_tag "application" %>
<%= stylesheet_link_tag "application" %>
generates something like this:
生成这样的东西:
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
rel="stylesheet" />
Note: with the Asset Pipeline the :cache and :concat options aren't used anymore, delete these options from the javascript_include_tag and stylesheet_link_tag.
注意:对于资产管道,不再使用 :cache 和 :concat 选项,请从 javascript_include_tag 和 stylesheet_link_tag 中删除这些选项。
The fingerprinting behavior is controlled by the config.assets.digest initialization option (which defaults to true for production and false for everything else).
指纹行为由 config.assets.digest 初始化选项控制(生产环境默认为 true,其他情况默认为 false)。
回答by Alexander Shlenchack
If you want to precompile assets in development environment you can use this command:
如果要在开发环境中预编译资产,可以使用以下命令:
RAILS_ENV=development bundle exec rake assets:precompile
You can precompile assets in development environment by default using config/development.rb
默认情况下,您可以使用 config/development.rb 在开发环境中预编译资产
config.assets.debug = false
In most cases you don't need it because your development process will be more hard.
在大多数情况下,您不需要它,因为您的开发过程会更加困难。

