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
|
@@ -1,37 +1,41 @@
|
|
|
1
|
+
require 'sprockets/utils'
|
|
2
|
+
|
|
1
3
|
module Sprockets
|
|
2
4
|
# `Compressing` is an internal mixin whose public methods are exposed on
|
|
3
|
-
# the `Environment` and `
|
|
5
|
+
# the `Environment` and `CachedEnvironment` classes.
|
|
4
6
|
module Compressing
|
|
7
|
+
include Utils
|
|
8
|
+
|
|
5
9
|
def compressors
|
|
6
|
-
|
|
10
|
+
config[:compressors]
|
|
7
11
|
end
|
|
8
12
|
|
|
9
13
|
def register_compressor(mime_type, sym, klass)
|
|
10
|
-
|
|
14
|
+
self.config = hash_reassoc(config, :compressors, mime_type) do |compressors|
|
|
15
|
+
compressors[sym] = klass
|
|
16
|
+
compressors
|
|
17
|
+
end
|
|
11
18
|
end
|
|
12
19
|
|
|
13
20
|
# Return CSS compressor or nil if none is set
|
|
14
21
|
def css_compressor
|
|
15
|
-
|
|
22
|
+
if defined? @css_compressor
|
|
23
|
+
@css_compressor
|
|
24
|
+
end
|
|
16
25
|
end
|
|
17
26
|
|
|
18
27
|
# Assign a compressor to run on `text/css` assets.
|
|
19
28
|
#
|
|
20
29
|
# The compressor object must respond to `compress`.
|
|
21
30
|
def css_compressor=(compressor)
|
|
22
|
-
unregister_bundle_processor 'text/css', css_compressor if css_compressor
|
|
31
|
+
unregister_bundle_processor 'text/css', @css_compressor if defined? @css_compressor
|
|
23
32
|
@css_compressor = nil
|
|
24
33
|
return unless compressor
|
|
25
34
|
|
|
26
35
|
if compressor.is_a?(Symbol)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if compressor.respond_to?(:compress)
|
|
31
|
-
klass = Class.new(Processor) do
|
|
32
|
-
@name = "css_compressor"
|
|
33
|
-
@processor = proc { |context, data| compressor.compress(data) }
|
|
34
|
-
end
|
|
36
|
+
@css_compressor = klass = config[:compressors]['text/css'][compressor] || raise(Error, "unknown compressor: #{compressor}")
|
|
37
|
+
elsif compressor.respond_to?(:compress)
|
|
38
|
+
klass = LegacyProcProcessor.new(:css_compressor, proc { |context, data| compressor.compress(data) })
|
|
35
39
|
@css_compressor = :css_compressor
|
|
36
40
|
else
|
|
37
41
|
@css_compressor = klass = compressor
|
|
@@ -42,26 +46,23 @@ module Sprockets
|
|
|
42
46
|
|
|
43
47
|
# Return JS compressor or nil if none is set
|
|
44
48
|
def js_compressor
|
|
45
|
-
|
|
49
|
+
if defined? @js_compressor
|
|
50
|
+
@js_compressor
|
|
51
|
+
end
|
|
46
52
|
end
|
|
47
53
|
|
|
48
54
|
# Assign a compressor to run on `application/javascript` assets.
|
|
49
55
|
#
|
|
50
56
|
# The compressor object must respond to `compress`.
|
|
51
57
|
def js_compressor=(compressor)
|
|
52
|
-
unregister_bundle_processor 'application/javascript', js_compressor if js_compressor
|
|
58
|
+
unregister_bundle_processor 'application/javascript', @js_compressor if defined? @js_compressor
|
|
53
59
|
@js_compressor = nil
|
|
54
60
|
return unless compressor
|
|
55
61
|
|
|
56
62
|
if compressor.is_a?(Symbol)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if compressor.respond_to?(:compress)
|
|
61
|
-
klass = Class.new(Processor) do
|
|
62
|
-
@name = "js_compressor"
|
|
63
|
-
@processor = proc { |context, data| compressor.compress(data) }
|
|
64
|
-
end
|
|
63
|
+
@js_compressor = klass = config[:compressors]['application/javascript'][compressor] || raise(Error, "unknown compressor: #{compressor}")
|
|
64
|
+
elsif compressor.respond_to?(:compress)
|
|
65
|
+
klass = LegacyProcProcessor.new(:js_compressor, proc { |context, data| compressor.compress(data) })
|
|
65
66
|
@js_compressor = :js_compressor
|
|
66
67
|
else
|
|
67
68
|
@js_compressor = klass = compressor
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require 'sprockets/compressing'
|
|
2
|
+
require 'sprockets/dependencies'
|
|
3
|
+
require 'sprockets/engines'
|
|
4
|
+
require 'sprockets/mime'
|
|
5
|
+
require 'sprockets/paths'
|
|
6
|
+
require 'sprockets/processing'
|
|
7
|
+
require 'sprockets/transformers'
|
|
8
|
+
require 'sprockets/utils'
|
|
9
|
+
|
|
10
|
+
module Sprockets
|
|
11
|
+
module Configuration
|
|
12
|
+
include Paths, Mime, Engines, Transformers, Processing, Compressing, Dependencies, Utils
|
|
13
|
+
|
|
14
|
+
def initialize_configuration(parent)
|
|
15
|
+
@config = parent.config
|
|
16
|
+
@computed_config = parent.computed_config
|
|
17
|
+
@logger = parent.logger
|
|
18
|
+
@context_class = Class.new(parent.context_class)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :config
|
|
22
|
+
|
|
23
|
+
attr_accessor :computed_config
|
|
24
|
+
|
|
25
|
+
def config=(config)
|
|
26
|
+
raise TypeError, "can't assign mutable config" unless config.frozen?
|
|
27
|
+
@config = config
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Get and set `Logger` instance.
|
|
31
|
+
attr_accessor :logger
|
|
32
|
+
|
|
33
|
+
# The `Environment#version` is a custom value used for manually
|
|
34
|
+
# expiring all asset caches.
|
|
35
|
+
#
|
|
36
|
+
# Sprockets is able to track most file and directory changes and
|
|
37
|
+
# will take care of expiring the cache for you. However, its
|
|
38
|
+
# impossible to know when any custom helpers change that you mix
|
|
39
|
+
# into the `Context`.
|
|
40
|
+
#
|
|
41
|
+
# It would be wise to increment this value anytime you make a
|
|
42
|
+
# configuration change to the `Environment` object.
|
|
43
|
+
def version
|
|
44
|
+
config[:version]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Assign an environment version.
|
|
48
|
+
#
|
|
49
|
+
# environment.version = '2.0'
|
|
50
|
+
#
|
|
51
|
+
def version=(version)
|
|
52
|
+
self.config = hash_reassoc(config, :version) { version.dup }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Public: Returns a `Digest` implementation class.
|
|
56
|
+
#
|
|
57
|
+
# Defaults to `Digest::SHA256`.
|
|
58
|
+
def digest_class
|
|
59
|
+
config[:digest_class]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Deprecated: Assign a `Digest` implementation class. This maybe any Ruby
|
|
63
|
+
# `Digest::` implementation such as `Digest::SHA256` or
|
|
64
|
+
# `Digest::MD5`.
|
|
65
|
+
#
|
|
66
|
+
# environment.digest_class = Digest::MD5
|
|
67
|
+
#
|
|
68
|
+
def digest_class=(klass)
|
|
69
|
+
self.config = config.merge(digest_class: klass).freeze
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Deprecated: Get `Context` class.
|
|
73
|
+
#
|
|
74
|
+
# This class maybe mutated and mixed in with custom helpers.
|
|
75
|
+
#
|
|
76
|
+
# environment.context_class.instance_eval do
|
|
77
|
+
# include MyHelpers
|
|
78
|
+
# def asset_url; end
|
|
79
|
+
# end
|
|
80
|
+
#
|
|
81
|
+
attr_reader :context_class
|
|
82
|
+
end
|
|
83
|
+
end
|
data/lib/sprockets/context.rb
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
require 'base64'
|
|
2
|
-
require 'rack/utils'
|
|
3
|
-
require 'sprockets/errors'
|
|
4
|
-
require 'sprockets/utils'
|
|
5
1
|
require 'pathname'
|
|
2
|
+
require 'rack/utils'
|
|
6
3
|
require 'set'
|
|
4
|
+
require 'sprockets/errors'
|
|
7
5
|
|
|
8
6
|
module Sprockets
|
|
9
|
-
# `Context` provides helper methods to all
|
|
10
|
-
# are typically accessed by ERB templates. You can mix in custom
|
|
11
|
-
#
|
|
12
|
-
#
|
|
7
|
+
# Deprecated: `Context` provides helper methods to all processors.
|
|
8
|
+
# They are typically accessed by ERB templates. You can mix in custom helpers
|
|
9
|
+
# by injecting them into `Environment#context_class`. Do not mix them into
|
|
10
|
+
# `Context` directly.
|
|
13
11
|
#
|
|
14
12
|
# environment.context_class.class_eval do
|
|
15
13
|
# include MyHelper
|
|
@@ -21,92 +19,86 @@ module Sprockets
|
|
|
21
19
|
# The `Context` also collects dependencies declared by
|
|
22
20
|
# assets. See `DirectiveProcessor` for an example of this.
|
|
23
21
|
class Context
|
|
24
|
-
attr_reader :environment, :pathname
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
attr_reader :environment, :filename, :pathname
|
|
23
|
+
|
|
24
|
+
# Deprecated
|
|
25
|
+
attr_accessor :__LINE__
|
|
28
26
|
|
|
29
|
-
def initialize(
|
|
30
|
-
@environment = environment
|
|
31
|
-
@
|
|
32
|
-
@
|
|
33
|
-
@
|
|
27
|
+
def initialize(input)
|
|
28
|
+
@environment = input[:environment]
|
|
29
|
+
@metadata = input[:metadata]
|
|
30
|
+
@load_path = input[:load_path]
|
|
31
|
+
@logical_path = input[:name]
|
|
32
|
+
@filename = input[:filename]
|
|
33
|
+
@dirname = File.dirname(@filename)
|
|
34
|
+
@pathname = Pathname.new(@filename)
|
|
35
|
+
@content_type = input[:content_type]
|
|
34
36
|
|
|
35
|
-
@
|
|
36
|
-
@
|
|
37
|
-
@
|
|
38
|
-
@
|
|
37
|
+
@required = Set.new(@metadata[:required])
|
|
38
|
+
@stubbed = Set.new(@metadata[:stubbed])
|
|
39
|
+
@links = Set.new(@metadata[:links])
|
|
40
|
+
@dependencies = Set.new(input[:metadata][:dependencies])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def metadata
|
|
44
|
+
{ required: @required,
|
|
45
|
+
stubbed: @stubbed,
|
|
46
|
+
links: @links,
|
|
47
|
+
dependencies: @dependencies }
|
|
39
48
|
end
|
|
40
49
|
|
|
41
50
|
# Returns the environment path that contains the file.
|
|
42
51
|
#
|
|
43
52
|
# If `app/javascripts` and `app/stylesheets` are in your path, and
|
|
44
|
-
# current file is `app/javascripts/foo/bar.js`, `
|
|
53
|
+
# current file is `app/javascripts/foo/bar.js`, `load_path` would
|
|
45
54
|
# return `app/javascripts`.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
end
|
|
55
|
+
attr_reader :load_path
|
|
56
|
+
alias_method :root_path, :load_path
|
|
49
57
|
|
|
50
58
|
# Returns logical path without any file extensions.
|
|
51
59
|
#
|
|
52
60
|
# 'app/javascripts/application.js'
|
|
53
61
|
# # => 'application'
|
|
54
62
|
#
|
|
55
|
-
|
|
56
|
-
@logical_path.chomp(File.extname(@logical_path))
|
|
57
|
-
end
|
|
63
|
+
attr_reader :logical_path
|
|
58
64
|
|
|
59
65
|
# Returns content type of file
|
|
60
66
|
#
|
|
61
67
|
# 'application/javascript'
|
|
62
68
|
# 'text/css'
|
|
63
69
|
#
|
|
64
|
-
|
|
65
|
-
environment.content_type_of(pathname)
|
|
66
|
-
end
|
|
70
|
+
attr_reader :content_type
|
|
67
71
|
|
|
68
|
-
# Given a logical path, `resolve` will find and return
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
# search.
|
|
72
|
+
# Public: Given a logical path, `resolve` will find and return an Asset URI.
|
|
73
|
+
# Relative paths will also be resolved. An accept type maybe given to
|
|
74
|
+
# restrict the search.
|
|
72
75
|
#
|
|
73
76
|
# resolve("foo.js")
|
|
74
|
-
# # => "
|
|
77
|
+
# # => "file:///path/to/app/javascripts/foo.js?type=application/javascript"
|
|
75
78
|
#
|
|
76
79
|
# resolve("./bar.js")
|
|
77
|
-
# # => "
|
|
80
|
+
# # => "file:///path/to/app/javascripts/bar.js?type=application/javascript"
|
|
78
81
|
#
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
elsif content_type = options[:content_type]
|
|
91
|
-
content_type = self.content_type if content_type == :self
|
|
92
|
-
|
|
93
|
-
if attributes.format_extension
|
|
94
|
-
if content_type != attributes.content_type
|
|
95
|
-
raise ContentTypeMismatch, "#{path} is " +
|
|
96
|
-
"'#{attributes.content_type}', not '#{content_type}'"
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
resolve(path) do |candidate|
|
|
101
|
-
if self.content_type == environment.content_type_of(candidate)
|
|
102
|
-
return candidate
|
|
103
|
-
end
|
|
104
|
-
end
|
|
82
|
+
# path - String logical or absolute path
|
|
83
|
+
# options
|
|
84
|
+
# accept - String content accept type
|
|
85
|
+
#
|
|
86
|
+
# Returns an Asset URI String.
|
|
87
|
+
def resolve(path, options = {})
|
|
88
|
+
uri, deps = environment.resolve!(path, options.merge(base_path: @dirname))
|
|
89
|
+
@dependencies.merge(deps)
|
|
90
|
+
uri
|
|
91
|
+
end
|
|
105
92
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
93
|
+
# Public: Load Asset by AssetURI and track it as a dependency.
|
|
94
|
+
#
|
|
95
|
+
# uri - AssetURI
|
|
96
|
+
#
|
|
97
|
+
# Returns Asset.
|
|
98
|
+
def load(uri)
|
|
99
|
+
asset = environment.load(uri)
|
|
100
|
+
@dependencies.merge(asset.metadata[:dependencies])
|
|
101
|
+
asset
|
|
110
102
|
end
|
|
111
103
|
|
|
112
104
|
# `depend_on` allows you to state a dependency on a file without
|
|
@@ -116,7 +108,11 @@ module Sprockets
|
|
|
116
108
|
# the dependency file with invalidate the cache of the
|
|
117
109
|
# source file.
|
|
118
110
|
def depend_on(path)
|
|
119
|
-
|
|
111
|
+
if environment.absolute_path?(path) && environment.directory?(path)
|
|
112
|
+
@dependencies << environment.build_file_digest_uri(path)
|
|
113
|
+
else
|
|
114
|
+
resolve(path, compat: false)
|
|
115
|
+
end
|
|
120
116
|
nil
|
|
121
117
|
end
|
|
122
118
|
|
|
@@ -128,9 +124,7 @@ module Sprockets
|
|
|
128
124
|
# file. Unlike `depend_on`, this will include recursively include
|
|
129
125
|
# the target asset's dependencies.
|
|
130
126
|
def depend_on_asset(path)
|
|
131
|
-
|
|
132
|
-
@_dependency_assets << filename
|
|
133
|
-
nil
|
|
127
|
+
load(resolve(path, compat: false))
|
|
134
128
|
end
|
|
135
129
|
|
|
136
130
|
# `require_asset` declares `path` as a dependency of the file. The
|
|
@@ -143,9 +137,7 @@ module Sprockets
|
|
|
143
137
|
# <%= require_asset "#{framework}.js" %>
|
|
144
138
|
#
|
|
145
139
|
def require_asset(path)
|
|
146
|
-
|
|
147
|
-
depend_on_asset(pathname)
|
|
148
|
-
@_required_paths << pathname.to_s
|
|
140
|
+
@required << resolve(path, accept: @content_type, pipeline: :self, compat: false)
|
|
149
141
|
nil
|
|
150
142
|
end
|
|
151
143
|
|
|
@@ -153,55 +145,19 @@ module Sprockets
|
|
|
153
145
|
# `path` must be an asset which may or may not already be included
|
|
154
146
|
# in the bundle.
|
|
155
147
|
def stub_asset(path)
|
|
156
|
-
@
|
|
148
|
+
@stubbed << resolve(path, accept: @content_type, pipeline: :self, compat: false)
|
|
157
149
|
nil
|
|
158
150
|
end
|
|
159
151
|
|
|
160
|
-
#
|
|
161
|
-
#
|
|
162
|
-
|
|
163
|
-
pathname = resolve(path)
|
|
164
|
-
content_type = environment.content_type_of(pathname)
|
|
165
|
-
stat = environment.stat(path)
|
|
166
|
-
return false unless stat && stat.file?
|
|
167
|
-
self.content_type.nil? || self.content_type == content_type
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
# Reads `path` and runs processors on the file.
|
|
171
|
-
#
|
|
172
|
-
# This allows you to capture the result of an asset and include it
|
|
173
|
-
# directly in another.
|
|
174
|
-
#
|
|
175
|
-
# <%= evaluate "bar.js" %>
|
|
152
|
+
# `link_asset` declares an external dependency on an asset without directly
|
|
153
|
+
# including it. The target asset is returned from this function making it
|
|
154
|
+
# easy to construct a link to it.
|
|
176
155
|
#
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if options[:data]
|
|
183
|
-
result = options[:data]
|
|
184
|
-
else
|
|
185
|
-
if environment.respond_to?(:default_external_encoding)
|
|
186
|
-
mime_type = environment.mime_types(pathname.extname)
|
|
187
|
-
encoding = environment.encoding_for_mime_type(mime_type)
|
|
188
|
-
result = Sprockets::Utils.read_unicode(pathname, encoding)
|
|
189
|
-
else
|
|
190
|
-
result = Sprockets::Utils.read_unicode(pathname)
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
processors.each do |processor|
|
|
195
|
-
begin
|
|
196
|
-
template = processor.new(pathname.to_s) { result }
|
|
197
|
-
result = template.render(self, {})
|
|
198
|
-
rescue Exception => e
|
|
199
|
-
annotate_exception! e
|
|
200
|
-
raise
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
result
|
|
156
|
+
# Returns an Asset or nil.
|
|
157
|
+
def link_asset(path)
|
|
158
|
+
asset = depend_on_asset(path)
|
|
159
|
+
@links << asset.uri
|
|
160
|
+
asset
|
|
205
161
|
end
|
|
206
162
|
|
|
207
163
|
# Returns a Base64-encoded `data:` URI with the contents of the
|
|
@@ -215,10 +171,9 @@ module Sprockets
|
|
|
215
171
|
# $('<img>').attr('src', '<%= asset_data_uri 'avatar.jpg' %>')
|
|
216
172
|
#
|
|
217
173
|
def asset_data_uri(path)
|
|
218
|
-
depend_on_asset(path)
|
|
219
|
-
|
|
220
|
-
base64
|
|
221
|
-
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
|
|
174
|
+
asset = depend_on_asset(path)
|
|
175
|
+
data = EncodingUtils.base64(asset.source)
|
|
176
|
+
"data:#{asset.content_type};base64,#{Rack::Utils.escape(data)}"
|
|
222
177
|
end
|
|
223
178
|
|
|
224
179
|
# Expands logical path to full url to asset.
|
|
@@ -243,47 +198,32 @@ Extend your environment context with a custom method.
|
|
|
243
198
|
|
|
244
199
|
# Expand logical image asset path.
|
|
245
200
|
def image_path(path)
|
|
246
|
-
asset_path(path, :
|
|
201
|
+
asset_path(path, type: :image)
|
|
247
202
|
end
|
|
248
203
|
|
|
249
204
|
# Expand logical video asset path.
|
|
250
205
|
def video_path(path)
|
|
251
|
-
asset_path(path, :
|
|
206
|
+
asset_path(path, type: :video)
|
|
252
207
|
end
|
|
253
208
|
|
|
254
209
|
# Expand logical audio asset path.
|
|
255
210
|
def audio_path(path)
|
|
256
|
-
asset_path(path, :
|
|
211
|
+
asset_path(path, type: :audio)
|
|
257
212
|
end
|
|
258
213
|
|
|
259
214
|
# Expand logical font asset path.
|
|
260
215
|
def font_path(path)
|
|
261
|
-
asset_path(path, :
|
|
216
|
+
asset_path(path, type: :font)
|
|
262
217
|
end
|
|
263
218
|
|
|
264
219
|
# Expand logical javascript asset path.
|
|
265
220
|
def javascript_path(path)
|
|
266
|
-
asset_path(path, :
|
|
221
|
+
asset_path(path, type: :javascript)
|
|
267
222
|
end
|
|
268
223
|
|
|
269
224
|
# Expand logical stylesheet asset path.
|
|
270
225
|
def stylesheet_path(path)
|
|
271
|
-
asset_path(path, :
|
|
226
|
+
asset_path(path, type: :stylesheet)
|
|
272
227
|
end
|
|
273
|
-
|
|
274
|
-
private
|
|
275
|
-
# Annotates exception backtrace with the original template that
|
|
276
|
-
# the exception was raised in.
|
|
277
|
-
def annotate_exception!(exception)
|
|
278
|
-
location = pathname.to_s
|
|
279
|
-
location << ":#{@__LINE__}" if @__LINE__
|
|
280
|
-
|
|
281
|
-
exception.extend(Sprockets::EngineError)
|
|
282
|
-
exception.sprockets_annotation = " (in #{location})"
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
def logger
|
|
286
|
-
environment.logger
|
|
287
|
-
end
|
|
288
228
|
end
|
|
289
229
|
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
require 'sprockets/digest_utils'
|
|
2
|
+
require 'sprockets/path_digest_utils'
|
|
3
|
+
require 'sprockets/uri_utils'
|
|
4
|
+
|
|
5
|
+
module Sprockets
|
|
6
|
+
# `Dependencies` is an internal mixin whose public methods are exposed on the
|
|
7
|
+
# `Environment` and `CachedEnvironment` classes.
|
|
8
|
+
module Dependencies
|
|
9
|
+
include DigestUtils, PathDigestUtils, URIUtils
|
|
10
|
+
|
|
11
|
+
# Public: Mapping dependency schemes to resolver functions.
|
|
12
|
+
#
|
|
13
|
+
# key - String scheme
|
|
14
|
+
# value - Proc.call(Environment, String)
|
|
15
|
+
#
|
|
16
|
+
# Returns Hash.
|
|
17
|
+
def dependency_resolvers
|
|
18
|
+
config[:dependency_resolvers]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Public: Default set of dependency URIs for assets.
|
|
22
|
+
#
|
|
23
|
+
# Returns Set of String URIs.
|
|
24
|
+
def dependencies
|
|
25
|
+
config[:dependencies]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Public: Register new dependency URI resolver.
|
|
29
|
+
#
|
|
30
|
+
# scheme - String scheme
|
|
31
|
+
# block -
|
|
32
|
+
# environment - Environment
|
|
33
|
+
# uri - String dependency URI
|
|
34
|
+
#
|
|
35
|
+
# Returns nothing.
|
|
36
|
+
def register_dependency_resolver(scheme, &block)
|
|
37
|
+
self.config = hash_reassoc(config, :dependency_resolvers) do |hash|
|
|
38
|
+
hash.merge(scheme => block)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Public: Add environmental dependency inheirted by all assets.
|
|
43
|
+
#
|
|
44
|
+
# uri - String dependency URI
|
|
45
|
+
#
|
|
46
|
+
# Returns nothing.
|
|
47
|
+
def add_dependency(uri)
|
|
48
|
+
self.config = hash_reassoc(config, :dependencies) do |set|
|
|
49
|
+
set + Set.new([uri])
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
alias_method :depend_on, :add_dependency
|
|
53
|
+
|
|
54
|
+
# Internal: Resolve set of dependency URIs.
|
|
55
|
+
#
|
|
56
|
+
# Returns Array of resolved Objects.
|
|
57
|
+
def resolve_dependencies(uris)
|
|
58
|
+
uris.map { |uri| resolve_dependency(uri) }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Internal: Resolve dependency URIs.
|
|
62
|
+
#
|
|
63
|
+
# Returns resolved Object.
|
|
64
|
+
def resolve_dependency(str)
|
|
65
|
+
scheme = str[/([^:]+)/, 1]
|
|
66
|
+
if resolver = config[:dependency_resolvers][scheme]
|
|
67
|
+
resolver.call(self, str)
|
|
68
|
+
else
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|