pakyow-assets 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: b2473155b9ea62813fe1db2b6771e89c2d639df0
4
- data.tar.gz: ffa5e7ab9d7b770fc96e48fc1e33e6120cf01179
3
+ metadata.gz: c322853e853a355500e4b045b22acbcbec3c8925
4
+ data.tar.gz: 29ce5c954d0d4c71025efdd12dfe70548155a9be
5
5
  SHA512:
6
- metadata.gz: 8d369efcc20b280a5219ea42c2c5ed71a8e6d61d4ab2864771944d5b1e4bc4f6c2cfccc015b68e2e8b09384b274e85a2e336d59880fb47c412cf85583ae82592
7
- data.tar.gz: 167a5a1a9f68772ade30c41296fe7f19bad85dfb5784be23ecb0f4744531e648b5896483d06e4d0585ae4d0d78305f588fa71433e79f535ddb8188adb7396300
6
+ metadata.gz: 3e8c251d6f988046ff36e19bfef1200d4d5eefdb3d5009eb1eca1e02652f519e7567d0284400212b1178c2b826fd9eae8febacdefed5a2afa30fc621746c5dba
7
+ data.tar.gz: 760555a99ee7ee4ba3078831066c40aec304b3ac455eb4228b56107cbd544a8d507e12a0e706df66f6186077e954caf51929d1b2fd67e127b6231a536f5bd7ca
data/CHANGELOG.md CHANGED
@@ -1,6 +1,17 @@
1
+ # 0.1.2 / 2015-11-30
2
+
3
+ * Fix bug causing html processor being registered too late, thus not mixing in
4
+ the fingerprinted asset names into the html
5
+ * Fingerprint contents after all assets are fingerprinted, not before
6
+ * Add option to not fingerprint assets
7
+ * Add Vary and Last-Modified cache headers
8
+ * Handle unknown asset types by copying them over during compilation
9
+
1
10
  # 0.1.1 / 2015-11-29
2
11
 
3
- * TODO
12
+ * Take dependent assets into account when building the hash
13
+ * Use sass gem rather than sassc; choosing reliability over speed
14
+ * Make sure assets middleware executes first
4
15
 
5
16
  # 0.1.0 / 2015-11-26
6
17
 
data/lib/assets.rb CHANGED
@@ -56,13 +56,24 @@ module Pakyow
56
56
  end
57
57
 
58
58
  def self.output_ext(ext)
59
- ".#{preprocessors[normalize_ext(ext)][:output_ext]}"
59
+ ".#{preprocessor_for_ext(ext)[:output_ext]}"
60
60
  end
61
61
 
62
62
  def self.preprocessor?(ext)
63
63
  preprocessors.values.map { |info| info[:output_ext] }.flatten.include?(normalize_ext(ext))
64
64
  end
65
65
 
66
+ def self.preprocessor_for_ext(ext)
67
+ ext = normalize_ext(ext)
68
+
69
+ preprocessors[ext] || {
70
+ block: nil,
71
+ output_ext: ext,
72
+ fingerprint_contents: false,
73
+ finterprint: false
74
+ }
75
+ end
76
+
66
77
  def self.compile_asset_at_path(asset, path)
67
78
  absolute_path = File.join(path, asset)
68
79
 
@@ -70,28 +81,42 @@ module Pakyow
70
81
  asset_ext = output_ext(File.extname(asset))
71
82
  asset_file = File.basename(asset, '.*')
72
83
 
84
+ if fingerprinted?(asset_ext)
85
+ compiled_asset = "#{asset_file + '-' + asset_hash(absolute_path) + asset_ext}"
86
+ else
87
+ compiled_asset = "#{asset_file + asset_ext}"
88
+ end
89
+
73
90
  compiled_path = File.join(
74
91
  Pakyow::Config.app.root,
75
92
  Pakyow::Config.assets.compiled_asset_path,
76
93
  asset_dir,
77
- "#{asset_file + '-' + asset_hash(absolute_path) + asset_ext}"
94
+ compiled_asset
78
95
  )
79
96
 
80
97
  unless File.exists?(compiled_path)
81
98
  FileUtils.mkdir_p(File.dirname(compiled_path))
