Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require assets only for development environment

I'm using the assets pipeline from Rails 3.1 and I want to include some javascript files only if it's the development environment.

Example:

//= require application/jquery
//= require application/jquery_ujs
// next ones only for development environment
//= require application/badglobals
//= require application/consul

Its there a standar way of doing this? Any suggestions?

Update

Looking at the Sprockets current documentation, seems like there is not a way to do this.

like image 579
PabloRosales Avatar asked Oct 04 '11 04:10

PabloRosales


2 Answers

Why not just require these in the view? Is it important that they are loaded in the asset? To load them in the view:

<% if Rails.env.development? %>
  <%= javascript_include_tag "application/badglobals" %>
  <%= javascript_include_tag "application/consul" %>
<% end %>
like image 144
Ryan Bigg Avatar answered Nov 05 '22 10:11

Ryan Bigg


If you rename your application.js file (or whichever file you're calling //= require ... in) to application.js.erb, you can take advantage of require_asset. i.e:

//= require application/jquery
//= require application/jquery_ujs
<% if Rails.env.development? %>
  <%= require_asset 'application/badglobals' %>
  <%= require_asset 'application/consul' %>
<% end %>

Source: https://github.com/sstephenson/sprockets/issues/90

like image 32
XtraSimplicity Avatar answered Nov 05 '22 10:11

XtraSimplicity