简体   繁体   中英

Include external JS in Rails asset pipeline that gets bundled during deploys

I reviewed this question already - Including external libraries using the Rails 3.1 asset pipeline - which is basically what I'm looking for with one caveat: I'd like to include the external url in the asset pipeline so that when I deploy to production, the external url gets bundled into a file with the other local files that were required.

More details:

My use case here is that I have found a plugin on GitHub that I like, and being a good member of the open source community, I'd like to help contribute back. The most time efficient technique that I'm considering is to fork the repo, then point my local project that needs that file to the raw source of the required JS file during development, but have that file get bundled with the rest of my application when I deploy to staging / production. This would allow me to keep the plugin tied closely to the parent project, but keep things on my own track so that I can decide when to merge in updates from the base project as well as submit any fixes I have back to the parent with easy pull requests.

With that said, I have not been able to find any documentation about how to do this with the current rails JS asset pipeline or if it is possible at all. Some quick tests locally point to the idea that this works in a css file but not for js files. Can anyone confirm if this is possible? Thanks!

I would use something like this custom rake task . Basically what you do is write that rake task into the beginning of your deploy script, but modified to put the file in your vendor/assets folder (or where ever you want that is pulled into the asset pipeline.)

That way you pull the requested file on deploy, but it's there in your code when your asset pipeline bundles everything up.

Here is the code in-case the link rots:

namespace :remote_file do
  desc "Get a file from a remote server"
  task :fetch do
  # based on http://snippets.dzone.com/posts/show/2469
  # http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg
  Net::HTTP.start("farm1.static.flickr.com") do |http|
    resp = http.get("/92/218926700_ecedc5fef7_o.jpg")
    open("fun.jpg", "w") { |file| file.write(resp.body) } 
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM