sprockets 4.0.0.beta2 → 4.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +25 -13
  4. data/lib/rake/sprocketstask.rb +1 -0
  5. data/lib/sprockets.rb +4 -2
  6. data/lib/sprockets/asset.rb +1 -0
  7. data/lib/sprockets/autoload.rb +1 -0
  8. data/lib/sprockets/autoload/babel.rb +1 -0
  9. data/lib/sprockets/autoload/closure.rb +1 -0
  10. data/lib/sprockets/autoload/coffee_script.rb +1 -0
  11. data/lib/sprockets/autoload/eco.rb +1 -0
  12. data/lib/sprockets/autoload/ejs.rb +1 -0
  13. data/lib/sprockets/autoload/jsminc.rb +1 -0
  14. data/lib/sprockets/autoload/sass.rb +1 -0
  15. data/lib/sprockets/autoload/sassc.rb +1 -0
  16. data/lib/sprockets/autoload/uglifier.rb +1 -0
  17. data/lib/sprockets/autoload/yui.rb +1 -0
  18. data/lib/sprockets/babel_processor.rb +3 -1
  19. data/lib/sprockets/base.rb +3 -2
  20. data/lib/sprockets/bower.rb +1 -0
  21. data/lib/sprockets/bundle.rb +1 -0
  22. data/lib/sprockets/cache.rb +4 -1
  23. data/lib/sprockets/cache/file_store.rb +1 -0
  24. data/lib/sprockets/cache/memory_store.rb +1 -0
  25. data/lib/sprockets/cache/null_store.rb +1 -0
  26. data/lib/sprockets/cached_environment.rb +4 -3
  27. data/lib/sprockets/closure_compressor.rb +3 -1
  28. data/lib/sprockets/coffee_script_processor.rb +2 -1
  29. data/lib/sprockets/compressing.rb +19 -0
  30. data/lib/sprockets/configuration.rb +1 -0
  31. data/lib/sprockets/context.rb +3 -2
  32. data/lib/sprockets/dependencies.rb +1 -0
  33. data/lib/sprockets/digest_utils.rb +64 -41
  34. data/lib/sprockets/directive_processor.rb +15 -10
  35. data/lib/sprockets/eco_processor.rb +1 -0
  36. data/lib/sprockets/ejs_processor.rb +1 -0
  37. data/lib/sprockets/encoding_utils.rb +1 -0
  38. data/lib/sprockets/environment.rb +3 -2
  39. data/lib/sprockets/erb_processor.rb +1 -0
  40. data/lib/sprockets/errors.rb +1 -0
  41. data/lib/sprockets/file_reader.rb +1 -0
  42. data/lib/sprockets/http_utils.rb +1 -0
  43. data/lib/sprockets/jsminc_compressor.rb +1 -0
  44. data/lib/sprockets/jst_processor.rb +1 -0
  45. data/lib/sprockets/loader.rb +2 -1
  46. data/lib/sprockets/manifest.rb +1 -0
  47. data/lib/sprockets/manifest_utils.rb +1 -0
  48. data/lib/sprockets/mime.rb +1 -0
  49. data/lib/sprockets/path_dependency_utils.rb +1 -0
  50. data/lib/sprockets/path_digest_utils.rb +1 -0
  51. data/lib/sprockets/path_utils.rb +18 -13
  52. data/lib/sprockets/paths.rb +1 -0
  53. data/lib/sprockets/preprocessors/default_source_map.rb +1 -0
  54. data/lib/sprockets/processing.rb +1 -0
  55. data/lib/sprockets/processor_utils.rb +15 -3
  56. data/lib/sprockets/resolve.rb +2 -1
  57. data/lib/sprockets/sass_cache_store.rb +1 -0
  58. data/lib/sprockets/sass_compressor.rb +1 -0
  59. data/lib/sprockets/sass_functions.rb +1 -0
  60. data/lib/sprockets/sass_importer.rb +1 -0
  61. data/lib/sprockets/sass_processor.rb +15 -3
  62. data/lib/sprockets/sassc_compressor.rb +4 -1
  63. data/lib/sprockets/sassc_processor.rb +5 -0
  64. data/lib/sprockets/server.rb +2 -1
  65. data/lib/sprockets/source_map_comment_processor.rb +1 -0
  66. data/lib/sprockets/source_map_processor.rb +9 -2
  67. data/lib/sprockets/source_map_utils.rb +1 -0
  68. data/lib/sprockets/transformers.rb +1 -0
  69. data/lib/sprockets/uglifier_compressor.rb +9 -12
  70. data/lib/sprockets/unloaded_asset.rb +1 -0
  71. data/lib/sprockets/uri_tar.rb +3 -4
  72. data/lib/sprockets/uri_utils.rb +2 -1
  73. data/lib/sprockets/utils.rb +25 -11
  74. data/lib/sprockets/utils/gzip.rb +1 -0
  75. data/lib/sprockets/version.rb +2 -1
  76. data/lib/sprockets/yui_compressor.rb +1 -0
  77. metadata +2 -2
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'digest/md5'
2
3
  require 'digest/sha1'
