I'm trying to extract all vendor assets into separate file and serve them as a minified and combined file also in development environment. I could do that in Rails 3 by using debug: false
in javascript_link_tag
and stylesheet_link_tag
helpers like this:
我正在尝试将所有供应商资产提取到单独的文件中,并在开发环境中将它们作为缩小和组合文件提供。我可以在Rails 3中使用debug:false在javascript_link_tag和stylesheet_link_tag帮助器中这样做:
<%= stylesheet_link_tag "vendor", :media => "all", :debug => false %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "vendor", :debug => false %>
<%= javascript_include_tag "application" %>
That made Rails to serve me vendor.js
and vendor.css
as a minified and combined assets even in development environment. application.js
and application.css
were served as usually in development environment.
这使得Rails即使在开发环境中也可以将vendor.js和vendor.css作为一种缩小的合并资产。 application.js和application.css通常在开发环境中提供。
I can not achieve similar results in Rails 4, because if using the lines above, then this will be generated into html for every asset specified in vendor
assets:
我无法在Rails 4中获得类似的结果,因为如果使用上面的行,那么这将生成为供应商资产中指定的每个资产的html:
<script debug="false"... ><script>
How to achieve the same thing in Rails 4?
如何在Rails 4中实现同样的功能?
I have written a detailed blog post for Rails 3 about this feature at my blog. You can check it out if my question is not clear enough http://itreallymatters.net/post/45763483826/speeding-up-page-load-time-in-rails
我在博客上为Rails 3撰写了一篇关于此功能的详细博客文章。如果我的问题不够明确,你可以查看它http://itreallymatters.net/post/45763483826/speeding-up-page-load-time-in-rails
4 个解决方案
#1
7
Overall: Its a regression with Sprockets 2. Read on for an explanation and solution.
总体而言:它与Sprockets的回归2.继续阅读以获得解释和解决方案。
How Debugging works internally
Assume you have a file called vendor.js
with the following:
假设您有一个名为vendor.js的文件,其中包含以下内容:
# vendor.js
//= require jquery
//= require knockout
//= ... some more requires
function one() { }
function two() { }
// and some javascript code
First, let's see what the Asset Debugging does:
首先,让我们看看资产调试的作用:
-
Places a single
<script src="vendor.js">
tag if debugging is disabled,如果禁用调试,则放置一个
(or)
(要么)
Places multiple
<script src="vendor.js?body=1">, <script src="jquery.js?body=1">, <script src="knockout.js?body=1">, ...
when debugging is enabled放置多个
-
The
body=1
is also an integral part of debugging. If you say<script src="vendor.js?body=1">
- it only renders the javascript insidevendor.js
. It doesn't include any of the otherrequire ...
code.body = 1也是调试的一个组成部分。如果你说
But if you hit
vendor.js
alone, without the?body=1
, it includes all therequire ...
code as well.但是,如果你单独使用vendor.js,没有?body = 1,它也包含所有require ...代码。
So a combination of the above two produce the necessary debugging output. What we want to do is, when we say javascript_include_tag "vendor", :debug => false
, we want a single <script src="vendor.js">
tag with NO ?body=1
appended.
因此,上述两者的组合产生了必要的调试输出。我们想要做的是,当我们说javascript_include_tag“vendor”,:debug => false时,我们想要一个
Explaining the Regression
The regressed code is here. Specifically the buggy code is this one statement:
回归代码在这里。特别是有缺陷的代码是这一个声明:
L88. if request_debug_assets?
Its checking request_debug_assets?, and then automatically setting :debug => true
further in line #92. But request_debug_assets?
is returning true, because that is being set at the application configuration level.
它检查request_debug_assets ?,然后在第92行自动设置:debug => true。但是request_debug_assets?返回true,因为它是在应用程序配置级别设置的。
This one statement should have ideally been:
理想情况下,这一陈述应该是:
L88. if request_debug_assets? && options["debug"] != false
Solution / Patch
I'll raise a pull request for the same, but until the pull request is reviewed and merged, you can do the following in an initializer:
我会提出相同的拉取请求,但在审核并合并拉取请求之前,您可以在初始化程序中执行以下操作:
# config/initializers/sprockets_debug_patch.rb
module Sprockets::Rails::Helper
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
# CHECK options["debug"] as well, not just the global assets.debug
if request_debug_assets? && options["debug"] != false
# REST ALL SAME CODE AS ORIGINAL METHOD
Do the same for stylesheet_include_tag
as well. Unfortunately there is no better way than copy/pasting the method code, but this resolves the problem.
对stylesheet_include_tag执行相同的操作。不幸的是,没有比复制/粘贴方法代码更好的方法,但这解决了这个问题。
Throughout Sprockets::Rails::Helper
class, you will find that it says all this will be deprecated in Sprockets 3.x. I don't know if Rails 4 is scheduled to ship with Sprockets 3.x. If so, then these patches might end up not being required.
在整个Sprockets :: Rails :: Helper类中,你会发现它表示所有这些都将在Sprockets 3.x中弃用。我不知道Rails 4是否计划与Sprockets 3.x一起发货。如果是这样,那么最终可能不需要这些补丁。
#2
4
Another option if you don't want to use monkeypatch right now and you are just trying to work with your app without worrying about all of you assets is to set / change
另一个选择,如果您现在不想使用monkeypatch并且您只是尝试使用您的应用而不必担心所有资产是设置/更改
config.assets.debug = false
config.assets.compress = false
in config/development.rb
在config / development.rb中
#3
0
I fixed this in sprockets-rails master and I'll release a new version soon. Meanwhile you can get the fix in your application pointing sprockets-rails to github in your Gemfile:
我在sprockets-rails master中修复了这个问题,我很快就会发布一个新版本。同时,您可以在应用程序中获取修复程序,将sprockets-rails指向Gemfile中的github:
gem "sprockets-rails", github: "rails/sprockets-rails"
#4
0
With regards to Rails 4.2 and sprockets-rails 2.3.3 this is working:
关于Rails 4.2和sprockets-rails 2.3.3这是有效的:
config.asset.debug = false
and then:
接着:
?debug_assets=1
#1
7
Overall: Its a regression with Sprockets 2. Read on for an explanation and solution.
总体而言:它与Sprockets的回归2.继续阅读以获得解释和解决方案。
How Debugging works internally
Assume you have a file called vendor.js
with the following:
假设您有一个名为vendor.js的文件,其中包含以下内容:
# vendor.js
//= require jquery
//= require knockout
//= ... some more requires
function one() { }
function two() { }
// and some javascript code
First, let's see what the Asset Debugging does:
首先,让我们看看资产调试的作用:
-
Places a single
<script src="vendor.js">
tag if debugging is disabled,如果禁用调试,则放置一个
(or)
(要么)
Places multiple
<script src="vendor.js?body=1">, <script src="jquery.js?body=1">, <script src="knockout.js?body=1">, ...
when debugging is enabled放置多个
-
The
body=1
is also an integral part of debugging. If you say<script src="vendor.js?body=1">
- it only renders the javascript insidevendor.js
. It doesn't include any of the otherrequire ...
code.body = 1也是调试的一个组成部分。如果你说
But if you hit
vendor.js
alone, without the?body=1
, it includes all therequire ...
code as well.但是,如果你单独使用vendor.js,没有?body = 1,它也包含所有require ...代码。
So a combination of the above two produce the necessary debugging output. What we want to do is, when we say javascript_include_tag "vendor", :debug => false
, we want a single <script src="vendor.js">
tag with NO ?body=1
appended.
因此,上述两者的组合产生了必要的调试输出。我们想要做的是,当我们说javascript_include_tag“vendor”,:debug => false时,我们想要一个
Explaining the Regression
The regressed code is here. Specifically the buggy code is this one statement:
回归代码在这里。特别是有缺陷的代码是这一个声明:
L88. if request_debug_assets?
Its checking request_debug_assets?, and then automatically setting :debug => true
further in line #92. But request_debug_assets?
is returning true, because that is being set at the application configuration level.
它检查request_debug_assets ?,然后在第92行自动设置:debug => true。但是request_debug_assets?返回true,因为它是在应用程序配置级别设置的。
This one statement should have ideally been:
理想情况下,这一陈述应该是:
L88. if request_debug_assets? && options["debug"] != false
Solution / Patch
I'll raise a pull request for the same, but until the pull request is reviewed and merged, you can do the following in an initializer:
我会提出相同的拉取请求,但在审核并合并拉取请求之前,您可以在初始化程序中执行以下操作:
# config/initializers/sprockets_debug_patch.rb
module Sprockets::Rails::Helper
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
# CHECK options["debug"] as well, not just the global assets.debug
if request_debug_assets? && options["debug"] != false
# REST ALL SAME CODE AS ORIGINAL METHOD
Do the same for stylesheet_include_tag
as well. Unfortunately there is no better way than copy/pasting the method code, but this resolves the problem.
对stylesheet_include_tag执行相同的操作。不幸的是,没有比复制/粘贴方法代码更好的方法,但这解决了这个问题。
Throughout Sprockets::Rails::Helper
class, you will find that it says all this will be deprecated in Sprockets 3.x. I don't know if Rails 4 is scheduled to ship with Sprockets 3.x. If so, then these patches might end up not being required.
在整个Sprockets :: Rails :: Helper类中,你会发现它表示所有这些都将在Sprockets 3.x中弃用。我不知道Rails 4是否计划与Sprockets 3.x一起发货。如果是这样,那么最终可能不需要这些补丁。
#2
4
Another option if you don't want to use monkeypatch right now and you are just trying to work with your app without worrying about all of you assets is to set / change
另一个选择,如果您现在不想使用monkeypatch并且您只是尝试使用您的应用而不必担心所有资产是设置/更改
config.assets.debug = false
config.assets.compress = false
in config/development.rb
在config / development.rb中
#3
0
I fixed this in sprockets-rails master and I'll release a new version soon. Meanwhile you can get the fix in your application pointing sprockets-rails to github in your Gemfile:
我在sprockets-rails master中修复了这个问题,我很快就会发布一个新版本。同时,您可以在应用程序中获取修复程序,将sprockets-rails指向Gemfile中的github:
gem "sprockets-rails", github: "rails/sprockets-rails"
#4
0
With regards to Rails 4.2 and sprockets-rails 2.3.3 this is working:
关于Rails 4.2和sprockets-rails 2.3.3这是有效的:
config.asset.debug = false
and then:
接着:
?debug_assets=1