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

Make optimised images depend on the configuration #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/image_optim/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'image_optim'
require 'set'

class ImageOptim
# Adds image_optim as preprocessor for gif, jpeg, png and svg images
Expand Down Expand Up @@ -27,6 +28,10 @@ class Railtie < Rails::Railtie

@image_optim = ImageOptim.new(options(app))

register_dependency_resolver(app, 'image-optim-config') do
options(app).to_h
end

register_preprocessor(app) do |*args|
if args[1] # context and data arguments in sprockets 2
optimize_image_data(args[1])
Expand All @@ -35,6 +40,7 @@ class Railtie < Rails::Railtie
{
:data => optimize_image_data(input[:data]),
:charset => nil, # no gzipped version with rails/sprockets#228
:dependencies => Set.new.add('image-optim-config'),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using Set[] or to_set would be nicer

}
end
end
Expand Down Expand Up @@ -79,5 +85,17 @@ def register_preprocessor(app, &processor)
end
end
end

def register_dependency_resolver(app, scheme, &resolver)
return unless Sprockets::Environment.method_defined?(:register_dependency_resolver)

if app.assets
app.assets.register_dependency_resolver scheme, &resolver
else
app.config.assets.configure do |env|
env.register_dependency_resolver scheme, &resolver
end
end
end
end
end