3
4
  require 'digest/sha2'
@@ -34,6 +35,50 @@ module Sprockets
34
35
  DIGEST_SIZES[bytes.bytesize]
35
36
  end
36
37
 
38
+ ADD_VALUE_TO_DIGEST = {
39
+ String => ->(val, digest) { digest << val },
40
+ FalseClass => ->(val, digest) { digest << 'FalseClass'.freeze },
41
+ TrueClass => ->(val, digest) { digest << 'TrueClass'.freeze },
42
+ NilClass => ->(val, digest) { digest << 'NilClass'.freeze },
43
+
44
+ Symbol => ->(val, digest) {
45
+ digest << 'Symbol'.freeze
46
+ digest << val.to_s
47
+ },
48
+ Fixnum => ->(val, digest) {
49
+ digest << 'Fixnum'.freeze
50
+ digest << val.to_s
51
+ },
52
+ Bignum => ->(val, digest) {
53
+ digest << 'Bignum'.freeze
54
+ digest << val.to_s
55
+ },
56
+ Array => ->(val, digest) {
57
+ digest << 'Array'.freeze
58
+ val.each do |element|
59
+ ADD_VALUE_TO_DIGEST[element.class].call(element, digest)
60
+ end
61
+ },
62
+ Hash => ->(val, digest) {
63
+ digest << 'Hash'.freeze
64
+ val.sort.each do |array|
65
+ ADD_VALUE_TO_DIGEST[Array].call(array, digest)
66
+ end
67
+ },
68
+ Set => ->(val, digest) {
69
+ digest << 'Set'.freeze
70
+ ADD_VALUE_TO_DIGEST[Array].call(val, digest)
71
+ },
72
+ Encoding => ->(val, digest) {
73
+ digest << 'Encoding'.freeze
74
+ digest << val.name
75
+ },
76
+ }
77
+ ADD_VALUE_TO_DIGEST.default_proc = ->(_, val) {
78
+ raise TypeError, "couldn't digest #{ val }"
79
+ }
80
+ private_constant :ADD_VALUE_TO_DIGEST
81
+
37
82
  # Internal: Generate a hexdigest for a nested JSON serializable object.
38
83
  #
39
84
  # This is used for generating cache keys, so its pretty important its
@@ -43,48 +88,18 @@ module Sprockets
43
88
  #
44
89
  # Returns a String digest of the object.
45
90
  def digest(obj)
46
- digest = digest_class.new
47
- queue = [obj]
48
-
49
- while queue.length > 0
50
- obj = queue.shift
51
- klass = obj.class
52
-
53
- if klass == String
54
- digest << obj
55
- elsif klass == Symbol
56
- digest << 'Symbol'
57
- digest << obj.to_s
58
- elsif klass == Fixnum
59
- digest << 'Fixnum'
60
- digest << obj.to_s
61
- elsif klass == Bignum
62
- digest << 'Bignum'
63
- digest << obj.to_s
64
- elsif klass == TrueClass
65
- digest << 'TrueClass'
66
- elsif klass == FalseClass
67
- digest << 'FalseClass'
68
- elsif klass == NilClass
69
- digest << 'NilClass'.freeze
70
- elsif klass == Array
71
- digest << 'Array'
72
- queue.concat(obj)
73
- elsif klass == Hash
74
- digest << 'Hash'
75
- queue.concat(obj.sort)
76
- elsif klass == Set
77
- digest << 'Set'
78
- queue.concat(obj.to_a)
79
- elsif klass == Encoding
80
- digest << 'Encoding'
81
- digest << obj.name
82
- else
83
- raise TypeError, "couldn't digest #{klass}"
84
- end
85
- end
91
+ build_digest(obj).digest
92
+ end
86
93
 
