Sinatra, Backbone, RequireJS - Can I replace asset-pipeline with r.js optimizer?

197 views Asked by At

I'm new to RequireJS and the millions of new front-end tools and it's overwhelming. I'm starting a new multi-page Backbone app. It is very similar in many ways to a previous project. My approach is to take the new one and make it much more modular and reusable. The previous app is build with Sinatra to route requests and vanilla Backbone. My approach with the new app is to use RequireJS to load certain modules on each page while still routing through Sinatra. I want to keep some things similar so as to possibly make this approach backwards compatible to potentially rewrite the previous app while still moving things in a more modular direction.

Anyway, I had sinatra-asset-pipeline installed in the previous app and now in the new app as well. I have a well defined app structure with the new app and well defined entry points for each page. A stub of the directory for the entry points is like this:

app/
  assets/
    project/
      bundles/
        bundle1.js
        bundle2.js

In the layout.erb I'm loading each bundle on a per-page basis like so:

<script src=<%= asset_path('require')%> ></script>

<script type="text/javascript">
  require(['/assets/global'], function(global) {
    // @bundle is defined in each sinatra route
    require(["/assets/bundles/<%= @bundle %>"]);
  });
</script>

Each bundle will require and load its own modules. I understand that the r.js optimizer will look up the tree of the required modules and merge them together. I also understand you can setup modules in a build config. Would I be able to completely replace the asset-pipeline and just use the r.js optimizer to optimize each specific bundle and load them on a per-page basis?

0

There are 0 answers