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.
- checksums.yaml +4 -4
- data/LICENSE +2 -2
- data/README.md +226 -119
- data/lib/rake/sprocketstask.rb +5 -4
- data/lib/sprockets/asset.rb +143 -211
- data/lib/sprockets/autoload/closure.rb +7 -0
- data/lib/sprockets/autoload/coffee_script.rb +7 -0
- data/lib/sprockets/autoload/eco.rb +7 -0
- data/lib/sprockets/autoload/ejs.rb +7 -0
- data/lib/sprockets/autoload/sass.rb +7 -0
- data/lib/sprockets/autoload/uglifier.rb +7 -0
- data/lib/sprockets/autoload/yui.rb +7 -0
- data/lib/sprockets/autoload.rb +11 -0
- data/lib/sprockets/base.rb +47 -395
- data/lib/sprockets/bower.rb +58 -0
- data/lib/sprockets/bundle.rb +65 -0
- data/lib/sprockets/cache/file_store.rb +165 -14
- data/lib/sprockets/cache/memory_store.rb +66 -0
- data/lib/sprockets/cache/null_store.rb +46 -0
- data/lib/sprockets/cache.rb +234 -0
- data/lib/sprockets/cached_environment.rb +69 -0
- data/lib/sprockets/closure_compressor.rb +41 -10
- data/lib/sprockets/coffee_script_processor.rb +25 -0
- data/lib/sprockets/coffee_script_template.rb +6 -0
- data/lib/sprockets/compressing.rb +24 -23
- data/lib/sprockets/configuration.rb +83 -0
- data/lib/sprockets/context.rb +84 -144
- data/lib/sprockets/dependencies.rb +73 -0
- data/lib/sprockets/digest_utils.rb +156 -0
- data/lib/sprockets/directive_processor.rb +207 -211
- data/lib/sprockets/eco_processor.rb +32 -0
- data/lib/sprockets/eco_template.rb +3 -35
- data/lib/sprockets/ejs_processor.rb +31 -0
- data/lib/sprockets/ejs_template.rb +3 -34
- data/lib/sprockets/encoding_utils.rb +258 -0
- data/lib/sprockets/engines.rb +42 -35
- data/lib/sprockets/environment.rb +17 -64
- data/lib/sprockets/erb_processor.rb +30 -0
- data/lib/sprockets/erb_template.rb +6 -0
- data/lib/sprockets/errors.rb +5 -13
- data/lib/sprockets/file_reader.rb +15 -0
- data/lib/sprockets/http_utils.rb +115 -0
- data/lib/sprockets/jst_processor.rb +35 -15
- data/lib/sprockets/legacy.rb +314 -0
- data/lib/sprockets/legacy_proc_processor.rb +35 -0
- data/lib/sprockets/legacy_tilt_processor.rb +29 -0
- data/lib/sprockets/loader.rb +176 -0
- data/lib/sprockets/manifest.rb +153 -129
- data/lib/sprockets/manifest_utils.rb +45 -0
- data/lib/sprockets/mime.rb +112 -31
- data/lib/sprockets/path_dependency_utils.rb +85 -0
- data/lib/sprockets/path_digest_utils.rb +47 -0
- data/lib/sprockets/path_utils.rb +282 -0
- data/lib/sprockets/paths.rb +42 -19
- data/lib/sprockets/processing.rb +168 -126
- data/lib/sprockets/processor_utils.rb +103 -0
- data/lib/sprockets/resolve.rb +208 -0
- data/lib/sprockets/sass_cache_store.rb +18 -18
- data/lib/sprockets/sass_compressor.rb +47 -15
- data/lib/sprockets/sass_functions.rb +2 -70
- data/lib/sprockets/sass_importer.rb +2 -30
- data/lib/sprockets/sass_processor.rb +285 -0
- data/lib/sprockets/sass_template.rb +4 -63
- data/lib/sprockets/server.rb +104 -79
- data/lib/sprockets/transformers.rb +145 -0
- data/lib/sprockets/uglifier_compressor.rb +46 -12
- data/lib/sprockets/uri_utils.rb +190 -0
- data/lib/sprockets/utils.rb +193 -44
- data/lib/sprockets/version.rb +1 -1
- data/lib/sprockets/yui_compressor.rb +49 -11
- data/lib/sprockets.rb +132 -81
- metadata +84 -89
- data/lib/sprockets/asset_attributes.rb +0 -137
- data/lib/sprockets/bundled_asset.rb +0 -78
- data/lib/sprockets/caching.rb +0 -96
- data/lib/sprockets/charset_normalizer.rb +0 -41
- data/lib/sprockets/index.rb +0 -100
- data/lib/sprockets/processed_asset.rb +0 -152
- data/lib/sprockets/processor.rb +0 -32
- data/lib/sprockets/safety_colons.rb +0 -28
- data/lib/sprockets/scss_template.rb +0 -13
- data/lib/sprockets/static_asset.rb +0 -58
data/lib/sprockets/processing.rb
CHANGED
|
@@ -1,62 +1,40 @@
|
|
|
1
1
|
require 'sprockets/engines'
|
|
2
|
+
require 'sprockets/file_reader'
|
|
3
|
+
require 'sprockets/legacy_proc_processor'
|
|
4
|
+
require 'sprockets/legacy_tilt_processor'
|
|
2
5
|
require 'sprockets/mime'
|
|
3
|
-
require 'sprockets/
|
|
6
|
+
require 'sprockets/processor_utils'
|
|
7
|
+
require 'sprockets/uri_utils'
|
|
4
8
|
require 'sprockets/utils'
|
|
5
9
|
|
|
6
10
|
module Sprockets
|
|
7
11
|
# `Processing` is an internal mixin whose public methods are exposed on
|
|
8
|
-
# the `Environment` and `
|
|
12
|
+
# the `Environment` and `CachedEnvironment` classes.
|
|
9
13
|
module Processing
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
#
|
|
15
|
-
def format_extensions
|
|
16
|
-
@trail.extensions - @engines.keys
|
|
14
|
+
include ProcessorUtils, URIUtils, Utils
|
|
15
|
+
|
|
16
|
+
def pipelines
|
|
17
|
+
config[:pipelines]
|
|
17
18
|
end
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
def register_pipeline(name, proc = nil, &block)
|
|
21
|
+
proc ||= block
|
|
22
|
+
|
|
23
|
+
self.config = hash_reassoc(config, :pipelines) do |pipelines|
|
|
24
|
+
pipelines.merge(name.to_sym => proc)
|
|
25
|
+
end
|
|
22
26
|
end
|
|
23
27
|
|
|
24
|
-
# Returns an `Array` of `Processor` classes. If a `mime_type`
|
|
25
|
-
# argument is supplied, the processors registered under that
|
|
26
|
-
# extension will be returned.
|
|
27
|
-
#
|
|
28
28
|
# Preprocessors are ran before Postprocessors and Engine
|
|
29
29
|
# processors.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
# recommended to subclass `Tilt::Template`.
|
|
33
|
-
def preprocessors(mime_type = nil)
|
|
34
|
-
if mime_type
|
|
35
|
-
@preprocessors[mime_type].dup
|
|
36
|
-
else
|
|
37
|
-
deep_copy_hash(@preprocessors)
|
|
38
|
-
end
|
|
30
|
+
def preprocessors
|
|
31
|
+
config[:preprocessors]
|
|
39
32
|
end
|
|
33
|
+
alias_method :processors, :preprocessors
|
|
40
34
|
|
|
41
|
-
# Returns an `Array` of `Processor` classes. If a `mime_type`
|
|
42
|
-
# argument is supplied, the processors registered under that
|
|
43
|
-
# extension will be returned.
|
|
44
|
-
#
|
|
45
35
|
# Postprocessors are ran after Preprocessors and Engine processors.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
# recommended to subclass `Tilt::Template`.
|
|
49
|
-
def postprocessors(mime_type = nil)
|
|
50
|
-
if mime_type
|
|
51
|
-
@postprocessors[mime_type].dup
|
|
52
|
-
else
|
|
53
|
-
deep_copy_hash(@postprocessors)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Deprecated alias for `register_preprocessor`.
|
|
58
|
-
def register_processor(*args, &block)
|
|
59
|
-
register_preprocessor(*args, &block)
|
|
36
|
+
def postprocessors
|
|
37
|
+
config[:postprocessors]
|
|
60
38
|
end
|
|
61
39
|
|
|
62
40
|
# Registers a new Preprocessor `klass` for `mime_type`.
|
|
@@ -69,137 +47,201 @@ module Sprockets
|
|
|
69
47
|
# data.gsub(...)
|
|
70
48
|
# end
|
|
71
49
|
#
|
|
72
|
-
def register_preprocessor(
|
|
73
|
-
|
|
74
|
-
name = klass.to_s
|
|
75
|
-
klass = Class.new(Processor) do
|
|
76
|
-
@name = name
|
|
77
|
-
@processor = block
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
@preprocessors[mime_type].push(klass)
|
|
50
|
+
def register_preprocessor(*args, &block)
|
|
51
|
+
register_config_processor(:preprocessors, *args, &block)
|
|
82
52
|
end
|
|
53
|
+
alias_method :register_processor, :register_preprocessor
|
|
83
54
|
|
|
84
55
|
# Registers a new Postprocessor `klass` for `mime_type`.
|
|
85
56
|
#
|
|
86
|
-
# register_postprocessor '
|
|
57
|
+
# register_postprocessor 'application/javascript', Sprockets::DirectiveProcessor
|
|
87
58
|
#
|
|
88
59
|
# A block can be passed for to create a shorthand processor.
|
|
89
60
|
#
|
|
90
|
-
# register_postprocessor '
|
|
61
|
+
# register_postprocessor 'application/javascript', :my_processor do |context, data|
|
|
91
62
|
# data.gsub(...)
|
|
92
63
|
# end
|
|
93
64
|
#
|
|
94
|
-
def register_postprocessor(
|
|
95
|
-
|
|
96
|
-
name = klass.to_s
|
|
97
|
-
klass = Class.new(Processor) do
|
|
98
|
-
@name = name
|
|
99
|
-
@processor = block
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
@postprocessors[mime_type].push(klass)
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# Deprecated alias for `unregister_preprocessor`.
|
|
107
|
-
def unregister_processor(*args)
|
|
108
|
-
unregister_preprocessor(*args)
|
|
65
|
+
def register_postprocessor(*args, &block)
|
|
66
|
+
register_config_processor(:postprocessors, *args, &block)
|
|
109
67
|
end
|
|
110
68
|
|
|
111
69
|
# Remove Preprocessor `klass` for `mime_type`.
|
|
112
70
|
#
|
|
113
71
|
# unregister_preprocessor 'text/css', Sprockets::DirectiveProcessor
|
|
114
72
|
#
|
|
115
|
-
def unregister_preprocessor(
|
|
116
|
-
|
|
117
|
-
klass = @preprocessors[mime_type].detect { |cls|
|
|
118
|
-
cls.respond_to?(:name) &&
|
|
119
|
-
cls.name == "Sprockets::Processor (#{klass})"
|
|
120
|
-
}
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
@preprocessors[mime_type].delete(klass)
|
|
73
|
+
def unregister_preprocessor(*args)
|
|
74
|
+
unregister_config_processor(:preprocessors, *args)
|
|
124
75
|
end
|
|
76
|
+
alias_method :unregister_processor, :unregister_preprocessor
|
|
125
77
|
|
|
126
78
|
# Remove Postprocessor `klass` for `mime_type`.
|
|
127
79
|
#
|
|
128
80
|
# unregister_postprocessor 'text/css', Sprockets::DirectiveProcessor
|
|
129
81
|
#
|
|
130
|
-
def unregister_postprocessor(
|
|
131
|
-
|
|
132
|
-
klass = @postprocessors[mime_type].detect { |cls|
|
|
133
|
-
cls.respond_to?(:name) &&
|
|
134
|
-
cls.name == "Sprockets::Processor (#{klass})"
|
|
135
|
-
}
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
@postprocessors[mime_type].delete(klass)
|
|
82
|
+
def unregister_postprocessor(*args)
|
|
83
|
+
unregister_config_processor(:postprocessors, *args)
|
|
139
84
|
end
|
|
140
85
|
|
|
141
|
-
# Returns an `Array` of `Processor` classes. If a `mime_type`
|
|
142
|
-
# argument is supplied, the processors registered under that
|
|
143
|
-
# extension will be returned.
|
|
144
|
-
#
|
|
145
86
|
# Bundle Processors are ran on concatenated assets rather than
|
|
146
87
|
# individual files.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
# recommended to subclass `Tilt::Template`.
|
|
150
|
-
def bundle_processors(mime_type = nil)
|
|
151
|
-
if mime_type
|
|
152
|
-
@bundle_processors[mime_type].dup
|
|
153
|
-
else
|
|
154
|
-
deep_copy_hash(@bundle_processors)
|
|
155
|
-
end
|
|
88
|
+
def bundle_processors
|
|
89
|
+
config[:bundle_processors]
|
|
156
90
|
end
|
|
157
91
|
|
|
158
92
|
# Registers a new Bundle Processor `klass` for `mime_type`.
|
|
159
93
|
#
|
|
160
|
-
# register_bundle_processor '
|
|
94
|
+
# register_bundle_processor 'application/javascript', Sprockets::DirectiveProcessor
|
|
161
95
|
#
|
|
162
96
|
# A block can be passed for to create a shorthand processor.
|
|
163
97
|
#
|
|
164
|
-
# register_bundle_processor '
|
|
98
|
+
# register_bundle_processor 'application/javascript', :my_processor do |context, data|
|
|
165
99
|
# data.gsub(...)
|
|
166
100
|
# end
|
|
167
101
|
#
|
|
168
|
-
def register_bundle_processor(
|
|
169
|
-
|
|
170
|
-
name = klass.to_s
|
|
171
|
-
klass = Class.new(Processor) do
|
|
172
|
-
@name = name
|
|
173
|
-
@processor = block
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
@bundle_processors[mime_type].push(klass)
|
|
102
|
+
def register_bundle_processor(*args, &block)
|
|
103
|
+
register_config_processor(:bundle_processors, *args, &block)
|
|
178
104
|
end
|
|
179
105
|
|
|
180
106
|
# Remove Bundle Processor `klass` for `mime_type`.
|
|
181
107
|
#
|
|
182
|
-
# unregister_bundle_processor '
|
|
108
|
+
# unregister_bundle_processor 'application/javascript', Sprockets::DirectiveProcessor
|
|
183
109
|
#
|
|
184
|
-
def unregister_bundle_processor(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
110
|
+
def unregister_bundle_processor(*args)
|
|
111
|
+
unregister_config_processor(:bundle_processors, *args)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Public: Register bundle metadata reducer function.
|
|
115
|
+
#
|
|
116
|
+
# Examples
|
|
117
|
+
#
|
|
118
|
+
# Sprockets.register_bundle_metadata_reducer 'application/javascript', :jshint_errors, [], :+
|
|
119
|
+
#
|
|
120
|
+
# Sprockets.register_bundle_metadata_reducer 'text/css', :selector_count, 0 { |total, count|
|
|
121
|
+
# total + count
|
|
122
|
+
# }
|
|
123
|
+
#
|
|
124
|
+
# mime_type - String MIME Type. Use '*/*' applies to all types.
|
|
125
|
+
# key - Symbol metadata key
|
|
126
|
+
# initial - Initial memo to pass to the reduce funciton (default: nil)
|
|
127
|
+
# block - Proc accepting the memo accumulator and current value
|
|
128
|
+
#
|
|
129
|
+
# Returns nothing.
|
|
130
|
+
def register_bundle_metadata_reducer(mime_type, key, *args, &block)
|
|
131
|
+
case args.size
|
|
132
|
+
when 0
|
|
133
|
+
reducer = block
|
|
134
|
+
when 1
|
|
135
|
+
if block_given?
|
|
136
|
+
initial = args[0]
|
|
137
|
+
reducer = block
|
|
138
|
+
else
|
|
139
|
+
initial = nil
|
|
140
|
+
reducer = args[0].to_proc
|
|
141
|
+
end
|
|
142
|
+
when 2
|
|
143
|
+
initial = args[0]
|
|
144
|
+
reducer = args[1].to_proc
|
|
145
|
+
else
|
|
146
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0..2)"
|
|
190
147
|
end
|
|
191
148
|
|
|
192
|
-
|
|
149
|
+
self.config = hash_reassoc(config, :bundle_reducers, mime_type) do |reducers|
|
|
150
|
+
reducers.merge(key => [initial, reducer])
|
|
151
|
+
end
|
|
193
152
|
end
|
|
194
153
|
|
|
154
|
+
protected
|
|
155
|
+
def resolve_processors_cache_key_uri(uri)
|
|
156
|
+
params = parse_uri_query_params(uri[11..-1])
|
|
157
|
+
params[:engine_extnames] = params[:engines] ? params[:engines].split(',') : []
|
|
158
|
+
processors = processors_for(params[:type], params[:file_type], params[:engine_extnames], params[:pipeline])
|
|
159
|
+
processors_cache_keys(processors)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def build_processors_uri(type, file_type, engine_extnames, pipeline)
|
|
163
|
+
engines = engine_extnames.join(',') if engine_extnames.any?
|
|
164
|
+
query = encode_uri_query_params(
|
|
165
|
+
type: type,
|
|
166
|
+
file_type: file_type,
|
|
167
|
+
engines: engines,
|
|
168
|
+
pipeline: pipeline
|
|
169
|
+
)
|
|
170
|
+
"processors:#{query}"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def processors_for(type, file_type, engine_extnames, pipeline)
|
|
174
|
+
pipeline ||= :default
|
|
175
|
+
config[:pipelines][pipeline.to_sym].call(self, type, file_type, engine_extnames)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def default_processors_for(type, file_type, engine_extnames)
|
|
179
|
+
bundled_processors = config[:bundle_processors][type]
|
|
180
|
+
if bundled_processors.any?
|
|
181
|
+
bundled_processors
|
|
182
|
+
else
|
|
183
|
+
self_processors_for(type, file_type, engine_extnames)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def self_processors_for(type, file_type, engine_extnames)
|
|
188
|
+
processors = []
|
|
189
|
+
|
|
190
|
+
processors.concat config[:postprocessors][type]
|
|
191
|
+
|
|
192
|
+
if type != file_type && processor = config[:transformers][file_type][type]
|
|
193
|
+
processors << processor
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
processors.concat engine_extnames.map { |ext| engines[ext] }
|
|
197
|
+
processors.concat config[:preprocessors][file_type]
|
|
198
|
+
|
|
199
|
+
if processors.any? || mime_type_charset_detecter(type)
|
|
200
|
+
processors << FileReader
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
processors
|
|
204
|
+
end
|
|
205
|
+
|
|
195
206
|
private
|
|
196
|
-
def
|
|
197
|
-
|
|
207
|
+
def register_config_processor(type, mime_type, klass, proc = nil, &block)
|
|
208
|
+
proc ||= block
|
|
209
|
+
processor = wrap_processor(klass, proc)
|
|
210
|
+
|
|
211
|
+
self.config = hash_reassoc(config, type, mime_type) do |processors|
|
|
212
|
+
processors.unshift(processor)
|
|
213
|
+
processors
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
compute_transformers!
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def unregister_config_processor(type, mime_type, klass)
|
|
220
|
+
if klass.is_a?(String) || klass.is_a?(Symbol)
|
|
221
|
+
klass = config[type][mime_type].detect do |cls|
|
|
222
|
+
cls.respond_to?(:name) && cls.name == "Sprockets::LegacyProcProcessor (#{klass})"
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
self.config = hash_reassoc(config, type, mime_type) do |processors|
|
|
227
|
+
processors.delete(klass)
|
|
228
|
+
processors
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
compute_transformers!
|
|
232
|
+
end
|
|
198
233
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
234
|
+
def wrap_processor(klass, proc)
|
|
235
|
+
if !proc
|
|
236
|
+
if klass.respond_to?(:call)
|
|
237
|
+
klass
|
|
238
|
+
else
|
|
239
|
+
LegacyTiltProcessor.new(klass)
|
|
202
240
|
end
|
|
241
|
+
elsif proc.respond_to?(:arity) && proc.arity == 2
|
|
242
|
+
LegacyProcProcessor.new(klass.to_s, proc)
|
|
243
|
+
else
|
|
244
|
+
proc
|
|
203
245
|
end
|
|
204
246
|
end
|
|
205
247
|
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
# Functional utilities for dealing with Processor functions.
|
|
3
|
+
#
|
|
4
|
+
# A Processor is a general function that my modify or transform an asset as
|
|
5
|
+
# part of the pipeline. CoffeeScript to JavaScript conversion, Minification
|
|
6
|
+
# or Concatenation are all implemented as seperate Processor steps.
|
|
7
|
+
#
|
|
8
|
+
# Processors maybe any object that responds to call. So procs or a class that
|
|
9
|
+
# defines a self.call method.
|
|
10
|
+
#
|
|
11
|
+
# For ergonomics, processors may return a number of shorthand values.
|
|
12
|
+
# Unfortunately, this means that processors can not compose via ordinary
|
|
13
|
+
# function composition. The composition helpers here can help.
|
|
14
|
+
module ProcessorUtils
|
|
15
|
+
extend self
|
|
16
|
+
|
|
17
|
+
# Public: Compose processors in right to left order.
|
|
18
|
+
#
|
|
19
|
+
# processors - Array of processors callables
|
|
20
|
+
#
|
|
21
|
+
# Returns a composed Proc.
|
|
22
|
+
def compose_processors(*processors)
|
|
23
|
+
context = self
|
|
24
|
+
|
|
25
|
+
if processors.length == 1
|
|
26
|
+
obj = method(:call_processor).to_proc.curry[processors.first]
|
|
27
|
+
else
|
|
28
|
+
obj = method(:call_processors).to_proc.curry[processors]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
metaclass = (class << obj; self; end)
|
|
32
|
+
metaclass.send(:define_method, :cache_key) do
|
|
33
|
+
context.processors_cache_keys(processors)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
obj
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Public: Invoke list of processors in right to left order.
|
|
40
|
+
#
|
|
41
|
+
# The right to left order processing mirrors standard function composition.
|
|
42
|
+
# Think about:
|
|
43
|
+
#
|
|
44
|
+
# bundle.call(uglify.call(coffee.call(input)))
|
|
45
|
+
#
|
|
46
|
+
# processors - Array of processor callables
|
|
47
|
+
# input - Hash of input data to pass to each processor
|
|
48
|
+
#
|
|
49
|
+
# Returns a Hash with :data and other processor metadata key/values.
|
|
50
|
+
def call_processors(processors, input)
|
|
51
|
+
data = input[:data] || ""
|
|
52
|
+
metadata = (input[:metadata] || {}).dup
|
|
53
|
+
|
|
54
|
+
processors.reverse_each do |processor|
|
|
55
|
+
result = call_processor(processor, input.merge(data: data, metadata: metadata))
|
|
56
|
+
data = result.delete(:data)
|
|
57
|
+
metadata.merge!(result)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
metadata.merge(data: data)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Public: Invoke processor.
|
|
64
|
+
#
|
|
65
|
+
# processor - Processor callables
|
|
66
|
+
# input - Hash of input data to pass to processor
|
|
67
|
+
#
|
|
68
|
+
# Returns a Hash with :data and other processor metadata key/values.
|
|
69
|
+
def call_processor(processor, input)
|
|
70
|
+
metadata = (input[:metadata] || {}).dup
|
|
71
|
+
metadata[:data] = input[:data]
|
|
72
|
+
|
|
73
|
+
case result = processor.call({data: "", metadata: {}}.merge(input))
|
|
74
|
+
when NilClass
|
|
75
|
+
metadata
|
|
76
|
+
when Hash
|
|
77
|
+
metadata.merge(result)
|
|
78
|
+
when String
|
|
79
|
+
metadata.merge(data: result)
|
|
80
|
+
else
|
|
81
|
+
raise TypeError, "invalid processor return type: #{result.class}"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Internal: Get processor defined cached key.
|
|
86
|
+
#
|
|
87
|
+
# processor - Processor function
|
|
88
|
+
#
|
|
89
|
+
# Returns JSON serializable key or nil.
|
|
90
|
+
def processor_cache_key(processor)
|
|
91
|
+
processor.cache_key if processor.respond_to?(:cache_key)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Internal: Get combined cache keys for set of processors.
|
|
95
|
+
#
|
|
96
|
+
# processors - Array of processor functions
|
|
97
|
+
#
|
|
98
|
+
# Returns Array of JSON serializable keys.
|
|
99
|
+
def processors_cache_keys(processors)
|
|
100
|
+
processors.map { |processor| processor_cache_key(processor) }
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
require 'sprockets/http_utils'
|
|
3
|
+
require 'sprockets/path_dependency_utils'
|
|
4
|
+
require 'sprockets/uri_utils'
|
|
5
|
+
|
|
6
|
+
module Sprockets
|
|
7
|
+
module Resolve
|
|
8
|
+
include HTTPUtils, PathDependencyUtils, URIUtils
|
|
9
|
+
|
|
10
|
+
# Public: Find Asset URI for given a logical path by searching the
|
|
11
|
+
# environment's load paths.
|
|
12
|
+
#
|
|
13
|
+
# resolve("application.js")
|
|
14
|
+
# # => "file:///path/to/app/javascripts/application.js?type=application/javascript"
|
|
15
|
+
#
|
|
16
|
+
# An accept content type can be given if the logical path doesn't have a
|
|
17
|
+
# format extension.
|
|
18
|
+
#
|
|
19
|
+
# resolve("application", accept: "application/javascript")
|
|
20
|
+
# # => "file:///path/to/app/javascripts/application.coffee?type=application/javascript"
|
|
21
|
+
#
|
|
22
|
+
# The String Asset URI is returned or nil if no results are found.
|
|
23
|
+
def resolve(path, options = {})
|
|
24
|
+
path = path.to_s
|
|
25
|
+
paths = options[:load_paths] || config[:paths]
|
|
26
|
+
accept = options[:accept]
|
|
27
|
+
|
|
28
|
+
if valid_asset_uri?(path)
|
|
29
|
+
uri, deps = resolve_asset_uri(path)
|
|
30
|
+
elsif absolute_path?(path)
|
|
31
|
+
filename, type, deps = resolve_absolute_path(paths, path, accept)
|
|
32
|
+
elsif relative_path?(path)
|
|
33
|
+
filename, type, pipeline, deps = resolve_relative_path(paths, path, options[:base_path], accept)
|
|
34
|
+
else
|
|
35
|
+
filename, type, pipeline, deps = resolve_logical_path(paths, path, accept)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
if filename
|
|
39
|
+
params = {}
|
|
40
|
+
params[:type] = type if type
|
|
41
|
+
params[:pipeline] = pipeline if pipeline
|
|
42
|
+
params[:pipeline] = options[:pipeline] if options[:pipeline]
|
|
43
|
+
uri = build_asset_uri(filename, params)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
return uri, deps
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Public: Same as resolve() but raises a FileNotFound exception instead of
|
|
50
|
+
# nil if no assets are found.
|
|
51
|
+
def resolve!(path, options = {})
|
|
52
|
+
uri, deps = resolve(path, options.merge(compat: false))
|
|
53
|
+
|
|
54
|
+
unless uri
|
|
55
|
+
message = "couldn't find file '#{path}'"
|
|
56
|
+
message << " with type '#{options[:accept]}'" if options[:accept]
|
|
57
|
+
raise FileNotFound, message
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
return uri, deps
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
protected
|
|
64
|
+
def resolve_asset_uri(uri)
|
|
65
|
+
filename, _ = parse_asset_uri(uri)
|
|
66
|
+
return uri, Set.new([build_file_digest_uri(filename)])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def resolve_absolute_path(paths, filename, accept)
|
|
70
|
+
deps = Set.new
|
|
71
|
+
filename = File.expand_path(filename)
|
|
72
|
+
|
|
73
|
+
# Ensure path is under load paths
|
|
74
|
+
return nil, nil, deps unless paths_split(paths, filename)
|
|
75
|
+
|
|
76
|
+
_, mime_type, _, _ = parse_path_extnames(filename)
|
|
77
|
+
type = resolve_transform_type(mime_type, accept)
|
|
78
|
+
return nil, nil, deps if accept && !type
|
|
79
|
+
|
|
80
|
+
return nil, nil, deps unless file?(filename)
|
|
81
|
+
|
|
82
|
+
deps << build_file_digest_uri(filename)
|
|
83
|
+
return filename, type, deps
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def resolve_relative_path(paths, path, dirname, accept)
|
|
87
|
+
filename = File.expand_path(path, dirname)
|
|
88
|
+
load_path, _ = paths_split(paths, dirname)
|
|
89
|
+
if load_path && logical_path = split_subpath(load_path, filename)
|
|
90
|
+
resolve_logical_path([load_path], logical_path, accept)
|
|
91
|
+
else
|
|
92
|
+
return nil, nil, Set.new
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def resolve_logical_path(paths, logical_path, accept)
|
|
97
|
+
logical_name, mime_type, _, pipeline = parse_path_extnames(logical_path)
|
|
98
|
+
parsed_accept = parse_accept_options(mime_type, accept)
|
|
99
|
+
transformed_accepts = expand_transform_accepts(parsed_accept)
|
|
100
|
+
filename, mime_type, deps = resolve_under_paths(paths, logical_name, transformed_accepts)
|
|
101
|
+
|
|
102
|
+
if filename
|
|
103
|
+
deps << build_file_digest_uri(filename)
|
|
104
|
+
type = resolve_transform_type(mime_type, parsed_accept)
|
|
105
|
+
return filename, type, pipeline, deps
|
|
106
|
+
else
|
|
107
|
+
return nil, nil, nil, deps
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def resolve_under_paths(paths, logical_name, accepts)
|
|
112
|
+
all_deps = Set.new
|
|
113
|
+
return nil, nil, all_deps if accepts.empty?
|
|
114
|
+
|
|
115
|
+
logical_basename = File.basename(logical_name)
|
|
116
|
+
paths.each do |load_path|
|
|
117
|
+
candidates, deps = path_matches(load_path, logical_name, logical_basename)
|
|
118
|
+
all_deps.merge(deps)
|
|
119
|
+
candidate = find_best_q_match(accepts, candidates) do |c, matcher|
|
|
120
|
+
match_mime_type?(c[1] || "application/octet-stream", matcher)
|
|
121
|
+
end
|
|
122
|
+
return candidate + [all_deps] if candidate
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
return nil, nil, all_deps
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def parse_accept_options(mime_type, types)
|
|
129
|
+
accepts = []
|
|
130
|
+
accepts += parse_q_values(types) if types
|
|
131
|
+
|
|
132
|
+
if mime_type
|
|
133
|
+
if accepts.empty? || accepts.any? { |accept, _| match_mime_type?(mime_type, accept) }
|
|
134
|
+
accepts = [[mime_type, 1.0]]
|
|
135
|
+
else
|
|
136
|
+
return []
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
if accepts.empty?
|
|
141
|
+
accepts << ['*/*', 1.0]
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
accepts
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def normalize_logical_path(path)
|
|
148
|
+
dirname, basename = File.split(path)
|
|
149
|
+
path = dirname if basename == 'index'
|
|
150
|
+
path
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def path_matches(load_path, logical_name, logical_basename)
|
|
154
|
+
dirname = File.dirname(File.join(load_path, logical_name))
|
|
155
|
+
candidates = dirname_matches(dirname, logical_basename)
|
|
156
|
+
deps = file_digest_dependency_set(dirname)
|
|
157
|
+
|
|
158
|
+
result = resolve_alternates(load_path, logical_name)
|
|
159
|
+
result[0].each do |fn|
|
|
160
|
+
candidates << [fn, parse_path_extnames(fn)[1]]
|
|
161
|
+
end
|
|
162
|
+
deps.merge(result[1])
|
|
163
|
+
|
|
164
|
+
dirname = File.join(load_path, logical_name)
|
|
165
|
+
if directory? dirname
|
|
166
|
+
result = dirname_matches(dirname, "index")
|
|
167
|
+
candidates.concat(result)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
deps.merge(file_digest_dependency_set(dirname))
|
|
171
|
+
|
|
172
|
+
return candidates.select { |fn, _| file?(fn) }, deps
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def dirname_matches(dirname, basename)
|
|
176
|
+
candidates = []
|
|
177
|
+
entries = self.entries(dirname)
|
|
178
|
+
entries.each do |entry|
|
|
179
|
+
name, type, _, _ = parse_path_extnames(entry)
|
|
180
|
+
if basename == name
|
|
181
|
+
candidates << [File.join(dirname, entry), type]
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
candidates
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def resolve_alternates(load_path, logical_name)
|
|
188
|
+
return [], Set.new
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Internal: Returns the name, mime type and `Array` of engine extensions.
|
|
192
|
+
#
|
|
193
|
+
# "foo.js.coffee.erb"
|
|
194
|
+
# # => ["foo", "application/javascript", [".coffee", ".erb"]]
|
|
195
|
+
#
|
|
196
|
+
def parse_path_extnames(path)
|
|
197
|
+
engines = []
|
|
198
|
+
extname, value = match_path_extname(path, extname_map)
|
|
199
|
+
|
|
200
|
+
if extname
|
|
201
|
+
path = path.chomp(extname)
|
|
202
|
+
type, engines, pipeline = value.values_at(:type, :engines, :pipeline)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
return path, type, engines, pipeline
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|