esvg 4.6.2 → 4.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41bb830e6af4939063aeca838f5c69c550a851f71e701599ed3d4cbb52a42b5c
4
- data.tar.gz: f494f42e0e9f9f847145a8f9d65658a9bf3104741f4a4bf513866cb6d92f942b
3
+ metadata.gz: f283459b842a4ae61b966de4db35aa1be8520bddbdcb3f24ce6c9784b5797137
4
+ data.tar.gz: '078008b881bdbbc81cfb581bb7a0dc7b692087acd37bd838fad1cf62ccc1d543'
5
5
  SHA512:
6
- metadata.gz: bbce86545a336224d47db5219352407210fa16b16efc85c6f984058ee3ba09ea559a24122488c03e02bef076f141895107a70a2ce5f5868898f73c3dd708037e
7
- data.tar.gz: e62b4ad90f511af5671df2d0c39c9c54e9506b25f580da403f84015cefb77f4fc843d2def09b42382bb52bb23991f70beaca2c19402883af4ac233c6aaddab14
6
+ metadata.gz: 4e34a8ff5f0f8b33c6a8c900336facc7649bec44e2aa99dda6601da1cf06a2d9372e9eccf089d74f9a74a3926d34ed0094897c3af4a83c97537c7ddcd165de75
7
+ data.tar.gz: 28bb6320c1f83fbed93d1b32100bfeb3146f2e1e74668beceaf810d9cbdb2ce94c1a3fc5c64627b262374b2bf3d18ff66a970e3ab52cd6e67d5c3d9369630672
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ### 4.6.3 (2018-12-19)
4
+ - Fix: Reworked configuration model, fixing build optimization failures.
5
+
6
+ ### 4.6.2 (2018-12-18)
7
+ - New: Config is reloaded on change in development mode (Rails, Jekyll)
8
+
9
+ ### 4.6.1 (2018-12-17)
10
+ - New: Embeds All Svgs in development mode (making reubilding after change unnecessary)
11
+
3
12
  ### 4.6.0 (2018-12-12)
4
13
  - New: `presets` configuration. Configure `presets: { icon: { height: '1em', fill: 'currentColor' }}` in esvg.yml and set option `preset: 'icon'` to apply these defaults options.
5
14
  - New: `sizes` configuration. Configure `sizes: { sm: { height: '10px' }, med: { height: '14px' }, … }` in esvg.yml and set option `size: 'sm'` to add load those size configuration options.
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  ## Usage: Rails
30
30
 
31
- Add `Esvg.precompile_assets` to your `config/initializers/assets.rb` to add build with `rake assets:precomile`.
31
+ Add `Esvg.precompile_assets` to your `config/initializers/assets.rb` to add build with `rake assets:precompile`.
32
32
 
33
33
  Add SVG files to your `app/assets/svgs/` directory.
34
34
 
data/exe/esvg CHANGED
@@ -8,6 +8,7 @@ require 'esvg'
8
8
  options = {
9
9
  core: false,
10
10
  fingerprint: false,
11
+ cli: true
11
12
  }
12
13
 
13
14
  OptionParser.new do |opts|
@@ -49,6 +49,7 @@ module Esvg
49
49
  extend self
50
50
 
51
51
  include Esvg::Utils
52
+
52
53
  def new(options={})
53
54
  @svgs ||=[]
54
55
  c = config(options)
@@ -110,10 +111,6 @@ module Esvg
110
111
  defined?(Rails) || File.exist?("./bin/rails")
111
112
  end
112
113
 
113
- def rails_production?
114
- rails? && Rails.env.production?
115
- end
116
-
117
114
  def html_safe(input)
118
115
  input = input.html_safe if rails?
119
116
  input
@@ -121,9 +118,9 @@ module Esvg
121
118
 
122
119
  # Add SVG build task to `rake assets:precompile`
123
120
  def precompile_assets
124
- if rails_production? && defined?(Rake)
121
+ if rails? && defined?(Rake)
125
122
  ::Rake::Task['assets:precompile'].enhance do
126
- new(gzip: true, print: true).build
123
+ svgs.map(&:build)
127
124
  end
128
125
  end
129
126
  end
@@ -170,37 +167,46 @@ module Esvg
170
167
 
171
168
  if rails? || options[:rails]
172
169
  config.merge!(CONFIG_RAILS)
170
+ config[:env] ||= Rails.env
173
171
  elsif defined?(Jekyll)
174
172
  config.merge!(CONFIG_JEKYLL)
173
+ config[:env] ||= Jekyll.env
175
174
  end
176
175
 
176
+ config.merge!(options)
177
+
177
178
  if path = paths.select{ |p| File.exist?(p)}.first
178
179
  options[:config_file] = path
179
180
  config.merge!(deep_symbolize_keys(YAML.load(File.read(path) || {})))
181
+
182
+ # Allow CLI passed options to override config file
183
+ config.merge!(options) if options[:cli]
180
184
  else
181
185
  options[:config_file] = false
182
186
  end
183
187
 
184
- config.merge!(options)
185
-
186
188
  if defined? Jekyll