82
- FileUtils.rm(Dir.glob(File.join(Pakyow::Config.app.root, Pakyow::Config.assets.compiled_asset_path, asset_dir, "#{asset_file}-*#{asset_ext}")))
99
+
100
+ if fingerprinted?(asset_ext)
101
+ glob_path = File.join(Pakyow::Config.app.root, Pakyow::Config.assets.compiled_asset_path, asset_dir, "#{asset_file}-*#{asset_ext}")
102
+ else
103
+ glob_path = File.join(Pakyow::Config.app.root, Pakyow::Config.assets.compiled_asset_path, asset_dir, asset_file + asset_ext)
104
+ end
105
+
106
+ FileUtils.rm(Dir.glob(glob_path))
83
107
  File.open(compiled_path, 'wb+') { |fp| fp.write(preprocess(absolute_path)) }
84
108
  end
85
109
 
86
110
  compiled_path
87
111
  end
88
112
 
89
- def self.preprocessor(*exts, output: nil, fingerprint_contents: false, &block)
113
+ def self.preprocessor(*exts, output: nil, fingerprint: false, fingerprint_contents: false, &block)
90
114
  exts.each do |ext|
91
115
  preprocessors[ext] = {
92
116
  block: block,
93
117
  output_ext: output || ext,
94
- fingerprint_contents: fingerprint_contents
118
+ fingerprint_contents: fingerprint_contents,
119
+ fingerprint: fingerprint
95
120
  }
96
121
  end
97
122
  end
@@ -103,11 +128,10 @@ module Pakyow
103
128
  end
104
129
 
105
130
  def self.preprocess(path)
106
- preprocessor = preprocessors[normalize_ext(File.extname(path))]
131
+ preprocessor = preprocessor_for_ext(File.extname(path))
107
132
  block = preprocessor[:block]
108
133
 
109
- contents = block.nil? ? File.open(path, 'rb').read : block.call(path)
110
- preprocessor[:fingerprint_contents] ? mixin_fingerprints(contents) : contents
134
+ block.nil? ? File.read(path) : block.call(path)
111
135
  end
112
136
 
113
137
  def self.precompile
@@ -120,21 +144,44 @@ module Pakyow
120
144
  compile_asset_at_path(asset, info[:path])
121
145
  absolute_path = File.join(info[:path], asset)
122
146
 
123
- fingerprint = Digest::MD5.file(absolute_path).hexdigest
147
+ fingerprint = asset_hash(absolute_path)
124
148
 
125
- fingerprinted_asset = File.join(
126
- File.dirname(asset),
127
- "#{File.basename(asset, '.*')}-#{fingerprint + output_ext(File.extname(asset))}",
128
- )
149
+ if fingerprinted?(File.extname(asset))
150
+ fingerprinted_asset = File.join(
151
+ File.dirname(asset),
152
+ "#{File.basename(asset, '.*')}-#{fingerprint + output_ext(File.extname(asset))}",
153
+ )
154
+ else
155
+ fingerprinted_asset = File.join(
156
+ File.dirname(asset),
157
+ File.basename(asset, '.*') + output_ext(File.extname(asset)),
158
+ )
159
+ end
129
160
 
130
161
  replaceable_asset = File.join(
131
162
  File.dirname(asset),
132
163
  File.basename(asset, '.*') + output_ext(File.extname(asset)),
133
164
  )
134
165
 
135
- manifest[replaceable_asset] = fingerprinted_asset
166
+ manifest[replaceable_asset] = {
167
+ original_ext: File.extname(asset),
168
+ fingerprinted_asset: fingerprinted_asset
169
+ }
136
170
  end
137
171
  end
172
+
173
+ base = File.join(Pakyow::Config.app.root, Pakyow::Config.assets.compiled_asset_path)
174
+
175
+ manifest.each do |replaceable_asset, info|
176
+ next unless fingerprint_contents?(info[:original_ext])
177
+
178
+ path = File.join(base, info[:fingerprinted_asset])
179
+
180
+ content = File.read(path)
181
+ File.open(path, 'wb') { |file|
182
+ file.write(mixin_fingerprints(content))
183
+ }
184
+ end
138
185
  end
139
186
 
140
187
  def self.manifest
@@ -144,8 +191,8 @@ module Pakyow
144
191
  def self.mixin_fingerprints(content)
145
192
  return content if content.nil? || content.empty?
146
193
 
