Might be nice to have an option to change base dir for images
This is a feature request, but I'm not sure how to label it as such.
I use juicer to compress css files in a src directory and write the compressed files to a build directory. Also as part of the build, I copy images from the src to the build directory. The problem with this approach is that when juicer resolves image paths in the css files, they resolve to the src directory.
This is probably not the most elegant way to solve this problem, but I added a couple of parameters (url_src and url_dest) to juicer and added the following code to stylesheet_merger.rb:
dir = File.expand_path(File.dirname(file))
if (@url_src && @url_dest) # this if statement is new
url_src = File.expand_path(@url_src)
url_dest = File.expand_path(@url_dest)
dir.gsub!(url_src, url_dest)
end
So if url_src is src and url_dest is build, then url's in the src directory get resolved as if they were in the dest directory, which is where they get copied to by the build. I can then copy my entire build directory (images and compressed css files) to my hosting provider and the relative image urls in the css files are correct.
I don't know if other people do it this way, but it works well for me.
Thanks, Dallan! This is actually a feature I could use myself. Your input on solution looks good, but I will take a little more time to think about it to make sure we get all corners covered. If you want to make a patch out of it including test cases I'd be more than happy to merge it in!
Well, my solution could break cache-busters because it changes the URLs upstream, before the image filename timestamp is read. So when copying images from src to dest, you'd have to keep the src timestamps. But I wasn't sure how to implement a solution that changed src to dest urls after the cachebuster was calculated, so I went with the simple-but-lesser approach.
So with that caveat, if you'd still like me to submit a patch with some test cases I'm happy to do that. However, I haven't done anything like that before so I'd appreciate a couple of sentences on what you're looking for.
Hi Dallan! Sorry for the lousy reply-time. I got completely swamped completing my book, and had to cut anything that didn't directly pitch in to that. Sorry.
If you want some help on the tests, will looking at the existing test cases help? If you need more specific advice, don't hesitate to ask, and I'll help as best I can. You could shoot me an email on [email protected] if you want.