I would like to somehow prevent certain assets from being included in the asset pipeline in the development environment.
我想以某种方式阻止某些资产被包含在开发环境中的资产管道中。
So far, I have tried the following:
到目前为止,我尝试过以下方法:
# app/assets/javascripts/application.js.erb
<% if Rails.env.production? %>
//= require google_analytics_snippet
<% end %>
and
和
# app/assets/javascripts/application.js.erb
<% if ENV['RACK_ENV'] == 'production' %>
//= require google_analytics_snippet
<% end %>
All I seem to be achieving is whether or not the //= require google_analytics_snippet
line appears in the manifest. The actual code in the google_analytics_snippet.js file is never loaded, regardless of environment when I use either of these attempted solutions.
我似乎要实现的是// =需要google_analytics_snippet线是否出现在清单中。当我使用这些尝试的解决方案时,无论环境如何,都不会加载google_analytics_snippet.js文件中的实际代码。
Is there a way I can do this?
有没有办法可以做到这一点?
Edit:
I was using a javascript file called olark.js in my examples when I first posted this question. That was a bad choice of example since Olark has a rubygem which may solve the problem. I have changed the example because I am looking for the general form solution.
编辑:当我第一次发布这个问题时,我在我的例子中使用了一个名为olark.js的javascript文件。这是一个不好的选择,因为Olark有一个可以解决问题的rubygem。我已经改变了这个例子,因为我正在寻找一般的表格解决方案。
2 个解决方案
#1
19
I've looked through the source of the sprockets and I found, that the directive preprocessor always runs before any engine. So, it's not possible to add any conditional logic into the directives section with ERB or other engine.
我查看了链轮的来源,我发现,指令预处理器总是在任何引擎之前运行。因此,使用ERB或其他引擎将指令逻辑添加到指令部分是不可能的。
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L51
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L51
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/processing.rb#L36
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/processing.rb#L36
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb#L174
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb#L174
UPDATE
UPDATE
Joshua Peek, answered on my question:
Joshua Peek,回答我的问题:
The answer is yes, but if this is what you are trying to do:
答案是肯定的,但如果这是你想要做的:
<% if Rails.env.production? %> //= require google_analytics_snippet <% end %>
try this instead:
试试这个:
<% if Rails.env.production? require_asset "google_analytics_snippet" end %>
#2
0
Depending on your deployment environment you might look at the rack/olark gem.
根据您的部署环境,您可能会看到rack / olark gem。
You could then probably try something like this in your gemfile:
然后你可以在gemfile中尝试这样的东西:
group :production do
gem 'rack-olark'
end
Just a thought..
只是一个想法..
#1
19
I've looked through the source of the sprockets and I found, that the directive preprocessor always runs before any engine. So, it's not possible to add any conditional logic into the directives section with ERB or other engine.
我查看了链轮的来源,我发现,指令预处理器总是在任何引擎之前运行。因此,使用ERB或其他引擎将指令逻辑添加到指令部分是不可能的。
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L51
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb#L51
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/processing.rb#L36
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/processing.rb#L36
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb#L174
- https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb#L174
UPDATE
UPDATE
Joshua Peek, answered on my question:
Joshua Peek,回答我的问题:
The answer is yes, but if this is what you are trying to do:
答案是肯定的,但如果这是你想要做的:
<% if Rails.env.production? %> //= require google_analytics_snippet <% end %>
try this instead:
试试这个:
<% if Rails.env.production? require_asset "google_analytics_snippet" end %>
#2
0
Depending on your deployment environment you might look at the rack/olark gem.
根据您的部署环境,您可能会看到rack / olark gem。
You could then probably try something like this in your gemfile:
然后你可以在gemfile中尝试这样的东西:
group :production do
gem 'rack-olark'
end
Just a thought..
只是一个想法..