87
- digest.digest
94
+ # Internal: Generate a hexdigest for a nested JSON serializable object.
95
+ #
96
+ # The same as `pack_hexdigest(digest(obj))`.
97
+ #
98
+ # obj - A JSON serializable object.
99
+ #
100
+ # Returns a String digest of the object.
101
+ def hexdigest(obj)
102
+ build_digest(obj).hexdigest!
88
103
  end
89
104
 
90
105
  # Internal: Pack a binary digest to a hex encoded string.
@@ -164,5 +179,13 @@ module Sprockets
164
179
  def hexdigest_integrity_uri(hexdigest)
165
180
  integrity_uri(unpack_hexdigest(hexdigest))
166
181
  end
182
+
183
+ private
184
+ def build_digest(obj)
185
+ digest = digest_class.new
186
+
187
+ ADD_VALUE_TO_DIGEST[obj.class].call(obj, digest)
188
+ digest
189
+ end
167
190
  end
168
191
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'set'
2
3
  require 'shellwords'
3
4
 
@@ -48,7 +49,7 @@ module Sprockets
48
49
  /x
49
50
 
50
51
  def self.instance
51
- # Default to C omment styles
52
+ # Default to C comment styles
52
53
  @instance ||= new(comments: ["//", ["/*", "*/"]])
53
54
  end
54
55
 
@@ -112,9 +113,9 @@ module Sprockets
112
113
 
113
114
  header, directives = extract_directives(header)
114
115
 
115
- data = ""
116
+ data = String.new("")
116
117
  data.force_encoding(body.encoding)
117
- data << header << "\n" unless header.empty?
118
+ data << header unless header.empty?
118
119
  data << body
119
120
  # Ensure body ends in a new line
120
121
  data << "\n" if data.length > 0 && data[-1] != "\n"
@@ -130,7 +131,7 @@ module Sprockets
130
131
  # [[1, "require", "foo"], [2, "require", "bar"]]
131
132
  #
132
133
  def extract_directives(header)
133
- processed_header = ""
134
+ processed_header = String.new("")
134
135
  directives = []
135
136
 
136
137
  header.lines.each_with_index do |line, index|
@@ -145,7 +146,11 @@ module Sprockets
145
146
  processed_header << line
146
147
  end
147
148
 
148
- return processed_header.chomp, directives
149
+ processed_header.chomp!
150
+ # Ensure header ends in a new line like before it was processed
151
+ processed_header << "\n" if processed_header.length > 0 && header[-1] == "\n"
152
+
153
+ return processed_header, directives
149
154
  end
150
155
 
151
156
  # Gathers comment directives in the source and processes them.
@@ -183,7 +188,7 @@ module Sprockets
183
188
 
184
189
  # The `require` directive functions similar to Ruby's own `require`.
185
190
  # It provides a way to declare a dependency on a file in your path
186
- # and ensures its only loaded once before the source file.
191
+ # and ensures it's only loaded once before the source file.
187
192
  #
188
193
  # `require` works with files in the environment path:
189
194
  #
@@ -261,8 +266,8 @@ module Sprockets
261
266
  # it.
262
267
  #
263
268
  # This is used for caching purposes. Any changes that would
264
- # invalid the asset dependency will invalidate the cache our the
265
- # source file.
269
+ # invalidate the asset dependency will invalidate the cache of
270
+ # the source file.
266
271
  #
267
272
  # Unlike `depend_on`, the path must be a requirable asset.
268
273
  #
