Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude certain assets from pipeline in development environment?

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.

like image 680
David Tuite Avatar asked Dec 13 '11 06:12

David Tuite


1 Answers

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.

  • 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/context.rb#L174

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 %>
like image 174
cutalion Avatar answered Oct 05 '22 19:10

cutalion