sprockets 2.12.3 → 3.0.0

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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +2 -2
  3. data/README.md +226 -119
  4. data/lib/rake/sprocketstask.rb +5 -4
  5. data/lib/sprockets/asset.rb +143 -211
  6. data/lib/sprockets/autoload/closure.rb +7 -0
  7. data/lib/sprockets/autoload/coffee_script.rb +7 -0
  8. data/lib/sprockets/autoload/eco.rb +7 -0
  9. data/lib/sprockets/autoload/ejs.rb +7 -0
  10. data/lib/sprockets/autoload/sass.rb +7 -0
  11. data/lib/sprockets/autoload/uglifier.rb +7 -0
  12. data/lib/sprockets/autoload/yui.rb +7 -0
  13. data/lib/sprockets/autoload.rb +11 -0
  14. data/lib/sprockets/base.rb +47 -395
  15. data/lib/sprockets/bower.rb +58 -0
  16. data/lib/sprockets/bundle.rb +65 -0
  17. data/lib/sprockets/cache/file_store.rb +165 -14
  18. data/lib/sprockets/cache/memory_store.rb +66 -0
  19. data/lib/sprockets/cache/null_store.rb +46 -0
  20. data/lib/sprockets/cache.rb +234 -0
  21. data/lib/sprockets/cached_environment.rb +69 -0
  22. data/lib/sprockets/closure_compressor.rb +41 -10
  23. data/lib/sprockets/coffee_script_processor.rb +25 -0
  24. data/lib/sprockets/coffee_script_template.rb +6 -0
  25. data/lib/sprockets/compressing.rb +24 -23
  26. data/lib/sprockets/configuration.rb +83 -0
  27. data/lib/sprockets/context.rb +84 -144
  28. data/lib/sprockets/dependencies.rb +73 -0
  29. data/lib/sprockets/digest_utils.rb +156 -0
  30. data/lib/sprockets/directive_processor.rb +207 -211
  31. data/lib/sprockets/eco_processor.rb +32 -0
  32. data/lib/sprockets/eco_template.rb +3 -35
  33. data/lib/sprockets/ejs_processor.rb +31 -0
  34. data/lib/sprockets/ejs_template.rb +3 -34
  35. data/lib/sprockets/encoding_utils.rb +258 -0
  36. data/lib/sprockets/engines.rb +42 -35
  37. data/lib/sprockets/environment.rb +17 -64
  38. data/lib/sprockets/erb_processor.rb +30 -0
  39. data/lib/sprockets/erb_template.rb +6 -0
  40. data/lib/sprockets/errors.rb +5 -13
  41. data/lib/sprockets/file_reader.rb +15 -0
  42. data/lib/sprockets/http_utils.rb +115 -0
  43. data/lib/sprockets/jst_processor.rb +35 -15
  44. data/lib/sprockets/legacy.rb +314 -0
  45. data/lib/sprockets/legacy_proc_processor.rb +35 -0
  46. data/lib/sprockets/legacy_tilt_processor.rb +29 -0
  47. data/lib/sprockets/loader.rb +176 -0
  48. data/lib/sprockets/manifest.rb +153 -129
  49. data/lib/sprockets/manifest_utils.rb +45 -0
  50. data/lib/sprockets/mime.rb +112 -31
  51. data/lib/sprockets/path_dependency_utils.rb +85 -0
  52. data/lib/sprockets/path_digest_utils.rb +47 -0
  53. data/lib/sprockets/path_utils.rb +282 -0
  54. data/lib/sprockets/paths.rb +42 -19
  55. data/lib/sprockets/processing.rb +168 -126
  56. data/lib/sprockets/processor_utils.rb +103 -0
  57. data/lib/sprockets/resolve.rb +208 -0
  58. data/lib/sprockets/sass_cache_store.rb +18 -18
  59. data/lib/sprockets/sass_compressor.rb +47 -15
  60. data/lib/sprockets/sass_functions.rb +2 -70
  61. data/lib/sprockets/sass_importer.rb +2 -30
  62. data/lib/sprockets/sass_processor.rb +285 -0
  63. data/lib/sprockets/sass_template.rb +4 -63
  64. data/lib/sprockets/server.rb +104 -79
  65. data/lib/sprockets/transformers.rb +145 -0
  66. data/lib/sprockets/uglifier_compressor.rb +46 -12
  67. data/lib/sprockets/uri_utils.rb +190 -0
  68. data/lib/sprockets/utils.rb +193 -44
  69. data/lib/sprockets/version.rb +1 -1
  70. data/lib/sprockets/yui_compressor.rb +49 -11
  71. data/lib/sprockets.rb +132 -81
  72. metadata +84 -89
  73. data/lib/sprockets/asset_attributes.rb +0 -137
  74. data/lib/sprockets/bundled_asset.rb +0 -78
  75. data/lib/sprockets/caching.rb +0 -96
  76. data/lib/sprockets/charset_normalizer.rb +0 -41
  77. data/lib/sprockets/index.rb +0 -100
  78. data/lib/sprockets/processed_asset.rb +0 -152
  79. data/lib/sprockets/processor.rb +0 -32
  80. data/lib/sprockets/safety_colons.rb +0 -28
  81. data/lib/sprockets/scss_template.rb +0 -13
  82. data/lib/sprockets/static_asset.rb +0 -58
