documentcloud / jammit

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

include assets from other directories #28

Closed weepy closed 14 years ago

weepy commented 14 years ago

I'm not sure if this is possible already, but it didn't seem to work. What I'd like to do is load JS/CSS from other directories, e.g.

javascripts:
  playing:
    - app/javascripts/playing/*.js
    - ~/sprockets/play.js

Is this possible ?

documentcloud commented 14 years ago

Jammit will happily load assets from wherever (relative paths are relative to Rails.root), but Rails won't directly serve files from outside the public directory. For this reason, it's recommended to either:

The first two options are best. Here's a similar ticket:

http://github.com/documentcloud/jammit/issues/closed#issue/5

weepy commented 14 years ago

Could Jammit not serve the files itself when in development mode ? It could load the assets via some kind of specific route.

documentcloud commented 14 years ago

We could do that, but I don't like the security hole. Rails only serves files from public because it's gives you a strong line between an open file, available for download, and your application, which must be kept secret. If Jammit just willy-nilly served up any file that matched a Directory glob, (usually a .js or a .css, but still) it would make it far too easy to expose your source by accident.

If you'd really like to keep them in a different folder, and symlinks aren't cutting it for you, then you can always add a controller and route to serve the files up directly for development.

weepy commented 14 years ago

It needn't be a security hole, because it could check that the file served matched a line in assets.yml (I.e. it's served anyway). Additionally it would only work in development mode, and revert to the normal packaged version in production.

I think it would be a great feature to have that would help Jamit address one of the rather broken features of Javascript development, namely reuse of source.

documentcloud commented 14 years ago

Hmm. Fair enough. I'd be glad to take a patch that works just like you just outlined, if you feel like contributing one.

henrypoydar commented 14 years ago

We took a slightly different approach that allowed us to keep our plain JS files in app/javascripts while keeping them hidden in production: serving the files via rack middleware while in development mode. HTH someone (credit to @binary42 for the idea)

jcf commented 14 years ago

@hpoydar would you like to share the middleware you used?

henrypoydar commented 14 years ago

Essentially we customized Rack::Static - http://github.com/rack/rack/blob/master/lib/rack/static.rb

jcf commented 14 years ago

@hpoydar I just wrote a quick bit of middleware that serves JS files in app/javascripts.

http://gist.github.com/494167

I'd be interested to see how you solved serving assets and whether you avoided including app in your development URI paths.