middleman/middleman-sprockets — pull request #23 by bhollis (2013-04-15 00:53:40+00:00)
Title: Building CSS/JS files provided by gems

With my latest changes, static assets are always copied to the build output. However, CSS/JS is not - it can only be used by requiring/importing it into an app's local CSS/JS. Right now it's actually very misleading because Sprockets' Rack app will happily serve those assets in the preview server, but they won't get built!

I have three ideas for how to provide the ability for users to declare that a CSS or JS file from a gem be built directly into their project without having to include it in something else:

1. Add a `:build_external_assets` config that lets you list logical paths of assets to be built, which will be directly be added to the Sitemap and thus built. This is straightforward, but manual.
2. Override `javascript_include_tag` and `stylesheet_link_tag` to add the asset to the sitemap when invoked. This makes things more automatic, though direct references to assets (without using those helpers) misses out. It's also sort of scary magic - I could see it going wrong and being hard for users to understand why.
3. Override `find_asset` on our Sprockets environment and only add assets to the sitemap that get looked up. This has the potential to be the most magical - we could also use it to *only* build static assets that are referenced in some way. The logic could get pretty complicated, though, as we attempt to avoid building files that are used only for require/import but build stuff that's referenced directly.

For 2 and 3 we'd require something like middleman/middleman#623 to make it really work. It would also mean the sitemap itself would be harder to inspect or traverse (especially during preview) because it'd change as files were rendered.

Obviously we can implement combinations of the above as well. I'm happy to hack on this, but I'd like to get thoughts on it first.