@@ -0,0 +1,176 @@
1
+ require 'sprockets/asset'
2
+ require 'sprockets/digest_utils'
3
+ require 'sprockets/engines'
4
+ require 'sprockets/errors'
5
+ require 'sprockets/file_reader'
6
+ require 'sprockets/mime'
7
+ require 'sprockets/path_utils'
8
+ require 'sprockets/processing'
9
+ require 'sprockets/processor_utils'
10
+ require 'sprockets/resolve'
11
+ require 'sprockets/transformers'
12
+ require 'sprockets/uri_utils'
13
+
14
+ module Sprockets
15
+ # The loader phase takes a asset URI location and returns a constructed Asset
16
+ # object.
17
+ module Loader
18
+ include DigestUtils, PathUtils, ProcessorUtils, URIUtils
19
+ include Engines, Mime, Processing, Resolve, Transformers
20
+
21
+ # Public: Load Asset by AssetURI.
22
+ #
23
+ # uri - AssetURI
24
+ #
25
+ # Returns Asset.
26
+ def load(uri)
27
+ filename, params = parse_asset_uri(uri)
28
+ if params.key?(:id)
29
+ asset = cache.fetch("asset-uri:#{VERSION}#{uri}") do
30
+ load_asset_by_id_uri(uri, filename, params)
31
+ end
32
+ else
33
+ asset = fetch_asset_from_dependency_cache(uri, filename) do |paths|
34
+ if paths
35
+ digest = digest(resolve_dependencies(paths))
36
+ if id_uri = cache.get("asset-uri-digest:#{VERSION}:#{uri}:#{digest}", true)
37
+ cache.get("asset-uri:#{VERSION}:#{id_uri}", true)
38
+ end
39
+ else
40
+ load_asset_by_uri(uri, filename, params)
41
+ end
42
+ end
43
+ end
44
+ Asset.new(self, asset)
45
+ end
46
+
47
+ private
48
+ def load_asset_by_id_uri(uri, filename, params)
49
+ # Internal assertion, should be routed through load_asset_by_uri
50
+ unless id = params.delete(:id)
51
+ raise ArgumentError, "expected uri to have an id: #{uri}"
52
+ end
53
+
54
+ uri = build_asset_uri(filename, params)
55
+ asset = load_asset_by_uri(uri, filename, params)
56
+
57
+ if id && asset[:id] != id
58
+ raise VersionNotFound, "could not find specified id: #{uri}##{id}"
59
+ end
60
+
61
+ asset
62
+ end
63
+
64
+ def load_asset_by_uri(uri, filename, params)
65
+ # Internal assertion, should be routed through load_asset_by_id_uri
66
+ if params.key?(:id)
67
+ raise ArgumentError, "expected uri to have no id: #{uri}"
68
+ end
69
+
70
+ unless file?(filename)
71
+ raise FileNotFound, "could not find file: #{filename}"
72
+ end
73
+
74
+ load_path, logical_path = paths_split(config[:paths], filename)
75
+
76
+ unless load_path
77
+ raise FileOutsidePaths, "#{filename} is no longer under a load path: #{self.paths.join(', ')}"
78
+ end
79
+
80
+ logical_path, file_type, engine_extnames, _ = parse_path_extnames(logical_path)
81
+ logical_path = normalize_logical_path(logical_path)
82
+ name = logical_path
83
+
84
+ if pipeline = params[:pipeline]
85
+ logical_path += ".#{pipeline}"
86
+ end
87
+
88
+ if type = params[:type]
89
+ logical_path += config[:mime_types][type][:extensions].first
90
+ end
91
+
92
+ if type != file_type && !config[:transformers][file_type][type]
93
+ raise ConversionError, "could not convert #{file_type.inspect} to #{type.inspect}"
94
+ end
95
+
96
+ processors = processors_for(type, file_type, engine_extnames, pipeline)
97
+
98
+ processors_dep_uri = build_processors_uri(type, file_type, engine_extnames, pipeline)
99
+ dependencies = config[:dependencies] + [processors_dep_uri]
100
+
101
+ # Read into memory and process if theres a processor pipeline
102
+ if processors.any?
103
+ result = call_processors(processors, {
104
+ environment: self,
105
+ cache: self.cache,
106
+ uri: uri,
107
+ filename: filename,
108
+ load_path: load_path,
109
+ name: name,
110
+ content_type: type,
111
+ metadata: { dependencies: dependencies }
112
+ })
113
+ source = result.delete(:data)
114
+ metadata = result.merge!(
115
+ charset: source.encoding.name.downcase,
116
+ digest: digest(source),
117
+ length: source.bytesize
118
+ )
119
+ else
120
+ metadata = {
121
+ digest: file_digest(filename),
122
+ length: self.stat(filename).size,
123
+ dependencies: dependencies
124
+ }
125
+ end
126
+
127
+ asset = {
128
+ uri: uri,
129
+ load_path: load_path,
130
+ filename: filename,
131
+ name: name,
132
+ logical_path: logical_path,
133
+ content_type: type,
134
+ source: source,
135
+ metadata: metadata,
136
+ integrity: integrity_uri(metadata[:digest], type),
137
+ dependencies_digest: digest(resolve_dependencies(metadata[:dependencies]))
138
+ }
139
+
140
+ asset[:id] = pack_hexdigest(digest(asset))
141
+ asset[:uri] = build_asset_uri(filename, params.merge(id: asset[:id]))
142
+
143
+ # Deprecated: Avoid tracking Asset mtime
144
+ asset[:mtime] = metadata[:dependencies].map { |u|
145
+ if u.start_with?("file-digest:")
146
+ s = self.stat(parse_file_digest_uri(u))
147
+ s ? s.mtime.to_i : 0
148
+ else
149
+ 0
150
+ end
151
+ }.max
152
+
153
+ cache.set("asset-uri:#{VERSION}:#{asset[:uri]}", asset, true)
154
+ cache.set("asset-uri-digest:#{VERSION}:#{uri}:#{asset[:dependencies_digest]}", asset[:uri], true)
155
+
156
+ asset
157
+ end
158
+
159
+ def fetch_asset_from_dependency_cache(uri, filename, limit = 3)
160
+ key = "asset-uri-cache-dependencies:#{VERSION}:#{uri}:#{file_digest(filename)}"
161
+ history = cache.get(key) || []
162
+
163
+ history.each_with_index do |deps, index|
164
+ if asset = yield(deps)
165
+ cache.set(key, history.rotate!(index)) if index > 0
166
+ return asset
167
+ end
168
+ end
169
+
170
+ asset = yield
171
+ deps = asset[:metadata][:dependencies]
172
+ cache.set(key, history.unshift(deps).take(limit))
173
+ asset
174
+ end
175
+ end
176
+ end
@@ -1,26 +1,27 @@
1
- require 'multi_json'
2
- require 'securerandom'
1
+ require 'json'
3
2
  require 'time'
