Customizing the included Javascript libraries

First, you will need to run the customization generator rrt:customize. This will generate an rrt.js file, which contains the imports for all external libraries. You can now edit that file to remove anything you don't need, but be sure to not remove Bootstrap itself - unless you really mean to.

Next, take a look at the view partial in views/layouts/_external_js_libs.html.erb. This contains code which handles the loading of particularly heavy JS libraries:

  • Modernizr
  • GMaps
  • Flot
  • ChartJS

You can remove whichever libraries you don't intend to use.

Finally, in the views/layouts/_base layout there are special tags on the body element under the data-js-libs data attribute. Remove any of the default libraries from this attribute that you don't need. Search your project for the string content_for :js_libs and review any additional JS libs that are loaded on a per-view basis that you might not want (dashboard demo libraries for example).

Ordering of Javascript external libraries in _base layout

There is a specific order possible to how Javascript libraries are rendered to the page. In order:

  • :external_js_libs
  • Your application.js
  • :javascript

This allows you to specify exactly those libraries which should be rendered out before everything else, or are otherwise not part of your asset pipeline manifest in application.js.

Activating libraries in external_js_libs for inclusion in the rendered output

Let's say you have a specific page that you want to load Google Maps on, and only that page. To have the appropriate external JS libs be rendered to the output, you'd need to include the following on that specific page:

<% content_for :js_libs, "gmaps" %>

This will cause the Google Maps include tag to be rendered in _external_js_libs.html.erb.

Adding additional libraries to _external_js_libs.html.erb

All you have to do is place the javascript_include_tag in a wrapper clause that checks the @js_libs instance variable for inclusion of a library name you specified in your content_for :js_libs tag:

<% if @js_libs.include? 'chartjs' %>
  <% content_for :javascript do %>
    <%= javascript_include_tag "vendor/chartjs/Chart.bundle.min" %>
    <%= javascript_include_tag "vendor/chartjs/Chart.min" %>
  <% end %>
<% end %>

That block of code would be rendered when a content_for :js_libs, 'chartjs' tag is encountered on a page.

In this case I've also chosen to have the include tags rendered into the :javascript content placeholder in the _base layout - this means it will be rendered after the main application javascripts, and after the :external_js_libs content placeholder.

Removing the Demo JS code

Once you run the customization generator as above, you can simply remove all require statements that reference rrt/demo.