在开发环境中从管道中排除某些资产?

发布于 2024-12-21 01:29:35 字数 779 浏览 1 评论 0原文

我想以某种方式阻止某些资产包含在开发环境中的资产管道中。

到目前为止,我已经尝试了以下操作:

# 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 有一个 ruby​​gem 可以解决这个问题。我更改了示例,因为我正在寻找通用的表单解决方案。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2024-12-28 01:29:35

我查看了链轮的来源,我发现,
指令预处理器始终在任何引擎之前运行。
因此,不可能使用 ERB 或其他引擎将任何条件逻辑添加到指令部分。

更新

Joshua Peek,回答了我的问题:

答案是肯定的,但如果这是您想要做的:

<% if Rails.env.Production? %> 
//= 需要 google_analytics_snippet
<%结束%>

试试这个:

<% if Rails.env.Production?
require_asset“google_analytics_snippet”
结束%>

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:

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 %>
累赘 2024-12-28 01:29:35

根据您的部署环境,您可能会查看 rack/olark gem

然后你可以在你的 gemfile 中尝试这样的事情:

group :production do
  gem 'rack-olark'
end

只是一个想法..

Depending on your deployment environment you might look at the rack/olark gem.

You could then probably try something like this in your gemfile:

group :production do
  gem 'rack-olark'
end

Just a thought..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文