3
+ require 'sprockets/manifest_utils'
4
4
 
5
5
  module Sprockets
6
- # The Manifest logs the contents of assets compiled to a single
7
- # directory. It records basic attributes about the asset for fast
8
- # lookup without having to compile. A pointer from each logical path
9
- # indicates with fingerprinted asset is the current one.
6
+ # The Manifest logs the contents of assets compiled to a single directory. It
7
+ # records basic attributes about the asset for fast lookup without having to
8
+ # compile. A pointer from each logical path indicates which fingerprinted
9
+ # asset is the current one.
10
10
  #
11
- # The JSON is part of the public API and should be considered
12
- # stable. This should make it easy to read from other programming
13
- # languages and processes that don't have sprockets loaded. See
14
- # `#assets` and `#files` for more infomation about the structure.
11
+ # The JSON is part of the public API and should be considered stable. This
12
+ # should make it easy to read from other programming languages and processes
13
+ # that don't have sprockets loaded. See `#assets` and `#files` for more
14
+ # infomation about the structure.
15
15
  class Manifest
16
- attr_reader :environment, :path, :dir
17
-
18
- # Create new Manifest associated with an `environment`. `path` is
19
- # a full path to the manifest json file. The file may or may not
20
- # already exist. The dirname of the `path` will be used to write
21
- # compiled assets to. Otherwise, if the path is a directory, the
22
- # filename will default a random "manifest-123.json" file in that
23
- # directory.
16
+ include ManifestUtils
17
+
18
+ attr_reader :environment
19
+
20
+ # Create new Manifest associated with an `environment`. `filename` is a full
21
+ # path to the manifest json file. The file may or may not already exist. The
22
+ # dirname of the `filename` will be used to write compiled assets to.
23
+ # Otherwise, if the path is a directory, the filename will default a random
24
+ # ".sprockets-manifest-*.json" file in that directory.
24
25
  #