@@ -303,7 +308,7 @@ module Sprockets
303
308
  # //= link_directory "./fonts"
304
309
  #
305
310
  # Use caution when linking against JS or CSS assets. Include an explicit
306
- # extension or content type in these cases
311
+ # extension or content type in these cases.
307
312
  #
308
313
  # //= link_directory "./scripts" .js
309
314
  #
@@ -319,7 +324,7 @@ module Sprockets
319
324
  # //= link_tree "./images"
320
325
  #
321
326
  # Use caution when linking against JS or CSS assets. Include an explicit
322
- # extension or content type in these cases
327
+ # extension or content type in these cases.
323
328
  #
324
329
  # //= link_tree "./styles" .css
325
330
  #
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/autoload'
2
3
 
3
4
  module Sprockets
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/autoload'
2
3
 
3
4
  module Sprockets
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'base64'
2
3
  require 'stringio'
3
4
  require 'zlib'
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/base'
2
3
  require 'sprockets/cache/memory_store'
3
4
  require 'sprockets/cached_environment'
4
5
 
5
6
  module Sprockets
6
7
  class Environment < Base
7
- # `Environment` should initialized with your application's root
8
+ # `Environment` should be initialized with your application's root
8
9
  # directory. This should be the same as your Rails or Rack root.
9
10
  #
10
11
  # env = Environment.new(Rails.root)
@@ -18,7 +19,7 @@ module Sprockets
18
19
 
19
20
  # Returns a cached version of the environment.
20
21
  #
21
- # All its file system calls are cached which makes `cached` much
22
+ # All of its file system calls are cached which makes `cached` much
22
23
  # faster. This behavior is ideal in production since the file
23
24
  # system only changes between deploys.
24
25
  def cached
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'erb'
2
3
 
3
4
  module Sprockets
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Define some basic Sprockets error classes
2
3
  module Sprockets
3
4
  class Error < StandardError; end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'set'
2
3
 
3
4
  module Sprockets
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Sprockets
2
3
  # Internal: HTTP URI utilities. Many adapted from Rack::Utils. Mixed into
3
4
  # Environment.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/autoload'
2
3
  require 'sprockets/digest_utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Sprockets
2
3
  # Public: JST transformer.
3
4
  #
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/asset'
2
3
  require 'sprockets/digest_utils'
3
4
  require 'sprockets/errors'
@@ -185,7 +186,7 @@ module Sprockets
185
186
  dependencies_digest: DigestUtils.digest(resolve_dependencies(metadata[:dependencies]))
186
187
  }
187
188
 
188
- asset[:id] = pack_hexdigest(digest(asset))
189
+ asset[:id] = hexdigest(asset)
189
190
  asset[:uri] = build_asset_uri(unloaded.filename, unloaded.params.merge(id: asset[:id]))
190
191
 
191
192
  store_asset(asset, unloaded)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'json'
2
3
  require 'time'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'securerandom'
2
3
 
3
4
  module Sprockets
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/encoding_utils'
2
3
  require 'sprockets/http_utils'
3
4
  require 'sprockets/utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'set'
2
3
  require 'sprockets/path_utils'
3
4
  require 'sprockets/uri_utils'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/digest_utils'
2
3
  require 'sprockets/path_utils'
3
4
 
@@ -1,5 +1,4 @@
1
- require 'fileutils'
2
-
1
+ # frozen_string_literal: true
3
2
  module Sprockets
4
3
  # Internal: File and path related utilities. Mixed into Environment.
5
4
  #
@@ -57,7 +56,9 @@ module Sprockets
57
56
  if File.directory?(path)
58
57
  entries = Dir.entries(path, encoding: Encoding.default_internal)
59
58
  entries.reject! { |entry|
60
- entry =~ /^\.|~$|^\#.*\#$/
59
+ entry.start_with?(".".freeze) ||
60
+ (entry.start_with?("#".freeze) && entry.end_with?("#".freeze)) ||
61
+ entry.end_with?("~".freeze)
61
62
  }
62
63
  entries.sort!
63
64
  entries
@@ -149,16 +150,19 @@ module Sprockets
149
150
  #