187
- config[:build] = File.join(config[:destination], config[:build])
188
- config[:source] = File.join(config[:source_dir], config[:source])
189
+ config[:build] = File.join(config[:destination], config[:build])
190
+ config[:source] = File.join(config[:source_dir], config[:source])
189
191
  end
190
192
 
191
- config[:filename] = File.basename(config[:filename], '.*')
193
+ config[:filename] = File.basename(config[:filename], '.*')
192
194
 
193
- config[:pwd] = File.expand_path Dir.pwd
194
- config[:source] = File.expand_path config[:source] || config[:pwd]
195
- config[:build] = File.expand_path config[:build] || config[:pwd]
196
- config[:assets] = File.expand_path config[:assets] || config[:pwd]
195
+ config[:pwd] = File.expand_path Dir.pwd
196
+ config[:source] = File.expand_path config[:source] || config[:pwd]
197
+ config[:build] = File.expand_path config[:build] || config[:pwd]
198
+ config[:assets] = File.expand_path config[:assets] || config[:pwd]
199
+ config[:root] ||= config[:pwd] # For relative paths on print
197
200
 
198
- config[:temp] = config[:pwd] if config[:temp].nil?
199
- config[:temp] = File.expand_path File.join(config[:temp], '.esvg-cache')
201
+ config[:temp] = config[:pwd] if config[:temp].nil?
202
+ config[:temp] = File.expand_path( File.join(config[:temp], '.esvg-cache') )
200
203
 
201
- config[:aliases] = load_aliases(config[:alias])
204
+ config[:aliases] = load_aliases(config[:alias])
202
205
  config[:flatten_dir] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
203
- config[:read] = Time.now.to_i
206
+ config[:read] = Time.now.to_i
207
+ config[:env] ||= 'development'
208
+ config[:print] ||= true unless config[:env] == 'production'
209
+ config[:optimize] = true if config[:env] == 'production'
204
210
 
205
211
  config
206
212
  end
@@ -9,7 +9,7 @@ Jekyll::Hooks.register :site, :post_read do |site|
9
9
  site.exclude.push '.esvg-cache'
10
10
  config = {
11
11
  source_dir: site.config['source'],
12
- destination: site.config['destination']
12
+ destination: site.config['destination']
13
13
  }.merge site.config["esvg"] || {}
14
14
 
15
15
  Jekyll.esvg = Esvg.seed_cache(config)
@@ -14,22 +14,29 @@ module Esvg
14
14
  @config = options
15
15
  @symbols = []
16
16
  @svgs = []
17
- @files_loaded = 0
18
17
  read_cache
19
18
 
20
19
  load_files
21
20
  end
22
21
 
22
+ def production?
23
+ @config[:env] == 'production'
24
+ end
25
+
23
26
  def config
24
27
  # use cached configuration
25
- if !Esvg.rails_production? && config_expired? && config_changed?
26
- puts "LOADING CONFIG"
28
+ if !production? && config_expired? && config_changed?
29
+ puts "Reloading ESVG config: #{print_path( @config[:config_file] )}" if @config[:print]
27
30
  @config = Esvg.update_config( @config )
28
31
  else
29
32
  @config
30
33
  end
31
34
  end
32
35
 
36
+ def print_path( path )
37
+ sub_path( @config[:root], path )
38
+ end
39
+
33
40
  def config_expired?
34
41
  @config[:throttle_read] < (Time.now.to_i - @config[:read])
35
42
  end
@@ -38,34 +45,44 @@ module Esvg
38
45
  @config[:config_file] && @config[:read] < File.mtime( @config[:config_file] ).to_i
39
46
  end
40
47
 
48
+ def loaded_recently?
49
+ @last_load && (Time.now.to_i - @last_load) < @config[:throttle_read]
50
+ end
51
+
41
52
  def load_files
42
- return if @config[:throttle_read] < (Time.now.to_i - @files_loaded)
53
+ return if loaded_recently?
43
54
 
44
- files = Dir[File.join(@config[:source], '**/*.svg')].uniq.sort
55
+ files = Dir[File.join(@config[:source], '**/*.svg')].uniq.sort
56
+ size_before = @symbols.size
57
+ added = []
58
+ removed = []
45
59
 
46
- if files.empty? && @config[:print]
47
- puts "No svgs found at #{@config[:source]}"
48
- return
49
- end
50
-
51
- # Remove deleted files
52
- @symbols.reject(&:read).each { |s| @symbols.delete(s) }
60
+ # Remove all files which no longer exist
61
+ @symbols.reject(&:exist?).each { |s|
62
+ removed.push @symbols.delete(s)
63
+ }
53
64
 
54
65
  files.each do |path|
55
66
  unless @symbols.find { |s| s.path == path }
56
67
  @symbols << Symbol.new(path, self)
68
+ added.push @symbols.last
57
69
  end
58
70
  end
59
71
 