25
26
  # Manifest.new(environment, "./public/assets/manifest.json")
26
27
  #
@@ -29,48 +30,58 @@ module Sprockets
29
30
  @environment = args.shift
30
31
  end
31
32
 
32
- @dir, @path = args[0], args[1]
33
+ @directory, @filename = args[0], args[1]
34
+
35
+ # Whether the manifest file is using the old manifest-*.json naming convention
36
+ @legacy_manifest = false
33
37
 
34
38
  # Expand paths
35
- @dir = File.expand_path(@dir) if @dir
36
- @path = File.expand_path(@path) if @path
39
+ @directory = File.expand_path(@directory) if @directory
40
+ @filename = File.expand_path(@filename) if @filename
37
41
 
38
- # If path is given as the second arg
39
- if @dir && File.extname(@dir) != ""
40
- @dir, @path = nil, @dir
42
+ # If filename is given as the second arg
43
+ if @directory && File.extname(@directory) != ""
44
+ @directory, @filename = nil, @directory
41
45
  end
42
46
 
43
- # Default dir to the directory of the path
44
- @dir ||= File.dirname(@path) if @path
47
+ # Default dir to the directory of the filename
48
+ @directory ||= File.dirname(@filename) if @filename
45
49
 
46
- # If directory is given w/o path, pick a random manifest.json location
47
- if @dir && @path.nil?
48
- # Find the first manifest.json in the directory
49
- paths = Dir[File.join(@dir, "manifest*.json")]
50
- if paths.any?
51
- @path = paths.first
52
- else
53
- @path = File.join(@dir, "manifest-#{SecureRandom.hex(16)}.json")
50
+ # If directory is given w/o filename, pick a random manifest location
51
+ @rename_filename = nil
52
+ if @directory && @filename.nil?
53
+ @filename = find_directory_manifest(@directory)
54
+
55
+ # If legacy manifest name autodetected, mark to rename on save
56
+ if File.basename(@filename).start_with?("manifest")
57
+ @rename_filename = File.join(@directory, generate_manifest_path)
54
58
  end
