documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

Removing the Rails Controller and refactor into middleware #251

Open mulderp opened 10 years ago

mulderp commented 10 years ago

By using Rack middleware, we would allow easier integration of Jammit into non-Rails projects and addresses https://github.com/documentcloud/jammit/issues/161

The basic approach here is using a Rack::Request and extract path_info from there, package the assets with Jammit, and wrap it into a Rack::Response.

I was looking into Rack::Sprockets and Warden for some inspiration on the topic (incl. use of RSpec).

It would be great to hear your feedback what you think.

My first thought was also to rename the Controller into Manager or Packager, to avoid confusion with a Rails or Backbone controller.

The request-response can be tested with:

 bundle exec rspec spec/jammit/controller_spec.rb
mulderp commented 10 years ago

This PR is mainly for discussion, it is not yet functional.

jashkenas commented 10 years ago

Sounds like a decent enough idea in general -- but there are Rails-specfic things that Jammit needs to take advantage of, which simply aren't defined in Rack (AFAIK).

For example, Jammit needs to provide helpers so that you can include correctly generated script and style resources. For another, Jammit needs to access the correct rails ASSET_ID, or similar.

mulderp commented 10 years ago

Hmm.. interesting, I didn't think too much on the view helpers. Would it make sense / possible to support these from a separate Railtie?

I was thinking that the use case of asset management was: When hitting the '/assets' path, then build/precompile the requested assets from the path info. From many smaller projects (non-Rails), it is often enough to serve one app.js that is build from smaller files. I will think about this problem for larger projects, but maybe others have some ideas how to address this with a Rack solution too.

mulderp commented 10 years ago

A 2-layer approach like Warden/Devise, for jammit-rack/jammit might be nice.

I am just asking myself how jammit-rack would look like in development mode for a non-rails project. Packaging/Compressing assets might be skipped, but this would make the '/assets' middleware rather a static file server. Having a way to load multiple files like require.js allows with main.js might be nice, but not sure.

Anyone here sees use cases for Jammit-Rack for development mode?

mulderp commented 10 years ago

Ok, just as a note that it might be possible to move the view helper code into the Rack layer too, similar to rack-livereload where some new tags are inserted in the header: https://github.com/johnbintz/rack-livereload/blob/master/lib/rack/livereload.rb#L27-L36

mulderp commented 10 years ago

Update: I added a small test app for a Rack based Jammit approach here: https://github.com/mulderp/jammit_test/blob/master/config.ru

Also, scanning through the header with a regex and append the dependencies seems to work in principle.

mulderp commented 10 years ago

this overview on a node based middleware solution looks interesting in this context: http://blog.ponyfoo.com/2013/07/22/upgraded-asset-management