Rails / Assets pipelines: dynamically list assets included in a manifest

I have been successfully using the asset pipeline for several months. Now I would like to download some of my JS files asynchronously (using the yepnope library). It works well when config.assets.debug false.

But in development mode (where config.assets.debugusually true) the best option for me would be to dynamically retrieve a list of all js files included in my manifests (I got 2 manifests: application.js and externals.js) to give them yepnope for asynchronous loading.

Any idea to do this?

+5
source share
1 answer

, , ( , , , )

# given a list of Sprockets manifests, returns a flattened array of dependency paths
def paths_for_manifests(manifests = [])
  manifests.map do |manifest|
    Rails.application.assets[manifest].dependencies.map{|d| "/assets/#{d.logical_path}"}
  end.flatten
end

( JS/CoffeeScript):

paths = <%= paths_for_manifests(%w(externals.js application.js)) %>
+8

All Articles