55
59
  end
56
60
 
57
- unless @dir && @path
58
- raise ArgumentError, "manifest requires output path"
61
+ unless @directory && @filename
62
+ raise ArgumentError, "manifest requires output filename"
59
63
  end
60
64
 
61
- data = nil
65
+ data = {}
62
66
 
63
67
  begin
64
- if File.exist?(@path)
65
- data = json_decode(File.read(@path))
68
+ if File.exist?(@filename)
69
+ data = json_decode(File.read(@filename))
66
70
  end
67
- rescue MultiJson::DecodeError => e
68
- logger.error "#{@path} is invalid: #{e.class} #{e.message}"
71
+ rescue JSON::ParserError => e
72
+ logger.error "#{@filename} is invalid: #{e.class} #{e.message}"
69
73
  end
70
74
 
71
- @data = data.is_a?(Hash) ? data : {}
75
+ @data = data
72
76
  end
73
77
 
78
+ # Returns String path to manifest.json file.
79
+ attr_reader :filename
80
+ alias_method :path, :filename
81
+
82
+ attr_reader :directory
83
+ alias_method :dir, :directory
84
+
74
85
  # Returns internal assets mapping. Keys are logical paths which
75
86
  # map to the latest fingerprinted filename.
76
87
  #
@@ -100,6 +111,40 @@ module Sprockets
100
111
  @data['files'] ||= {}
101
112
  end
102
113
 
114
+ # Public: Find all assets matching pattern set in environment.
115
+ #
116
+ # Returns Enumerator of Assets.
117
+ def find(*args)
118
+ unless environment
119
+ raise Error, "manifest requires environment for compilation"
120
+ end
121
+
122
+ return to_enum(__method__, *args) unless block_given?
123
+
124
+ paths, filters = args.flatten.partition { |arg| self.class.simple_logical_path?(arg) }
125
+ filters = filters.map { |arg| self.class.compile_match_filter(arg) }
126
+
127
+ environment = self.environment.cached
128
+
129
+ paths.each do |path|
130
+ environment.find_all_linked_assets(path) do |asset|
131
+ yield asset
132
+ end
133
+ end
134
+
135
+ if filters.any?
136
+ environment.logical_paths do |logical_path, filename|
137
+ if filters.any? { |f| f.call(logical_path, filename) }
138
+ environment.find_all_linked_assets(filename) do |asset|
139
+ yield asset
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ nil
146
+ end
147
+
103
148
  # Compile and write asset to directory. The asset is written to a
104
149
  # fingerprinted filename like
105
150
  # `application-2e8e9a7c6b0aafa0c9bdeec90ea30213.js`. An entry is
@@ -112,33 +157,32 @@ module Sprockets
112
157
  raise Error, "manifest requires environment for compilation"
113
158
  end
114
159
 
115
- paths = environment.each_logical_path(*args).to_a +
116
- args.flatten.select { |fn| Pathname.new(fn).absolute? if fn.is_a?(String)}
160
+ filenames = []
117
161
 
118
- paths.each do |path|
119
- if asset = find_asset(path)
120
- files[asset.digest_path] = {
121
- 'logical_path' => asset.logical_path,
122
- 'mtime' => asset.mtime.iso8601,
123
- 'size' => asset.bytesize,
124
- 'digest' => asset.digest
125
- }
126
- assets[asset.logical_path] = asset.digest_path
127
-
128
- target = File.join(dir, asset.digest_path)
129
-
130
- if File.exist?(target)
131
- logger.debug "Skipping #{target}, already exists"
132
- else
133
- logger.info "Writing #{target}"
134
- asset.write_to target
135
- asset.write_to "#{target}.gz" if asset.is_a?(BundledAsset)
136
- end
162
+ find(*args) do |asset|
163
+ files[asset.digest_path] = {
164
+ 'logical_path' => asset.logical_path,
165
+ 'mtime' => asset.mtime.iso8601,
166
+ 'size' => asset.bytesize,
167
+ 'digest' => asset.hexdigest,
168
+ 'integrity' => asset.integrity
169
+ }
170
+ assets[asset.logical_path] = asset.digest_path
171
+
172
+ target = File.join(dir, asset.digest_path)
137
173
 
