简体   繁体   中英

How do you package static assets in a gem for use in Sinatra applications?

Let's say you have a gem which contains only static files and no code (other than the Ruby metadata in the gemspec, Rakefiles, etc.). The static files are things like CSS, JS, and images.

Now you'd like to use those assets in a Sinatra web application that's using sinatra-assetpack or another similar asset pipeline. We don't want these assets as part of each project they're included in, because they may change, and we don't want to update N projects.

What's the right way to include static content in multiple Sinatra applications?

The way I've done it is to add static assets to the vendor directory of a gem. Typically I'll set up a project with bundle install --binstubs --path vendor , so that directory is there anyway, and I saw some other Rails gems were using vendor, so I put stuff in there. It can make paths that reference the assets from the gem library files horribly long, but it's being abstracted away for the gem consumer anyway.

See Rack::JQuery for an example. Also, so that I don't have to pfaff around which assets should get built while having to constantly update the repo, I add the version number to the static files (you could do this with a directory too) and use this line in the gemspec to only add the current resources to the gem.

I've done it with CSS too (and a few others, it becomes an addiction:) but as you can see, at some point I chose a poor way to name the directories, but I'll stick with it now for the sake of consistency.

Hope that helps. If you find a better way, do let me know!

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