147
- manifest.each do |asset, fingerprinted_asset|
148
- content = content.gsub(asset, fingerprinted_asset)
194
+ manifest.each do |asset, info|
195
+ content = content.gsub(asset, info[:fingerprinted_asset])
149
196
  end
150
197
 
151
198
  content
@@ -162,5 +209,13 @@ module Pakyow
162
209
  return [] if block.nil?
163
210
  block.call(absolute_path)
164
211
  end
212
+
213
+ def self.fingerprinted?(ext)
214
+ preprocessor_for_ext(ext)[:fingerprint]
215
+ end
216
+
217
+ def self.fingerprint_contents?(ext)
218
+ preprocessor_for_ext(ext)[:fingerprint_contents]
219
+ end
165
220
  end
166
221
  end
data/lib/middleware.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  module Pakyow
2
4
  module Assets
3
5
  class Middleware
@@ -21,10 +23,12 @@ module Pakyow
21
23
  'Content-Type' => Rack::Mime.mime_type(File.extname(path))
22
24
  }
23
25
 
24
- if Pakyow::Config.assets.cache
26
+ if Pakyow::Config.assets.cache && Pakyow::Assets.fingerprinted?(File.extname(path))
25
27
  mtime = File.mtime(path)
26
28
  headers['Age'] = (Time.now - mtime).to_i
27
29
  headers['Cache-Control'] = 'public, max-age=31536000'
30
+ headers['Vary'] = 'Accept-Encoding'
31
+ headers['Last-Modified'] = mtime.httpdate
28
32
  end
29
33
 
30
34
  [200, headers, File.open(path)]
data/lib/pakyow-assets.rb CHANGED
@@ -8,7 +8,7 @@ require_relative 'preprocessors/image-preprocessor'
8
8
  require_relative 'preprocessors/javascript-preprocessor'
9
9
  require_relative 'preprocessors/sass-preprocessor'
10
10
 
11
- Pakyow::App.after :init do
11
+ Pakyow::App.after :configure do
12
12
  config.assets.stores.each_pair do |name, path|
13
13
  Pakyow::Assets.register_path_with_name(path, name)
14
14
  end
@@ -1,7 +1,7 @@
1
1
  require 'yui/compressor'
2
2
 
3
- Pakyow::Assets.preprocessor :css, fingerprint_contents: true do |path|
4
- content = File.open(path).read
3
+ Pakyow::Assets.preprocessor :css, fingerprint: true, fingerprint_contents: true do |path|
4
+ content = File.read(path)
5
5
 
6
6
  if Pakyow::Config.assets.minify
7
7
  begin
@@ -1 +1 @@
1
- Pakyow::Assets.preprocessor :png, :jpg, :gif, :ico
1
+ Pakyow::Assets.preprocessor :png, :jpg, :gif, :ico, fingerprint: true
@@ -1,7 +1,7 @@
1
1
  require 'yui/compressor'
2
2
 
3
- Pakyow::Assets.preprocessor :js, fingerprint_contents: true do |path|
4
- content = File.open(path).read
3
+ Pakyow::Assets.preprocessor :js, fingerprint: true, fingerprint_contents: true do |path|
4
+ content = File.read(path)
5
5
 
6
6
  if Pakyow::Config.assets.minify
7
7
  begin
@@ -1,7 +1,7 @@
1
1
  require 'sass'
2
2
  require 'yui/compressor'
3
3
 
4
- Pakyow::Assets.preprocessor :scss, :sass, output: :css, fingerprint_contents: true do |path|
4
+ Pakyow::Assets.preprocessor :scss, :sass, output: :css, fingerprint: true, fingerprint_contents: true do |path|
5
5
  content = Sass::Engine.for_file(path, {}).render
6
6
 
7
7
  if Pakyow::Config.assets.minify
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Pakyow
2
2
  module Assets
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.1.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pakyow-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-29 00:00:00.000000000 Z
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pakyow-support
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pakyow-core
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pakyow-presenter
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sass
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.4'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.4'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yui-compressor
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ~>
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0.12'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.12'
83
83
  description: Asset Handling for Pakyow
@@ -109,19 +109,18 @@ require_paths:
109
109
  - lib
110
110
  required_ruby_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - ">="
112
+ - - '>='
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - ">="
117
+ - - '>='
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.5
122
+ rubygems_version: 2.0.14
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Pakyow Assets
126
126
  test_files: []
127
- has_rdoc: