I'm writing a mountable Rails 3.1 engine that adds features to the main app (i.e. it isn't isolated). The engine's JS depends on jQuery, jQueryUI, DataTables and JSTree (among other libraries) so the engine's manifest looks a little something like:
// my_engine.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jstree_pre1.0_fix_1/jquery.jstree.js
//= require_tree .
I can then conditionally load this file into the main app's layout using:
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'my_engine/my_engine' if user_logged_in? %>
However, if the main app's manifest already includes one of the dependencies (most likely jQuery and jQuery_UJS) then those dependencies will be loaded twice. I'll see something like:
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
...
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jstree_pre1.0_fix_1/jquery.jstree.js?body=1" type="text/javascript"></script>
...
<script src="/assets/cms/application.js?body=1" type="text/javascript"></script>
Sprocket's require
directive will only require an asset once within the same bundle but here I am working with two bundles. To make it simple for other devs implementing my engine, I'd like them to be able to include one line that detects whether or not the shared dependencies are already included. Is there are nice way of doing this using Sprockets?
In the end, I chose to not to make the vendor libraries hard dependencies and I've written into the documentation that they must be required manually. This creates a little bit more work for downstream developers but I think it offers the most flexibility.
So, the engine's manifest file, my_engine.js
, is now just:
//= require_tree .
and an example main app:
application.js
://= require jquery
//= require jquery_ujs
//= require_tree .
my_engine_requirements.js
://= require jquery-ui
//= require jstree_pre1.0_fix_1/jquery.jstree.js
//= require my_engine/my_engine
application.html.erb
:<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'my_engine_requirements' if user_logged_in? %>
This solution technically doesn't answer my original question but it's what I did after rationalising the situation a bit more.
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