174
+ if File.exist?(target)
175
+ logger.debug "Skipping #{target}, already exists"
176
+ else
177
+ logger.info "Writing #{target}"
178
+ asset.write_to target
138
179
  end
180
+
181
+ filenames << asset.filename
139
182
  end
140
183
  save
141
- paths
184
+
185
+ filenames
142
186
  end
143
187
 
144
188
  # Removes file from directory and from manifest. `filename` must
@@ -148,7 +192,6 @@ module Sprockets
148
192
  #
149
193
  def remove(filename)
150
194
  path = File.join(dir, filename)
151
- gzip = "#{path}.gz"
152
195
  logical_path = files[filename]['logical_path']
153
196
 
154
197
  if assets[logical_path] == filename
@@ -157,7 +200,6 @@ module Sprockets
157
200
 
158
201
  files.delete(filename)
159
202
  FileUtils.rm(path) if File.exist?(path)
160
- FileUtils.rm(gzip) if File.exist?(gzip)
161
203
 
162
204
  save
163
205
 
@@ -167,79 +209,67 @@ module Sprockets
167
209
  end
168
210
 
169
211
  # Cleanup old assets in the compile directory. By default it will
170
- # keep the latest version plus 2 backups.
171
- def clean(keep = 2)
172
- self.assets.keys.each do |logical_path|
173
- # Get assets sorted by ctime, newest first
174
- assets = backups_for(logical_path)
212
+ # keep the latest version, 2 backups and any created within the past hour.
213
+ #
214
+ # Examples
215
+ #
216
+ # To force only 1 backup to be kept, set count=1 and age=0.
217
+ #
218
+ # To only keep files created within the last 10 minutes, set count=0 and
219
+ # age=600.
220
+ #
221
+ def clean(count = 2, age = 3600)
222
+ asset_versions = files.group_by { |_, attrs| attrs['logical_path'] }
175
223
 
176
- # Keep the last N backups
177
- assets = assets[keep..-1] || []
224
+ asset_versions.each do |logical_path, versions|
225
+ current = assets[logical_path]
178
226
 
179
- # Remove old assets
180
- assets.each { |path, _| remove(path) }
227
+ versions.reject { |path, _|
228
+ path == current
229
+ }.sort_by { |_, attrs|
230
+ # Sort by timestamp
231
+ Time.parse(attrs['mtime'])
232
+ }.reverse.each_with_index.drop_while { |(_, attrs), index|
233
+ age = [0, Time.now - Time.parse(attrs['mtime'])].max
234
+ # Keep if under age or within the count limit
235
+ age < age || index < count
236
+ }.each { |(path, _), _|
237
+ # Remove old assets
238
+ remove(path)
239
+ }
181
240
  end
182
241
  end
183
242
 
184
243
  # Wipe directive
185
244
  def clobber
186
- FileUtils.rm_r(@dir) if File.exist?(@dir)
187
- logger.info "Removed #{@dir}"
245
+ FileUtils.rm_r(directory) if File.exist?(directory)
246
+ logger.info "Removed #{directory}"
188
247
  nil
189
248
  end
190
249
 
191
- protected
192
- # Finds all the backup assets for a logical path. The latest
193
- # version is always excluded. The return array is sorted by the
194
- # assets mtime in descending order (Newest to oldest).
195
- def backups_for(logical_path)
196
- files.select { |filename, attrs|
197
- # Matching logical paths
198
- attrs['logical_path'] == logical_path &&
199
- # Excluding whatever asset is the current
200
- assets[logical_path] != filename
201
- }.sort_by { |filename, attrs|
202
- # Sort by timestamp
203
- Time.parse(attrs['mtime'])
204
- }.reverse
205
- end
206
-
207
- # Basic wrapper around Environment#find_asset. Logs compile time.
208
- def find_asset(logical_path)
209
- asset = nil
210
- ms = benchmark do
211
- asset = environment.find_asset(logical_path)
212
- end
213
- logger.debug "Compiled #{logical_path} (#{ms}ms)"
214
- asset
250
+ # Persist manfiest back to FS
251
+ def save
252
+ if @rename_filename
253
+ logger.info "Renaming #{@filename} to #{@rename_filename}"
254
+ FileUtils.mv(@filename, @rename_filename)
255
+ @filename = @rename_filename
256
+ @rename_filename = nil
215
257
  end
