在开发环境中从管道中排除某些资产?
我想以某种方式阻止某些资产包含在开发环境中的资产管道中。
到目前为止,我已经尝试了以下操作:
# app/assets/javascripts/application.js.erb
<% if Rails.env.production? %>
//= require google_analytics_snippet
<% end %>
我
# app/assets/javascripts/application.js.erb
<% if ENV['RACK_ENV'] == 'production' %>
//= require google_analytics_snippet
<% end %>
似乎要实现的只是 //= require google_analytics_snippet
行是否出现在清单中。无论我使用这些尝试的解决方案中的任何一个时的环境如何,google_analytics_snippet.js 文件中的实际代码都不会加载。
我有办法做到这一点吗?
编辑:
当我第一次发布这个问题时,我在示例中使用了一个名为 olark.js 的 javascript 文件。这是一个错误的例子选择,因为 Olark 有一个 rubygem 可以解决这个问题。我更改了示例,因为我正在寻找通用的表单解决方案。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我查看了链轮的来源,我发现,
指令预处理器始终在任何引擎之前运行。
因此,不可能使用 ERB 或其他引擎将任何条件逻辑添加到指令部分。
更新
Joshua Peek,回答了我的问题:
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.
UPDATE
Joshua Peek, answered on my question:
根据您的部署环境,您可能会查看 rack/olark gem。
然后你可以在你的 gemfile 中尝试这样的事情:
只是一个想法..
Depending on your deployment environment you might look at the rack/olark gem.
You could then probably try something like this in your gemfile:
Just a thought..