150
151
  # Returns [String extname, Object value] or nil nothing matched.
151
152
  def match_path_extname(path, extensions)
152
- match, key = nil, ""
153
- path_extnames(path).reverse_each do |extname|
154
- key.prepend(extname)
155
- if value = extensions[key]
156
- match = [key.dup, value]
157
- elsif match
158
- break
153
+ basename = File.basename(path)
154
+
155
+ i = basename.index('.'.freeze)
156
+ while i && i < basename.length - 1
157
+ extname = basename[i..-1]
158
+ if value = extensions[extname]
159
+ return extname, value
159
160
  end
161
+
162
+ i = basename.index('.'.freeze, i+1)
160
163
  end
161
- match
164
+
165
+ nil
162
166
  end
163
167
 
164
168
  # Internal: Match paths in a directory against available extensions.
@@ -177,6 +181,7 @@ module Sprockets
177
181
  def find_matching_path_for_extensions(path, basename, extensions)
178
182
  matches = []
179
183
  entries(path).each do |entry|
184
+ next unless File.basename(entry).start_with?(basename)
180
185
  extname, value = match_path_extname(entry, extensions)
181
186
  if basename == entry.chomp(extname)
182
187
  filename = File.join(path, entry)
@@ -304,9 +309,9 @@ module Sprockets
304
309
  yield f
305
310
  end
306
311
 
307
- FileUtils.mv(tmpname, filename)
312
+ File.rename(tmpname, filename)
308
313
  ensure
309
- FileUtils.rm(tmpname) if File.exist?(tmpname)
314
+ File.delete(tmpname) if File.exist?(tmpname)
310
315
  end
311
316
  end
312
317
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/path_utils'
2
3
  require 'sprockets/utils'
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Sprockets
2
3
  module Preprocessors
3
4
  # Private: Adds a default map to assets when one is not present
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'sprockets/file_reader'
2
3
  require 'sprockets/mime'
3
4
  require 'sprockets/processor_utils'
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  require 'set'
2
3
 
3
4
  module Sprockets
4
5
  # Functional utilities for dealing with Processor functions.
5
6
  #
6
- # A Processor is a general function that my modify or transform an asset as
7
+ # A Processor is a general function that may modify or transform an asset as
7
8
  # part of the pipeline. CoffeeScript to JavaScript conversion, Minification
8
9
  # or Concatenation are all implemented as seperate Processor steps.
9
10
  #
@@ -129,6 +130,17 @@ module Sprockets
129
130
  Set
130
131
  ]).freeze
131
132
 
133
+ # Internal: Hash of all "simple" value types allowed to be returned in
134
+ # processor metadata.
135
+ VALID_METADATA_VALUE_TYPES_HASH = VALID_METADATA_VALUE_TYPES.each_with_object({}) do |type, hash|
136
+ hash[type] = true
137
+ end.freeze
138
+
139
+ # Internal: Hash of all nested compound metadata types that can nest values.
140
+ VALID_METADATA_COMPOUND_TYPES_HASH = VALID_METADATA_COMPOUND_TYPES.each_with_object({}) do |type, hash|
141
+ hash[type] = true
142
+ end.freeze
143
+
132
144
  # Internal: Set of all allowed metadata types.
133
145
  VALID_METADATA_TYPES = (VALID_METADATA_VALUE_TYPES + VALID_METADATA_COMPOUND_TYPES).freeze
134
146
 
@@ -167,9 +179,9 @@ module Sprockets
167
179
  #
168
180
  # Returns true if class is in whitelist otherwise false.
169
181
  def valid_processor_metadata_value?(value)
170
- if VALID_METADATA_VALUE_TYPES.include?(value.class)
182
+ if VALID_METADATA_VALUE_TYPES_HASH[value.class]
171
183
  true
172
- elsif VALID_METADATA_COMPOUND_TYPES.include?(value.class)
184
+ elsif VALID_METADATA_COMPOUND_TYPES_HASH[value.class]
173
185
  value.all? { |v| valid_processor_metadata_value?(v) }
174
186
  else
175
187
  false