216
258
 
217
- # Persist manfiest back to FS
218
- def save
219
- FileUtils.mkdir_p File.dirname(path)
220
- File.open(path, 'w') do |f|
221
- f.write json_encode(@data)
222
- end
259
+ data = json_encode(@data)
260
+ FileUtils.mkdir_p File.dirname(@filename)
261
+ PathUtils.atomic_write(@filename) do |f|
262
+ f.write(data)
223
263
  end
264
+ end
224
265
 
225
266
  private
226
- # Feature detect newer MultiJson API
227
- if MultiJson.respond_to?(:dump)
228
- def json_decode(obj)
229
- MultiJson.load(obj)
230
- end
231
-
232
- def json_encode(obj)
233
- MultiJson.dump(obj)
234
- end
235
- else
236
- def json_decode(obj)
237
- MultiJson.decode(obj)
238
- end
267
+ def json_decode(obj)
268
+ JSON.parse(obj, create_additions: false)
269
+ end
239
270
 
240
- def json_encode(obj)
241
- MultiJson.encode(obj)
242
- end
271
+ def json_encode(obj)
272
+ JSON.generate(obj)
243
273
  end
244
274
 
245
275
  def logger
@@ -251,11 +281,5 @@ module Sprockets
251
281
  logger
252
282
  end
253
283
  end
254
-
255
- def benchmark
256
- start_time = Time.now.to_f
257
- yield
258
- ((Time.now.to_f - start_time) * 1000).to_i
259
- end
260
284
  end
261
285
  end
@@ -0,0 +1,45 @@
1
+ require 'securerandom'
2
+
3
+ module Sprockets
4
+ # Public: Manifest utilities.
5
+ module ManifestUtils
6
+ extend self
7
+
8
+ MANIFEST_RE = /^\.sprockets-manifest-[0-9a-f]{32}.json$/
9
+ LEGACY_MANIFEST_RE = /^manifest(-[0-9a-f]{32})?.json$/
10
+
11
+ # Public: Generate a new random manifest path.
12
+ #
13
+ # Manifests are not intended to be accessed publicly, but typically live
14
+ # alongside public assets for convenience. To avoid being served, the
15
+ # filename is prefixed with a "." which is usually hidden by web servers
16
+ # like Apache. To help in other environments that may not control this,
17
+ # a random hex string is appended to the filename to prevent people from
18
+ # guessing the location. If directory indexes are enabled on the server,
19
+ # all bets are off.
20
+ #
21
+ # Return String path.
22
+ def generate_manifest_path
23
+ ".sprockets-manifest-#{SecureRandom.hex(16)}.json"
24
+ end
25
+
26
+ # Public: Find or pick a new manifest filename for target build directory.
27
+ #
28
+ # dirname - String dirname
29
+ #
30
+ # Examples
31
+ #
32
+ # find_directory_manifest("/app/public/assets")
33
+ # # => "/app/public/assets/.sprockets-manifest-abc123.json"
34
+ #
35
+ # Returns String filename.
36
+ def find_directory_manifest(dirname)
37
+ entries = File.directory?(dirname) ? Dir.entries(dirname) : []
38
+ entry = entries.find { |e| e =~ MANIFEST_RE } ||
39
+ # Deprecated: Will be removed in 4.x
40
+ entries.find { |e| e =~ LEGACY_MANIFEST_RE } ||
41
+ generate_manifest_path
42
+ File.join(dirname, entry)
43
+ end
44
+ end
45
+ end