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.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With