72
+ if @config[:print]
73
+ puts %Q{Read #{files.size} SVGs from #{print_path( @config[:source] )}}
74
+ puts %Q{ Added: #{added.size} SVG#{'s' if added.size != 1} } if added.size > 0
75
+ puts %Q{ Removed: #{removed.size} SVG#{'s' if removed.size != 1} } if removed.size > 0
76
+ end
77
+
60
78
  @svgs.clear
61
79
 
62
- sort(@symbols.group_by(&:group)).each do |name, symbols|
80
+ sort(@symbols.group_by(&:dir)).each do |name, symbols|
63
81
  @svgs << Svg.new(name, symbols, self)
64
82
  end
65
83
 
66
- @files_loaded = Time.now.to_i
67
-
68
- puts "Read #{@symbols.size} files from #{@config[:source]}" if @config[:print]
84
+ @last_load = Time.now.to_i
85
+ write_cache if cache_stale?
69
86
 
70
87
  end
71
88
 
@@ -82,11 +99,11 @@ module Esvg
82
99
  @svgs.each do |file|
83
100
  write_file file.path, js(file.embed)
84
101
 
85
- puts "Writing #{file.path}" if @config[:print]
102
+ puts "Writing #{print_path( file.path )}" if @config[:print]
86
103
  paths << file.path
87
104
 
88
105
  if !file.asset && @config[:gzip] && gz = compress(file.path)
89
- puts "Writing #{gz}" if @config[:print]
106
+ puts "Writing #{print_path( gz )}" if @config[:print]
90
107
  paths << gz
91
108
  end
92
109
  end
@@ -96,12 +113,12 @@ module Esvg
96
113
  end
97
114
 
98
115
  def write_cache
99
- puts "Writing cache" if @config[:print]
116
+ puts "Writing cache: #{ print_path( File.join( @config[:temp], @config[:cache_file]) )}" if @config[:print]
100
117
  write_tmp @config[:cache_file], @symbols.map(&:data).to_yaml
101
118
  end
102
119
 
103
120
  def read_cache
104
- (YAML.load(read_tmp @config[:cache_file]) || []).each do |c|
121
+ (YAML.load(read_tmp(@config[:cache_file])) || []).each do |c|
105
122
  @config[:cache] = c
106
123
  @symbols << Symbol.new(c[:path], self)
107
124
  end
@@ -109,7 +126,7 @@ module Esvg
109
126
 
110
127
  # Embed svg symbols
111
128
  def embed_script(names=nil)
112
- if Esvg.rails_production?
129
+ if production?
113
130
  embeds = buildable_svgs(names).map(&:embed)
114
131
  else
115
132
  embeds = find_svgs(names).map(&:embed)
@@ -136,7 +153,7 @@ module Esvg
136
153
 
137
154
  def find_symbol(name, fallback=nil)
138
155
  # Ensure that file changes are picked up in development
139
- load_files unless Esvg.rails_production?
156
+ load_files unless production?
140
157
 
141
158
  name = get_alias dasherize(name)
142
159
 
@@ -18,12 +18,20 @@ module Esvg
18
18
  @parent.config
19
19
  end
20
20
 
21
+ def exist?
22
+ File.exist?(@path)
23
+ end
24
+
25
+ def dir
26
+ @group
27
+ end
28
+
21
29
  def read
22
- return if !File.exist?(@path)
30
+ return if !exist?
23
31
 
24
32
  # Ensure that cache optimization matches current optimization settings
25
33
  # If config has changed name, reset optimized build (name gets baked in)
26
- if changed? || @svgo_optimized != svgo? || name != file_name
34
+ if changed? || @svgo_optimized != optimize? || name != file_name
27
35
  @optimized = nil
28
36
  @optimized_at = nil
29
37
  end
@@ -95,7 +103,7 @@ module Esvg
95
103
  defs: @defs,
96
104
  optimized: @optimized,
97
105
  optimized_at: @optimized_at,
98
- svgo_optimized: svgo? && @svgo_optimized
106
+ svgo_optimized: optimize? && @svgo_optimized
99
107
  }
100
108
  end
101
109
 
@@ -160,17 +168,26 @@ module Esvg
160
168
  %Q{<use #{attributes(options)}></use>}
161
169
  end
162
170
 
163
- def svgo?
164
- config[:optimize] && !!Esvg.node_module('svgo')
171
+ # Only optimize if
172
+ # - Configuration asks for it
173
+ # - SVGO is present
174
+ # - If Rails is present
175
+ def optimize?
176
+ config[:optimize] && !!Esvg.node_module('svgo') && config[:env] == 'production'
165
177
  end
166
178
 
167
179
  def optimize
168
180
  read if changed?
169
181
 
170
182
  # Only optimize again if the file has changed
171
- return @optimized if @optimized && @optimized_at && @optimized_at > @mtime
183
+ if @optimized && @optimized_at && @optimized_at > @mtime
184
+ return @optimized
185
+ end
186
+
187
+ # Only optimize if SVGO is installed
188
+ if optimize?
189
+ puts "Optimizing #{name}.svg" if config[:print]
172
190
 
173
- if svgo?
174
191
  response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@content}' -o -})
175
192
  if !response[0].empty? && response[2].success?
176
193
  @optimized = response[0]
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "4.6.2"
2
+ VERSION = "4.6.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.6.2
4
+ version: 4.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-18 00:00:00.000000000 Z
11
+ date: 2018-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler