After some discussion at work about how to manage JavaScript dependencies in our
CMS, I decided to write a quick Rails plugin for doing just that. It’s called
Holly, and it lets you use special comments inside script and CSS files to
declare their dependencies. Whenever you use javascript_include_tag
or
stylesheet_link_tag
, Holly inspects the dependencies of the files you’re
loading, and makes sure all of them get included in the page’s markup in the
correct order and without duplication. This means you can just include the
libraries your page directly codes against, and let the server worry about
loading everything else you need. An example:
in effects.js
// @require prototype
in dragdrop.js
// @require effects
in widget.js
// @require dragdrop
// @load widget/item
// @load widget/page
// @load widget.css
in your template...
<%= javascript_include_tag 'widget' %>
produces...
<script src="/javascripts/prototype.js"></script>
<script src="/javascripts/effects.js"></script>
<script src="/javascripts/dragdrop.js"></script>
<script src="/javascripts/widget.js"></script>
<script src="/javascripts/widget/item.js"></script>
<script src="/javascripts/widget/page.js"></script>
<link href="/stylesheets/widget.css" rel="stylesheet" />
You also get a rake task for examining your assets’ relationships to one another. Head on over to the project page to find out how to download and use Holly.