sprockets 3.0.3 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +101 -0
- data/{LICENSE → MIT-LICENSE} +2 -2
- data/README.md +531 -276
- data/bin/sprockets +12 -7
- data/lib/rake/sprocketstask.rb +9 -4
- data/lib/sprockets/add_source_map_comment_to_asset_processor.rb +60 -0
- data/lib/sprockets/asset.rb +41 -28
- data/lib/sprockets/autoload/babel.rb +8 -0
- data/lib/sprockets/autoload/closure.rb +1 -0
- data/lib/sprockets/autoload/coffee_script.rb +1 -0
- data/lib/sprockets/autoload/eco.rb +1 -0
- data/lib/sprockets/autoload/ejs.rb +1 -0
- data/lib/sprockets/autoload/jsminc.rb +8 -0
- data/lib/sprockets/autoload/sass.rb +1 -0
- data/lib/sprockets/autoload/sassc.rb +8 -0
- data/lib/sprockets/autoload/uglifier.rb +1 -0
- data/lib/sprockets/autoload/yui.rb +1 -0
- data/lib/sprockets/autoload/zopfli.rb +7 -0
- data/lib/sprockets/autoload.rb +5 -0
- data/lib/sprockets/babel_processor.rb +66 -0
- data/lib/sprockets/base.rb +61 -13
- data/lib/sprockets/bower.rb +6 -3
- data/lib/sprockets/bundle.rb +41 -5
- data/lib/sprockets/cache/file_store.rb +32 -7
- data/lib/sprockets/cache/memory_store.rb +28 -10
- data/lib/sprockets/cache/null_store.rb +8 -0
- data/lib/sprockets/cache.rb +43 -6
- data/lib/sprockets/cached_environment.rb +15 -20
- data/lib/sprockets/closure_compressor.rb +6 -11
- data/lib/sprockets/coffee_script_processor.rb +20 -6
- data/lib/sprockets/compressing.rb +62 -2
- data/lib/sprockets/configuration.rb +5 -9
- data/lib/sprockets/context.rb +99 -25
- data/lib/sprockets/dependencies.rb +10 -9
- data/lib/sprockets/digest_utils.rb +103 -62
- data/lib/sprockets/directive_processor.rb +64 -36
- data/lib/sprockets/eco_processor.rb +4 -3
- data/lib/sprockets/ejs_processor.rb +4 -3
- data/lib/sprockets/encoding_utils.rb +1 -0
- data/lib/sprockets/environment.rb +9 -4
- data/lib/sprockets/erb_processor.rb +34 -21
- data/lib/sprockets/errors.rb +1 -0
- data/lib/sprockets/exporters/base.rb +71 -0
- data/lib/sprockets/exporters/file_exporter.rb +24 -0
- data/lib/sprockets/exporters/zlib_exporter.rb +33 -0
- data/lib/sprockets/exporters/zopfli_exporter.rb +14 -0
- data/lib/sprockets/exporting.rb +73 -0
- data/lib/sprockets/file_reader.rb +1 -0
- data/lib/sprockets/http_utils.rb +25 -7
- data/lib/sprockets/jsminc_compressor.rb +32 -0
- data/lib/sprockets/jst_processor.rb +11 -10
- data/lib/sprockets/loader.rb +244 -62
- data/lib/sprockets/manifest.rb +100 -46
- data/lib/sprockets/manifest_utils.rb +9 -6
- data/lib/sprockets/mime.rb +8 -42
- data/lib/sprockets/npm.rb +52 -0
- data/lib/sprockets/path_dependency_utils.rb +3 -11
- data/lib/sprockets/path_digest_utils.rb +2 -1
- data/lib/sprockets/path_utils.rb +107 -22
- data/lib/sprockets/paths.rb +1 -0
- data/lib/sprockets/preprocessors/default_source_map.rb +49 -0
- data/lib/sprockets/processing.rb +32 -52
- data/lib/sprockets/processor_utils.rb +38 -39
- data/lib/sprockets/resolve.rb +177 -97
- data/lib/sprockets/sass_cache_store.rb +1 -0
- data/lib/sprockets/sass_compressor.rb +21 -17
- data/lib/sprockets/sass_functions.rb +1 -0
- data/lib/sprockets/sass_importer.rb +1 -0
- data/lib/sprockets/sass_processor.rb +46 -18
- data/lib/sprockets/sassc_compressor.rb +56 -0
- data/lib/sprockets/sassc_processor.rb +297 -0
- data/lib/sprockets/server.rb +77 -44
- data/lib/sprockets/source_map_processor.rb +66 -0
- data/lib/sprockets/source_map_utils.rb +483 -0
- data/lib/sprockets/transformers.rb +63 -35
- data/lib/sprockets/uglifier_compressor.rb +23 -20
- data/lib/sprockets/unloaded_asset.rb +139 -0
- data/lib/sprockets/uri_tar.rb +99 -0
- data/lib/sprockets/uri_utils.rb +14 -14
- data/lib/sprockets/utils/gzip.rb +99 -0
- data/lib/sprockets/utils.rb +63 -71
- data/lib/sprockets/version.rb +2 -1
- data/lib/sprockets/yui_compressor.rb +5 -14
- data/lib/sprockets.rb +105 -33
- metadata +157 -27
- data/lib/sprockets/coffee_script_template.rb +0 -6
- data/lib/sprockets/eco_template.rb +0 -6
- data/lib/sprockets/ejs_template.rb +0 -6
- data/lib/sprockets/engines.rb +0 -81
- data/lib/sprockets/erb_template.rb +0 -6
- data/lib/sprockets/legacy.rb +0 -314
- data/lib/sprockets/legacy_proc_processor.rb +0 -35
- data/lib/sprockets/legacy_tilt_processor.rb +0 -29
- data/lib/sprockets/sass_template.rb +0 -7
data/lib/sprockets/bundle.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'set'
|
2
3
|
require 'sprockets/utils'
|
4
|
+
require 'sprockets/uri_utils'
|
3
5
|
|
4
6
|
module Sprockets
|
5
7
|
# Internal: Bundle processor takes a single file asset and prepends all the
|
@@ -8,22 +10,39 @@ module Sprockets
|
|
8
10
|
# Uses pipeline metadata:
|
9
11
|
#
|
10
12
|
# :required - Ordered Set of asset URIs to prepend
|
11
|
-
# :stubbed - Set of asset URIs to
|
13
|
+
# :stubbed - Set of asset URIs to subtract from the required set.
|
12
14
|
#
|
13
15
|
# Also see DirectiveProcessor.
|
14
16
|
class Bundle
|
15
17
|
def self.call(input)
|
16
18
|
env = input[:environment]
|
17
19
|
type = input[:content_type]
|
20
|
+
input[:links] ||= Set.new
|
18
21
|
dependencies = Set.new(input[:metadata][:dependencies])
|
19
22
|
|
20
|
-
processed_uri, deps = env.resolve(input[:filename], accept: type, pipeline: :self
|
23
|
+
processed_uri, deps = env.resolve(input[:filename], accept: type, pipeline: :self)
|
21
24
|
dependencies.merge(deps)
|
22
25
|
|
26
|
+
# DirectiveProcessor (and any other transformers called here with pipeline=self)
|
27
|
+
primary_asset = env.load(processed_uri)
|
28
|
+
to_load = primary_asset.metadata.delete(:to_load) || Set.new
|
29
|
+
to_link = primary_asset.metadata.delete(:to_link) || Set.new
|
30
|
+
|
31
|
+
to_load.each do |uri|
|
32
|
+
loaded_asset = env.load(uri)
|
33
|
+
dependencies.merge(loaded_asset.metadata[:dependencies])
|
34
|
+
if to_link.include?(uri)
|
35
|
+
primary_metadata = primary_asset.metadata
|
36
|
+
input[:links] << loaded_asset.uri
|
37
|
+
primary_metadata[:links] << loaded_asset.uri
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
23
41
|
find_required = proc { |uri| env.load(uri).metadata[:required] }
|
24
42
|
required = Utils.dfs(processed_uri, &find_required)
|
25
43
|
stubbed = Utils.dfs(env.load(processed_uri).metadata[:stubbed], &find_required)
|
26
44
|
required.subtract(stubbed)
|
45
|
+
dedup(required)
|
27
46
|
assets = required.map { |uri| env.load(uri) }
|
28
47
|
|
29
48
|
(required + stubbed).each do |uri|
|
@@ -31,21 +50,38 @@ module Sprockets
|
|
31
50
|
end
|
32
51
|
|
33
52
|
reducers = Hash[env.match_mime_type_keys(env.config[:bundle_reducers], type).flat_map(&:to_a)]
|
34
|
-
process_bundle_reducers(assets, reducers).merge(dependencies: dependencies, included: assets.map(&:uri))
|
53
|
+
process_bundle_reducers(input, assets, reducers).merge(dependencies: dependencies, included: assets.map(&:uri))
|
54
|
+
end
|
55
|
+
|
56
|
+
# Internal: Removes uri from required if it's already included as an alias.
|
57
|
+
#
|
58
|
+
# required - Set of required uris
|
59
|
+
#
|
60
|
+
# Returns deduped set of uris
|
61
|
+
def self.dedup(required)
|
62
|
+
dupes = required.reduce([]) do |r, uri|
|
63
|
+
path, params = URIUtils.parse_asset_uri(uri)
|
64
|
+
if (params.delete(:index_alias))
|
65
|
+
r << URIUtils.build_asset_uri(path, params)
|
66
|
+
end
|
67
|
+
r
|
68
|
+
end
|
69
|
+
required.subtract(dupes)
|
35
70
|
end
|
36
71
|
|
37
72
|
# Internal: Run bundle reducers on set of Assets producing a reduced
|
38
73
|
# metadata Hash.
|
39
74
|
#
|
75
|
+
# filename - String bundle filename
|
40
76
|
# assets - Array of Assets
|
41
77
|
# reducers - Array of [initial, reducer_proc] pairs
|
42
78
|
#
|
43
79
|
# Returns reduced asset metadata Hash.
|
44
|
-
def self.process_bundle_reducers(assets, reducers)
|
80
|
+
def self.process_bundle_reducers(input, assets, reducers)
|
45
81
|
initial = {}
|
46
82
|
reducers.each do |k, (v, _)|
|
47
83
|
if v.respond_to?(:call)
|
48
|
-
initial[k] = v.call
|
84
|
+
initial[k] = v.call(input)
|
49
85
|
elsif !v.nil?
|
50
86
|
initial[k] = v
|
51
87
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'fileutils'
|
2
3
|
require 'logger'
|
3
4
|
require 'sprockets/encoding_utils'
|
@@ -19,7 +20,9 @@ module Sprockets
|
|
19
20
|
class FileStore
|
20
21
|
# Internal: Default key limit for store.
|
21
22
|
DEFAULT_MAX_SIZE = 25 * 1024 * 1024
|
22
|
-
|
23
|
+
EXCLUDED_DIRS = ['.', '..'].freeze
|
24
|
+
GITKEEP_FILES = ['.gitkeep', '.keep'].freeze
|
25
|
+
|
23
26
|
# Internal: Default standard error fatal logger.
|
24
27
|
#
|
25
28
|
# Returns a Logger.
|
@@ -32,11 +35,12 @@ module Sprockets
|
|
32
35
|
# Public: Initialize the cache store.
|
33
36
|
#
|
34
37
|
# root - A String path to a directory to persist cached values to.
|
35
|
-
# max_size - A Integer of the maximum
|
36
|
-
# (default:
|
38
|
+
# max_size - A Integer of the maximum size the store will hold (in bytes).
|
39
|
+
# (default: 25MB).
|
40
|
+
# logger - The logger to which some info will be printed.
|
41
|
+
# (default logger level is FATAL and won't output anything).
|
37
42
|
def initialize(root, max_size = DEFAULT_MAX_SIZE, logger = self.class.default_logger)
|
38
43
|
@root = root
|
39
|
-
@size = find_caches.inject(0) { |n, (_, stat)| n + stat.size }
|
40
44
|
@max_size = max_size
|
41
45
|
@gc_size = max_size * 0.75
|
42
46
|
@logger = logger
|
@@ -107,11 +111,11 @@ module Sprockets
|
|
107
111
|
# Write data
|
108
112
|
PathUtils.atomic_write(path) do |f|
|
109
113
|
f.write(raw)
|
110
|
-
@size
|
114
|
+
@size = size + f.size unless exists
|
111
115
|
end
|
112
116
|
|
113
117
|
# GC if necessary
|
114
|
-
gc! if
|
118
|
+
gc! if size > @max_size
|
115
119
|
|
116
120
|
value
|
117
121
|
end
|
@@ -120,7 +124,24 @@ module Sprockets
|
|
120
124
|
#
|
121
125
|
# Returns String.
|
122
126
|
def inspect
|
123
|
-
"#<#{self.class} size=#{
|
127
|
+
"#<#{self.class} size=#{size}/#{@max_size}>"
|
128
|
+
end
|
129
|
+
|
130
|
+
# Public: Clear the cache
|
131
|
+
#
|
132
|
+
# adapted from ActiveSupport::Cache::FileStore#clear
|
133
|
+
#
|
134
|
+
# Deletes all items from the cache. In this case it deletes all the entries in the specified
|
135
|
+
# file store directory except for .keep or .gitkeep. Be careful which directory is specified
|
136
|
+
# as @root because everything in that directory will be deleted.
|
137
|
+
#
|
138
|
+
# Returns true
|
139
|
+
def clear(options=nil)
|
140
|
+
if File.exist?(@root)
|
141
|
+
root_dirs = Dir.entries(@root).reject { |f| (EXCLUDED_DIRS + GITKEEP_FILES).include?(f) }
|
142
|
+
FileUtils.rm_r(root_dirs.collect{ |f| File.join(@root, f) })
|
143
|
+
end
|
144
|
+
true
|
124
145
|
end
|
125
146
|
|
126
147
|
private
|
@@ -138,6 +159,10 @@ module Sprockets
|
|
138
159
|
}.sort_by { |_, stat| stat.mtime.to_i }
|
139
160
|
end
|
140
161
|
|
162
|
+
def size
|
163
|
+
@size ||= compute_size(find_caches)
|
164
|
+
end
|
165
|
+
|
141
166
|
def compute_size(caches)
|
142
167
|
caches.inject(0) { |sum, (_, stat)| sum + stat.size }
|
143
168
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Sprockets
|
2
3
|
class Cache
|
3
4
|
# Public: Basic in memory LRU cache.
|
@@ -21,6 +22,7 @@ module Sprockets
|
|
21
22
|
def initialize(max_size = DEFAULT_MAX_SIZE)
|
22
23
|
@max_size = max_size
|
23
24
|
@cache = {}
|
25
|
+
@mutex = Mutex.new
|
24
26
|
end
|
25
27
|
|
26
28
|
# Public: Retrieve value from cache.
|
@@ -31,12 +33,14 @@ module Sprockets
|
|
31
33
|
#
|
32
34
|
# Returns Object or nil or the value is not set.
|
33
35
|
def get(key)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
@mutex.synchronize do
|
37
|
+
exists = true
|
38
|
+
value = @cache.delete(key) { exists = false }
|
39
|
+
if exists
|
40
|
+
@cache[key] = value
|
41
|
+
else
|
42
|
+
nil
|
43
|
+
end
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
@@ -49,9 +53,11 @@ module Sprockets
|
|
49
53
|
#
|
50
54
|
# Returns Object value.
|
51
55
|
def set(key, value)
|
52
|
-
@
|
53
|
-
|
54
|
-
|
56
|
+
@mutex.synchronize do
|
57
|
+
@cache.delete(key)
|
58
|
+
@cache[key] = value
|
59
|
+
@cache.shift if @cache.size > @max_size
|
60
|
+
end
|
55
61
|
value
|
56
62
|
end
|
57
63
|
|
@@ -59,7 +65,19 @@ module Sprockets
|
|
59
65
|
#
|
60
66
|
# Returns String.
|
61
67
|
def inspect
|
62
|
-
|
68
|
+
@mutex.synchronize do
|
69
|
+
"#<#{self.class} size=#{@cache.size}/#{@max_size}>"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Public: Clear the cache
|
74
|
+
#
|
75
|
+
# Returns true
|
76
|
+
def clear(options=nil)
|
77
|
+
@mutex.synchronize do
|
78
|
+
@cache.clear
|
79
|
+
end
|
80
|
+
true
|
63
81
|
end
|
64
82
|
end
|
65
83
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module Sprockets
|
2
3
|
class Cache
|
3
4
|
# Public: A compatible cache store that doesn't store anything. Used by
|
@@ -41,6 +42,13 @@ module Sprockets
|
|
41
42
|
def inspect
|
42
43
|
"#<#{self.class}>"
|
43
44
|
end
|
45
|
+
|
46
|
+
# Public: Simulate clearing the cache
|
47
|
+
#
|
48
|
+
# Returns true
|
49
|
+
def clear(options=nil)
|
50
|
+
true
|
51
|
+
end
|
44
52
|
end
|
45
53
|
end
|
46
54
|
end
|
data/lib/sprockets/cache.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'logger'
|
2
3
|
require 'sprockets/digest_utils'
|
3
4
|
|
@@ -35,6 +36,12 @@ module Sprockets
|
|
35
36
|
#
|
36
37
|
# Returns argument value.
|
37
38
|
#
|
39
|
+
# clear(options)
|
40
|
+
#
|
41
|
+
# Clear the entire cache. Be careful with this method since it could
|
42
|
+
# affect other processes if shared cache is being used.
|
43
|
+
#
|
44
|
+
# The options hash is passed to the underlying cache implementation.
|
38
45
|
class Cache
|
39
46
|
# Builtin cache stores.
|
40
47
|
autoload :FileStore, 'sprockets/cache/file_store'
|
@@ -44,7 +51,7 @@ module Sprockets
|
|
44
51
|
# Internal: Cache key version for this class. Rarely should have to change
|
45
52
|
# unless the cache format radically changes. Will be bump on major version
|
46
53
|
# releases though.
|
47
|
-
VERSION = '
|
54
|
+
VERSION = '4.0.0'
|
48
55
|
|
49
56
|
def self.default_logger
|
50
57
|
logger = Logger.new($stderr)
|
@@ -55,12 +62,12 @@ module Sprockets
|
|
55
62
|
# Internal: Wrap a backend cache store.
|
56
63
|
#
|
57
64
|
# Always assign a backend cache store instance to Environment#cache= and
|
58
|
-
# use Environment#cache to
|
65
|
+
# use Environment#cache to retrieve a wrapped interface.
|
59
66
|
#
|
60
67
|
# cache - A compatible backend cache store instance.
|
61
68
|
def initialize(cache = nil, logger = self.class.default_logger)
|
62
69
|
@cache_wrapper = get_cache_wrapper(cache)
|
63
|
-
@fetch_cache = Cache::MemoryStore.new(
|
70
|
+
@fetch_cache = Cache::MemoryStore.new(1024)
|
64
71
|
@logger = logger
|
65
72
|
end
|
66
73
|
|
@@ -97,7 +104,7 @@ module Sprockets
|
|
97
104
|
# Public: Low level API to retrieve item directly from the backend cache
|
98
105
|
# store.
|
99
106
|
#
|
100
|
-
# This API may be used
|
107
|
+
# This API may be used publicly, but may have undefined behavior
|
101
108
|
# depending on the backend store being used. Prefer the
|
102
109
|
# Cache#fetch API over using this.
|
103
110
|
#
|
@@ -120,7 +127,7 @@ module Sprockets
|
|
120
127
|
|
121
128
|
# Public: Low level API to set item directly to the backend cache store.
|
122
129
|
#
|
123
|
-
# This API may be used
|
130
|
+
# This API may be used publicly, but may have undefined behavior
|
124
131
|
# depending on the backend store being used. Prefer the
|
125
132
|
# Cache#fetch API over using this.
|
126
133
|
#
|
@@ -143,6 +150,14 @@ module Sprockets
|
|
143
150
|
"#<#{self.class} local=#{@fetch_cache.inspect} store=#{@cache_wrapper.cache.inspect}>"
|
144
151
|
end
|
145
152
|
|
153
|
+
# Public: Clear cache
|
154
|
+
#
|
155
|
+
# Returns truthy on success, potentially raises exception on failure
|
156
|
+
def clear(options=nil)
|
157
|
+
@cache_wrapper.clear
|
158
|
+
@fetch_cache.clear
|
159
|
+
end
|
160
|
+
|
146
161
|
private
|
147
162
|
# Internal: Expand object cache key into a short String key.
|
148
163
|
#
|
@@ -153,7 +168,9 @@ module Sprockets
|
|
153
168
|
#
|
154
169
|
# Returns a String with a length less than 250 characters.
|
155
170
|
def expand_key(key)
|
156
|
-
|
171
|
+
digest_key = DigestUtils.pack_urlsafe_base64digest(DigestUtils.digest(key))
|
172
|
+
namespace = digest_key[0, 2]
|
173
|
+
"sprockets/v#{VERSION}/#{namespace}/#{digest_key}"
|
157
174
|
end
|
158
175
|
|
159
176
|
PEEK_SIZE = 100
|
@@ -209,6 +226,16 @@ module Sprockets
|
|
209
226
|
def set(key, value)
|
210
227
|
cache.set(key, value)
|
211
228
|
end
|
229
|
+
|
230
|
+
def clear(options=nil)
|
231
|
+
# dalli has a #flush method so try it
|
232
|
+
if cache.respond_to?(:flush)
|
233
|
+
cache.flush(options)
|
234
|
+
else
|
235
|
+
cache.clear(options)
|
236
|
+
end
|
237
|
+
true
|
238
|
+
end
|
212
239
|
end
|
213
240
|
|
214
241
|
class HashWrapper < Wrapper
|
@@ -219,6 +246,11 @@ module Sprockets
|
|
219
246
|
def set(key, value)
|
220
247
|
cache[key] = value
|
221
248
|
end
|
249
|
+
|
250
|
+
def clear(options=nil)
|
251
|
+
cache.clear
|
252
|
+
true
|
253
|
+
end
|
222
254
|
end
|
223
255
|
|
224
256
|
class ReadWriteWrapper < Wrapper
|
@@ -229,6 +261,11 @@ module Sprockets
|
|
229
261
|
def set(key, value)
|
230
262
|
cache.write(key, value)
|
231
263
|
end
|
264
|
+
|
265
|
+
def clear(options=nil)
|
266
|
+
cache.clear(options)
|
267
|
+
true
|
268
|
+
end
|
232
269
|
end
|
233
270
|
end
|
234
271
|
end
|
@@ -1,26 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'sprockets/base'
|
2
3
|
|
3
4
|
module Sprockets
|
4
|
-
# `
|
5
|
+
# `CachedEnvironment` is a special cached version of `Environment`.
|
5
6
|
#
|
6
|
-
# The
|
7
|
-
# for the instances lifetime. This makes `
|
7
|
+
# The exception is that all of its file system methods are cached
|
8
|
+
# for the instances lifetime. This makes `CachedEnvironment` much faster. This
|
8
9
|
# behavior is ideal in production environments where the file system
|
9
10
|
# is immutable.
|
10
11
|
#
|
11
|
-
# `
|
12
|
+
# `CachedEnvironment` should not be initialized directly. Instead use
|
12
13
|
# `Environment#cached`.
|
13
14
|
class CachedEnvironment < Base
|
14
15
|
def initialize(environment)
|
15
16
|
initialize_configuration(environment)
|
16
17
|
|
17
18
|
@cache = environment.cache
|
18
|
-
@stats =
|
19
|
-
@entries =
|
20
|
-
@uris =
|
21
|
-
|
22
|
-
@
|
23
|
-
@resolved_dependencies = Hash.new { |h, k| h[k] = _resolve_dependency(k) }
|
19
|
+
@stats = Concurrent::Map.new
|
20
|
+
@entries = Concurrent::Map.new
|
21
|
+
@uris = Concurrent::Map.new
|
22
|
+
@processor_cache_keys = Concurrent::Map.new
|
23
|
+
@resolved_dependencies = Concurrent::Map.new
|
24
24
|
end
|
25
25
|
|
26
26
|
# No-op return self as cached environment.
|
@@ -30,33 +30,28 @@ module Sprockets
|
|
30
30
|
alias_method :index, :cached
|
31
31
|
|
32
32
|
# Internal: Cache Environment#entries
|
33
|
-
alias_method :_entries, :entries
|
34
33
|
def entries(path)
|
35
|
-
@entries
|
34
|
+
@entries.fetch_or_store(path) { super(path) }
|
36
35
|
end
|
37
36
|
|
38
37
|
# Internal: Cache Environment#stat
|
39
|
-
alias_method :_stat, :stat
|
40
38
|
def stat(path)
|
41
|
-
@stats
|
39
|
+
@stats.fetch_or_store(path) { super(path) }
|
42
40
|
end
|
43
41
|
|
44
42
|
# Internal: Cache Environment#load
|
45
|
-
alias_method :_load, :load
|
46
43
|
def load(uri)
|
47
|
-
@uris
|
44
|
+
@uris.fetch_or_store(uri) { super(uri) }
|
48
45
|
end
|
49
46
|
|
50
47
|
# Internal: Cache Environment#processor_cache_key
|
51
|
-
alias_method :_processor_cache_key, :processor_cache_key
|
52
48
|
def processor_cache_key(str)
|
53
|
-
@processor_cache_keys
|
49
|
+
@processor_cache_keys.fetch_or_store(str) { super(str) }
|
54
50
|
end
|
55
51
|
|
56
52
|
# Internal: Cache Environment#resolve_dependency
|
57
|
-
alias_method :_resolve_dependency, :resolve_dependency
|
58
53
|
def resolve_dependency(str)
|
59
|
-
@resolved_dependencies
|
54
|
+
@resolved_dependencies.fetch_or_store(str) { super(str) }
|
60
55
|
end
|
61
56
|
|
62
57
|
private
|
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'sprockets/autoload'
|
3
|
+
require 'sprockets/digest_utils'
|
2
4
|
|
3
5
|
module Sprockets
|
4
6
|
# Public: Closure Compiler minifier.
|
@@ -34,20 +36,13 @@ module Sprockets
|
|
34
36
|
attr_reader :cache_key
|
35
37
|
|
36
38
|
def initialize(options = {})
|
37
|
-
@
|
38
|
-
@cache_key =
|
39
|
-
self.class.name,
|
40
|
-
Autoload::Closure::VERSION,
|
41
|
-
Autoload::Closure::COMPILER_VERSION,
|
42
|
-
VERSION,
|
43
|
-
options
|
44
|
-
].freeze
|
39
|
+
@options = options
|
40
|
+
@cache_key = "#{self.class.name}:#{Autoload::Closure::VERSION}:#{Autoload::Closure::COMPILER_VERSION}:#{VERSION}:#{DigestUtils.digest(options)}".freeze
|
45
41
|
end
|
46
42
|
|
47
43
|
def call(input)
|
48
|
-
|
49
|
-
|
50
|
-
end
|
44
|
+
@compiler ||= Autoload::Closure::Compiler.new(@options)
|
45
|
+
@compiler.compile(input[:data])
|
51
46
|
end
|
52
47
|
end
|
53
48
|
end
|
@@ -1,25 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'sprockets/autoload'
|
3
|
+
require 'sprockets/source_map_utils'
|
2
4
|
|
3
5
|
module Sprockets
|
4
6
|
# Processor engine class for the CoffeeScript compiler.
|
5
7
|
# Depends on the `coffee-script` and `coffee-script-source` gems.
|
6
8
|
#
|
7
|
-
# For more
|
9
|
+
# For more information see:
|
8
10
|
#
|
9
|
-
# https://github.com/
|
11
|
+
# https://github.com/rails/ruby-coffee-script
|
10
12
|
#
|
11
13
|
module CoffeeScriptProcessor
|
12
|
-
VERSION = '
|
14
|
+
VERSION = '2'
|
13
15
|
|
14
16
|
def self.cache_key
|
15
|
-
@cache_key ||=
|
17
|
+
@cache_key ||= "#{name}:#{Autoload::CoffeeScript::Source.version}:#{VERSION}".freeze
|
16
18
|
end
|
17
19
|
|
18
20
|
def self.call(input)
|
19
21
|
data = input[:data]
|
20
|
-
|
21
|
-
|
22
|
+
|
23
|
+
js, map = input[:cache].fetch([self.cache_key, data, input[:filename]]) do
|
24
|
+
result = Autoload::CoffeeScript.compile(
|
25
|
+
data,
|
26
|
+
sourceMap: "v3",
|
27
|
+
sourceFiles: [File.basename(input[:filename])],
|
28
|
+
generatedFile: input[:filename]
|
29
|
+
)
|
30
|
+
[result['js'], JSON.parse(result['v3SourceMap'])]
|
22
31
|
end
|
32
|
+
|
33
|
+
map = SourceMapUtils.format_source_map(map, input)
|
34
|
+
map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map)
|
35
|
+
|
36
|
+
{ data: js, map: map }
|
23
37
|
end
|
24
38
|
end
|
25
39
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'sprockets/utils'
|
2
3
|
|
3
4
|
module Sprockets
|
@@ -10,6 +11,24 @@ module Sprockets
|
|
10
11
|
config[:compressors]
|
11
12
|
end
|
12
13
|
|
14
|
+
# Public: Register a new compressor `klass` at `sym` for `mime_type`.
|
15
|
+
#
|
16
|
+
# Registering a processor allows it to be looked up by `sym` later when
|
17
|
+
# assigning a JavaScript or CSS compressor.
|
18
|
+
#
|
19
|
+
# Compressors only operate on JavaScript and CSS. If you want to compress a
|
20
|
+
# different type of asset, use a processor instead.
|
21
|
+
#
|
22
|
+
# Examples
|
23
|
+
#
|
24
|
+
# register_compressor 'text/css', :my_sass, MySassCompressor
|
25
|
+
# css_compressor = :my_sass
|
26
|
+
#
|
27
|
+
# mime_type - String MIME Type (one of: 'test/css' or 'application/javascript').
|
28
|
+
# sym - Symbol registration address.
|
29
|
+
# klass - The compressor class.
|
30
|
+
#
|
31
|
+
# Returns nothing.
|
13
32
|
def register_compressor(mime_type, sym, klass)
|
14
33
|
self.config = hash_reassoc(config, :compressors, mime_type) do |compressors|
|
15
34
|
compressors[sym] = klass
|
@@ -35,7 +54,7 @@ module Sprockets
|
|
35
54
|
if compressor.is_a?(Symbol)
|
36
55
|
@css_compressor = klass = config[:compressors]['text/css'][compressor] || raise(Error, "unknown compressor: #{compressor}")
|
37
56
|
elsif compressor.respond_to?(:compress)
|
38
|
-
klass =
|
57
|
+
klass = proc { |input| compressor.compress(input[:data]) }
|
39
58
|
@css_compressor = :css_compressor
|
40
59
|
else
|
41
60
|
@css_compressor = klass = compressor
|
@@ -62,7 +81,7 @@ module Sprockets
|
|
62
81
|
if compressor.is_a?(Symbol)
|
63
82
|
@js_compressor = klass = config[:compressors]['application/javascript'][compressor] || raise(Error, "unknown compressor: #{compressor}")
|
64
83
|
elsif compressor.respond_to?(:compress)
|
65
|
-
klass =
|
84
|
+
klass = proc { |input| compressor.compress(input[:data]) }
|
66
85
|
@js_compressor = :js_compressor
|
67
86
|
else
|
68
87
|
@js_compressor = klass = compressor
|
@@ -70,5 +89,46 @@ module Sprockets
|
|
70
89
|
|
71
90
|
register_bundle_processor 'application/javascript', klass
|
72
91
|
end
|
92
|
+
|
93
|
+
# Public: Checks if Gzip is enabled.
|
94
|
+
def gzip?
|
95
|
+
config[:gzip_enabled]
|
96
|
+
end
|
97
|
+
|
98
|
+
# Public: Checks if Gzip is disabled.
|
99
|
+
def skip_gzip?
|
100
|
+
!gzip?
|
101
|
+
end
|
102
|
+
|
103
|
+
# Public: Enable or disable the creation of Gzip files.
|
104
|
+
#
|
105
|
+
# To disable gzip generation set to a falsey value:
|
106
|
+
#
|
107
|
+
# environment.gzip = false
|
108
|
+
#
|
109
|
+
# To enable set to a truthy value. By default zlib wil
|
110
|
+
# be used to gzip assets. If you have the Zopfli gem
|
111
|
+
# installed you can specify the zopfli algorithm to be used
|
112
|
+
# instead:
|
113
|
+
#
|
114
|
+
# environment.gzip = :zopfli
|
115
|
+
#
|
116
|
+
def gzip=(gzip)
|
117
|
+
self.config = config.merge(gzip_enabled: gzip).freeze
|
118
|
+
|
119
|
+
case gzip
|
120
|
+
when false, nil
|
121
|
+
self.unregister_exporter Exporters::ZlibExporter
|
122
|
+
self.unregister_exporter Exporters::ZopfliExporter
|
123
|
+
when :zopfli
|
124
|
+
self.unregister_exporter Exporters::ZlibExporter
|
125
|
+
self.register_exporter '*/*', Exporters::ZopfliExporter
|
126
|
+
else
|
127
|
+
self.unregister_exporter Exporters::ZopfliExporter
|
128
|
+
self.register_exporter '*/*', Exporters::ZlibExporter
|
129
|
+
end
|
130
|
+
|
131
|
+
gzip
|
132
|
+
end
|
73
133
|
end
|
74
134
|
end
|
@@ -1,27 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'sprockets/compressing'
|
2
3
|
require 'sprockets/dependencies'
|
3
|
-
require 'sprockets/engines'
|
4
4
|
require 'sprockets/mime'
|
5
5
|
require 'sprockets/paths'
|
6
6
|
require 'sprockets/processing'
|
7
|
+
require 'sprockets/exporting'
|
7
8
|
require 'sprockets/transformers'
|
8
9
|
require 'sprockets/utils'
|
9
10
|
|
10
11
|
module Sprockets
|
11
12
|
module Configuration
|
12
|
-
include Paths, Mime,
|
13
|
+
include Paths, Mime, Transformers, Processing, Exporting, Compressing, Dependencies, Utils
|
13
14
|
|
14
15
|
def initialize_configuration(parent)
|
15
16
|
@config = parent.config
|
16
|
-
@computed_config = parent.computed_config
|
17
17
|
@logger = parent.logger
|
18
18
|
@context_class = Class.new(parent.context_class)
|
19
19
|
end
|
20
20
|
|
21
21
|
attr_reader :config
|
22
22
|
|
23
|
-
attr_accessor :computed_config
|
24
|
-
|
25
23
|
def config=(config)
|
26
24
|
raise TypeError, "can't assign mutable config" unless config.frozen?
|
27
25
|
@config = config
|
@@ -61,16 +59,14 @@ module Sprockets
|
|
61
59
|
|
62
60
|
# Deprecated: Assign a `Digest` implementation class. This maybe any Ruby
|
63
61
|
# `Digest::` implementation such as `Digest::SHA256` or
|
64
|
-
# `Digest::
|
62
|
+
# `Digest::SHA512`.
|
65
63
|
#
|
66
|
-
# environment.digest_class = Digest::
|
64
|
+
# environment.digest_class = Digest::SHA512
|
67
65
|
#
|
68
66
|
def digest_class=(klass)
|
69
67
|
self.config = config.merge(digest_class: klass).freeze
|
70
68
|
end
|
71
69
|
|
72
|
-
# Deprecated: Get `Context` class.
|
73
|
-
#
|
74
70
|
# This class maybe mutated and mixed in with custom helpers.
|
75
71
|
#
|
76
72
|
# environment.context_class.instance_eval do
|