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

Optional manifest_path config #58

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Below is the default configuration showing all available options;
activate :imageoptim do |options|
# Use a build manifest to prevent re-compressing images between builds
options.manifest = true
options.manifest_path = './build/'

# Silence problematic image_optim workers
options.skip_missing_workers = true
Expand Down
10 changes: 9 additions & 1 deletion features/manifest.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ Feature: Manifest
Given a successfully built app at "basic-app" with flags "--verbose"
Then the following files should exist:
| build/imageoptim.manifest.yml |
Then the manifest should have the right timestamp for "build/images/table.jpg"
Then the manifest at "build" should have the right timestamp for "build/images/table.jpg"

Scenario: Save manifest at custom path
Given a fixture app "basic-app"
And app "basic-app" is using config "custom-manifest-path"
Given a successfully built app at "basic-app" with flags "--verbose"
Then the following files should exist:
| custom/manifest/path/imageoptim.manifest.yml |
Then the manifest at "custom/manifest/path" should have the right timestamp for "build/images/table.jpg"

Scenario: Don't write a manifest file if it is disabled
Given a fixture app "basic-app"
Expand Down
8 changes: 4 additions & 4 deletions features/support/step_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
expect(File.size(File.join(current_dir, img))).to be < size.to_i
end

Then(/^the manifest should have the right timestamp for "(.*?)"$/) do |file|
manifest = YAML.load(File.read(manifest_path))
Then(/^the manifest at "(.*?)" should have the right timestamp for "(.*?)"$/) do |manifest_base_path, file|
manifest = YAML.load(File.read(manifest_path(manifest_base_path)))
file_stamp = File.mtime(File.join(current_dir, file))
expect(manifest[file]).to eql(file_stamp)
end
Expand Down Expand Up @@ -40,6 +40,6 @@
expect(File.mtime(target)).not_to eql(@modification_times[target])
end

def manifest_path
File.join(current_dir, 'build', 'imageoptim.manifest.yml')
def manifest_path(manifest_base_path = 'build')
File.join(current_dir, manifest_base_path, 'imageoptim.manifest.yml')
end
3 changes: 3 additions & 0 deletions fixtures/basic-app/config-custom-manifest-path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
activate :imageoptim do |options|
options.manifest_path = 'custom/manifest/path'
end
29 changes: 21 additions & 8 deletions lib/middleman-imageoptim/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module Imageoptim
class Manifest
MANIFEST_FILENAME = 'imageoptim.manifest.yml'.freeze

attr_reader :app
attr_reader :app, :path_option

def initialize(app)
def initialize(app, path_option = nil)
@app = app
@path_option = path_option
end

def path
File.join(build_dir, MANIFEST_FILENAME)
@path ||= File.join(base_path, MANIFEST_FILENAME)
end

def build_dir
Expand All @@ -31,18 +32,28 @@ def resource(key)

private

def base_path
if path_option.present?
path_option
else
build_dir
end
end

def resources
@resources ||= load(path)
@resources ||= begin
if File.exist?(path)
YAML.load_file(path)
else
{}
end
end
end

def dump(source)
YAML.dump(source)
end

def load(path)
YAML.load(File.read(path))
end

def build(resources)
resources.inject({}) do |new_manifest, resource|
new_manifest[resource.to_s] = File.mtime(resource)
Expand All @@ -51,6 +62,8 @@ def build(resources)
end

def write(manifest)
FileUtils.mkpath(base_path) unless File.exists?(path)

File.open(path, 'w') do |manifest_file|
manifest_file.write(manifest)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/middleman-imageoptim/optimizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def optimizer
end

def manifest
@manifest ||= Manifest.new(app)
@manifest ||= Manifest.new(app, options.manifest_path)
end

def update_bytes_saved(bytes)
Expand Down
11 changes: 7 additions & 4 deletions lib/middleman-imageoptim/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ class Options
# Mapping of valid option names to default values
EXTENSION_OPTIONS = [
:image_extensions,
:manifest
:manifest,
:manifest_path
].freeze

OPTIONS = {
advpng: { level: 4 },
allow_lossy: false,
gifsicle: { interlace: false },
image_extensions: %w(.png .jpg .jpeg .gif .svg),
jpegoptim: { strip: ['all'], max_quality: 100 },
jpegtran: { copy_chunks: false, progressive: true, jpegrescan: true },
nice: true,
manifest: true,
manifest_path: nil,
nice: true,
optipng: { level: 6, interlace: false },
pack: true,
pngcrush: { chunks: ['alla'], fix: false, brute: false },
Expand Down Expand Up @@ -46,8 +49,8 @@ def symbolize_key(key)
end

def set_options(user_options)
OPTIONS.keys.each do |name|
instance_variable_set(:"@#{name}", user_options.fetch(name, OPTIONS[name]))
OPTIONS.each do |name, default_value|
instance_variable_set(:"@#{name}", user_options.fetch(name, default_value))
end
end
end
Expand Down