Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with Rails asset pipeline as sprocket preprocessor #2

Closed
kristianmandrup opened this issue Aug 25, 2012 · 16 comments
Closed

Comments

@kristianmandrup
Copy link

Check out this article :)

http://psionides.eu/2012/05/06/extending-asset-pipeline-with-custom-preprocessors/

class RootUrlPreprocessor < Sprockets::Processor
  def evaluate(context, locals)
    data.gsub(%r"\bHOSTNAME\b", "myserver.com")
  end
end

The context is an instance of Sprockets::Context; you can use it to e.g. read the current file’s path or content type, generate paths to asset files or access asset pipeline configuration. You also have access to several asset helpers from ActionView, like javascript_include_tag, image_tag etc.

http://rubydoc.info/gems/sprockets/2.0.3/Sprockets/Context

I think you would need fx content_type and somehow access the full path of the asset and run it through image_optim.

# in config/initializers/sprockets.rb
Rails.application.assets.register_preprocessor('application/javascript',
                                               RootUrlPreprocessor)

Just found an example here: https://github.com/botandrose/sprockets-image_compressor

Maybe you could join forces ;)

Sweet and simple! Yeah :)

@toy
Copy link
Owner

toy commented Aug 26, 2012

image_optim is not the fastest thing (for png files) to run so I am not sure about running every time during deployment, it is better to optimize images before putting them into app repository. Though this needs to be checked.
Also I am really low on time, so don't hesitate if you have time and you are ready to do this :)

@botandrose
Copy link

Hi, guys, I really like the idea of replacing the core of sprockets-image_compressor with image_optim, but there would be one big regression: Image_optim won't currently work on Heroku because they don't have the binaries installed on their servers. I've statically compiled Heroku-friendly pngcrush and jpegoptim binaries and vendored them into sprockets-image_compressor to get around this issue. @toy, what are the odds of something similar happening here?

P.S. I'm running sprockets-image_compressor on deployment on about a dozen sites, and while not blazing fast, the overall asset compilation speed is acceptable.

@toy
Copy link
Owner

toy commented Aug 27, 2012

It may be a better idea to create separate gem with statically compiled binaries per platform and setting default_bin for each worker, otherwise image_optim will be a heavy gem. There is a problem with pngout as it can not be redistributed «without consent and agreement of creator».
It may be better to tune settings of compressors, as now they are making process very slow gaining few extra percents of compression.
Just to mention — I already used image_optim for paperclip post processor and it works very well there.

@toy
Copy link
Owner

toy commented Aug 30, 2012

@botandrose: just to be sure that you read previous one

@kristianmandrup
Copy link
Author

Thanks guys. Good discussion and nice points. Not sure what the outvome will be but hopefully someone wilvome up with an even better solution integrating all these nice solutions while still ensuring good performance etc.

@feliperaul
Copy link

Guys, while your thrilling effort doesn't take off, how do you suggest setting up a nice integration of image_optim with a production rails app ?

What about not running every time during deploy as an asset preprocessor, but maybe every time AFTER deploy ? So deploy doesn't take forever and you leave the script running after deploy is finished ?

@mooktakim
Copy link

For those that came here after googling (like me).
I have created a bin gem that allows you to run image_optim on heroku:
https://github.com/mooktakim/image_optim_bin

@toy
Copy link
Owner

toy commented Dec 8, 2013

Creating sprocket preprocessor appeared to be very simple, checkout rails branch, especially: https://github.com/toy/image_optim/blob/rails/lib/image_optim/railtie.rb

@kristianmandrup
Copy link
Author

Cool 👍

@toy
Copy link
Owner

toy commented Nov 1, 2014

@mooktakim I've finally managed to create bin pack for image_optimimage_optim_pack. Working on heroku — https://iopack.herokuapp.com/.
Pack doesn't include svgo and I am not sure that it will be easy to pack it and doesn't include pngout due to its license. I'll try to contact Ardfry Imaging, LLC about including it. Could you please tell me if you've got an agreement with them and if it was easy?

@mooktakim
Copy link

Hey @toy,
I didn't get in contact with them. I was just using it for myself. I'm not sure how easy it would be to get permission to include it.

@ghostropic
Copy link

Hey @toy
I'm attempting to get image_optim_pack pushed to Heroku. I tried using @mooktakim's build pack but fails while precompiling assets. Do you know of any other methods of getting your bin pack to build on Heroku?

@toy
Copy link
Owner

toy commented Feb 14, 2017

@ghostropic
Do I understand right that image_optim_pack itself also doesn't work? Can you share errors?
One way to get binaries is to build them using Makefile from that gem.
Maybe it is also better to move this to a separate issue for that project.

@ghostropic
Copy link

ghostropic commented Feb 15, 2017

@toy Sorry for the confusion. Image_optim_pack does work locally, but isn't useful to me unless I can get it working on Heroku, so I thought i'd ask if you had any leads on a build pack. I suppose my only option though is to learn to write my own.

@toy
Copy link
Owner

toy commented Feb 15, 2017

@ghostropic Ah no, I understood you, but my questions were confusing. As I understand heroku gives you ubuntu servers and image_optim_pack should work on ubuntu. So does it not work on heroku and which errors do you get? And one way to get binaries is to use make on machine you are targeting, so on heroku instance.

@toy
Copy link
Owner

toy commented Feb 15, 2017

@ghostropic As there was already a heroku app to check versions, I've added there a very basic upload with optimisation for checking if everything works, and it seems it does: https://iopack.herokuapp.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants