sprockets-helpers 1.3.0 → 1.4.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/README.md +5 -1
- data/lib/sprockets/helpers.rb +53 -77
- data/lib/sprockets/helpers/asset_path.rb +3 -2
- data/lib/sprockets/helpers/base_path.rb +6 -2
- data/lib/sprockets/helpers/file_path.rb +1 -1
- data/lib/sprockets/helpers/manifest_path.rb +2 -1
- data/lib/sprockets/helpers/settings.rb +66 -0
- data/lib/sprockets/helpers/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '068064e923da4422a80742ce0e1f6a840799722e22294992e6c6e35e44dff080'
|
4
|
+
data.tar.gz: c0a762b6b31bad8b4384fa7ef5a90ab84b5d56c0e576934f71b5b76f0e4acbd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07eeb179f20c73a09b4ee1c24f0ccd91ff0b18e8584ae46db83013fdc52e8b211f5c15328794e55ac958916c7190875e5431093b459155994cf6b713402ed9e0
|
7
|
+
data.tar.gz: bcb76256c24bfe98c34c60b07ab10ab81933da61fd7baee351c434b4b93eaf4de9c44ad9fb8f58dd96ed388b1bf33e15f7ea2cb65333360362b13e493a93cd06
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# sprockets-helpers
|
2
2
|
|
3
|
-
**Asset path helpers for Sprockets 3.x & <= 2.2 applications**
|
3
|
+
**Asset path helpers for Sprockets 4.x & 3.x & <= 2.2 applications**
|
4
4
|
|
5
5
|
Sprockets::Helpers adds the asset_path helpers, familiar to Rails developers, to Sprockets 2.x assets and applications.
|
6
6
|
|
@@ -222,6 +222,10 @@ class App < Sinatra::Base
|
|
222
222
|
end
|
223
223
|
```
|
224
224
|
|
225
|
+
## Roda Integration
|
226
|
+
|
227
|
+
See: https://github.com/hmdne/roda-sprockets
|
228
|
+
|
225
229
|
## Copyright
|
226
230
|
|
227
231
|
Copyright (c) 2011 [Peter Browne](http://petebrowne.com). See LICENSE for details.
|
data/lib/sprockets/helpers.rb
CHANGED
@@ -4,78 +4,30 @@ require 'sprockets/helpers/base_path'
|
|
4
4
|
require 'sprockets/helpers/asset_path'
|
5
5
|
require 'sprockets/helpers/file_path'
|
6
6
|
require 'sprockets/helpers/manifest_path'
|
7
|
+
require 'sprockets/helpers/settings'
|
7
8
|
require 'uri'
|
9
|
+
require 'forwardable'
|
8
10
|
|
9
11
|
module Sprockets
|
10
12
|
module Helpers
|
11
13
|
class << self
|
12
|
-
|
13
|
-
attr_accessor :are_using_sprockets_3
|
14
|
+
extend Forwardable
|
14
15
|
|
15
|
-
#
|
16
|
-
attr_accessor :
|
16
|
+
# Indicates whenever we are using Sprockets 3.x or higher.
|
17
|
+
attr_accessor :are_using_sprockets_3_plus
|
17
18
|
|
18
|
-
#
|
19
|
-
attr_accessor :
|
19
|
+
# Indicates whenever we are using Sprockets 4.x or higher.
|
20
|
+
attr_accessor :are_using_sprockets_4_plus
|
20
21
|
|
21
|
-
#
|
22
|
-
attr_accessor :
|
22
|
+
# Settings of Sprockets::Helpers
|
23
|
+
attr_accessor :settings
|
23
24
|
|
24
|
-
#
|
25
|
-
|
26
|
-
# :expand => true
|
27
|
-
# :digest => false
|
28
|
-
# :manifest => false
|
29
|
-
attr_accessor :debug
|
30
|
-
|
31
|
-
# Set the Sprockets environment to search for assets.
|
32
|
-
# This defaults to the context's #environment method.
|
33
|
-
attr_accessor :environment
|
34
|
-
|
35
|
-
# The manifest file used for lookup
|
36
|
-
attr_accessor :manifest
|
37
|
-
|
38
|
-
# The base URL the Sprocket environment is mapped to.
|
39
|
-
# This defaults to '/assets'.
|
40
|
-
def prefix
|
41
|
-
@prefix ||= '/assets'
|
42
|
-
@prefix.is_a?(Array) ? "/#{@prefix.first}" : @prefix
|
43
|
-
end
|
44
|
-
attr_writer :prefix
|
45
|
-
|
46
|
-
# Customize the protocol when using asset hosts.
|
47
|
-
# If the value is :relative, A relative protocol ('//')
|
48
|
-
# will be used.
|
49
|
-
def protocol
|
50
|
-
@protocol ||= 'http://'
|
51
|
-
end
|
52
|
-
attr_writer :protocol
|
53
|
-
|
54
|
-
# The path to the public directory, where the assets
|
55
|
-
# not managed by Sprockets will be located.
|
56
|
-
# Defaults to './public'
|
57
|
-
def public_path
|
58
|
-
@public_path ||= './public'
|
59
|
-
end
|
60
|
-
attr_writer :public_path
|
61
|
-
|
62
|
-
# The default options for each asset path method. This is where you
|
63
|
-
# can change your default directories for the fallback directory.
|
64
|
-
def default_path_options
|
65
|
-
@default_path_options ||= {
|
66
|
-
:audio_path => { :dir => 'audios' },
|
67
|
-
:font_path => { :dir => 'fonts' },
|
68
|
-
:image_path => { :dir => 'images' },
|
69
|
-
:javascript_path => { :dir => 'javascripts', :ext => 'js' },
|
70
|
-
:stylesheet_path => { :dir => 'stylesheets', :ext => 'css' },
|
71
|
-
:video_path => { :dir => 'videos' }
|
72
|
-
}
|
73
|
-
end
|
74
|
-
attr_writer :default_path_options
|
25
|
+
# Access the settings directly for compatibility purposes.
|
26
|
+
def_delegators :@settings, *Settings.public_instance_methods(false)
|
75
27
|
|
76
28
|
# Convience method for configuring Sprockets::Helpers.
|
77
29
|
def configure
|
78
|
-
yield
|
30
|
+
yield settings
|
79
31
|
end
|
80
32
|
|
81
33
|
# Hack to ensure methods from Sprockets::Helpers override the
|
@@ -92,8 +44,11 @@ module Sprockets
|
|
92
44
|
end
|
93
45
|
end
|
94
46
|
|
47
|
+
@settings = Settings.new
|
48
|
+
|
95
49
|
# We are checking here to skip this at runtime
|
96
|
-
@
|
50
|
+
@are_using_sprockets_3_plus = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0')
|
51
|
+
@are_using_sprockets_4_plus = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.0')
|
97
52
|
|
98
53
|
# Returns the path to an asset either in the Sprockets environment
|
99
54
|
# or the public directory. External URIs are untouched.
|
@@ -128,9 +83,9 @@ module Sprockets
|
|
128
83
|
uri = URI.parse(source)
|
129
84
|
return source if uri.absolute?
|
130
85
|
|
131
|
-
options[:prefix] =
|
86
|
+
options[:prefix] = sprockets_helpers_settings.prefix unless options[:prefix]
|
132
87
|
|
133
|
-
if
|
88
|
+
if sprockets_helpers_settings.debug || options[:debug]
|
134
89
|
options[:manifest] = false
|
135
90
|
options[:digest] = false
|
136
91
|
options[:asset_host] = false
|
@@ -153,7 +108,9 @@ module Sprockets
|
|
153
108
|
|
154
109
|
def asset_tag(source, options = {}, &block)
|
155
110
|
raise ::ArgumentError, 'block missing' unless block
|
156
|
-
options = { :expand => !!
|
111
|
+
options = { :expand => (!!sprockets_helpers_settings.debug && !::Sprockets::Helpers.are_using_sprockets_4_plus) ||
|
112
|
+
!!sprockets_helpers_settings.expand,
|
113
|
+
:debug => sprockets_helpers_settings.debug }.merge(options)
|
157
114
|
|
158
115
|
path = asset_path(source, options)
|
159
116
|
output = if options[:expand] && path.respond_to?(:map)
|
@@ -167,7 +124,7 @@ module Sprockets
|
|
167
124
|
end
|
168
125
|
|
169
126
|
def javascript_tag(source, options = {})
|
170
|
-
options =
|
127
|
+
options = sprockets_helpers_settings.default_path_options[:javascript_path].merge(options)
|
171
128
|
asset_tag(source, options) do |path|
|
172
129
|
%Q(<script src="#{path}" type="text/javascript"></script>)
|
173
130
|
end
|
@@ -176,7 +133,7 @@ module Sprockets
|
|
176
133
|
def stylesheet_tag(source, options = {})
|
177
134
|
media = options.delete(:media)
|
178
135
|
media_attr = media.nil? ? nil : " media=\"#{media}\""
|
179
|
-
options =
|
136
|
+
options = sprockets_helpers_settings.default_path_options[:stylesheet_path].merge(options)
|
180
137
|
asset_tag(source, options) do |path|
|
181
138
|
%Q(<link rel="stylesheet" type="text/css" href="#{path}"#{media_attr}>)
|
182
139
|
end
|
@@ -202,7 +159,7 @@ module Sprockets
|
|
202
159
|
# audio_path 'http://www.example.com/img/audio.mp3' # => 'http://www.example.com/img/audio.mp3'
|
203
160
|
#
|
204
161
|
def audio_path(source, options = {})
|
205
|
-
asset_path source,
|
162
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:audio_path].merge(options)
|
206
163
|
end
|
207
164
|
alias_method :path_to_audio, :audio_path
|
208
165
|
|
@@ -226,7 +183,7 @@ module Sprockets
|
|
226
183
|
# font_path 'http://www.example.com/img/font.ttf' # => 'http://www.example.com/img/font.ttf'
|
227
184
|
#
|
228
185
|
def font_path(source, options = {})
|
229
|
-
asset_path source,
|
186
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:font_path].merge(options)
|
230
187
|
end
|
231
188
|
alias_method :path_to_font, :font_path
|
232
189
|
|
@@ -250,7 +207,7 @@ module Sprockets
|
|
250
207
|
# image_path 'http://www.example.com/img/edit.png' # => 'http://www.example.com/img/edit.png'
|
251
208
|
#
|
252
209
|
def image_path(source, options = {})
|
253
|
-
asset_path source,
|
210
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:image_path].merge(options)
|
254
211
|
end
|
255
212
|
alias_method :path_to_image, :image_path
|
256
213
|
|
@@ -275,7 +232,7 @@ module Sprockets
|
|
275
232
|
# javascript_path 'http://www.example.com/js/xmlhr.js' # => 'http://www.example.com/js/xmlhr.js'
|
276
233
|
#
|
277
234
|
def javascript_path(source, options = {})
|
278
|
-
asset_path source,
|
235
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:javascript_path].merge(options)
|
279
236
|
end
|
280
237
|
alias_method :path_to_javascript, :javascript_path
|
281
238
|
|
@@ -300,7 +257,7 @@ module Sprockets
|
|
300
257
|
# stylesheet_path 'http://www.example.com/css/style.css' # => 'http://www.example.com/css/style.css'
|
301
258
|
#
|
302
259
|
def stylesheet_path(source, options = {})
|
303
|
-
asset_path source,
|
260
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:stylesheet_path].merge(options)
|
304
261
|
end
|
305
262
|
alias_method :path_to_stylesheet, :stylesheet_path
|
306
263
|
|
@@ -324,7 +281,7 @@ module Sprockets
|
|
324
281
|
# video_path 'http://www.example.com/img/video.mp4' # => 'http://www.example.com/img/video.mp4'
|
325
282
|
#
|
326
283
|
def video_path(source, options = {})
|
327
|
-
asset_path source,
|
284
|
+
asset_path source, sprockets_helpers_settings.default_path_options[:video_path].merge(options)
|
328
285
|
end
|
329
286
|
alias_method :path_to_video, :video_path
|
330
287
|
|
@@ -332,19 +289,38 @@ module Sprockets
|
|
332
289
|
|
333
290
|
# Returns the Sprockets environment #asset_path uses to search for
|
334
291
|
# assets. This can be overridden for more control, if necessary.
|
335
|
-
#
|
292
|
+
# For even more control, you just need to override #sprockets_helper_settings.
|
293
|
+
# Defaults to sprockets_helpers_settings.environment or the environment
|
336
294
|
# returned by #environment.
|
337
295
|
def assets_environment
|
338
|
-
|
296
|
+
sprockets_helpers_settings.environment || environment
|
297
|
+
end
|
298
|
+
|
299
|
+
# Returns the Sprockets helpers settings. This can be overridden for
|
300
|
+
# more control, for instance if you want to support polyinstantiation
|
301
|
+
# in your application.
|
302
|
+
def sprockets_helpers_settings
|
303
|
+
Helpers.settings
|
339
304
|
end
|
340
305
|
|
341
306
|
def find_asset_path(uri, source, options = {})
|
342
|
-
|
343
|
-
|
307
|
+
options = options.merge(:sprockets_helpers_settings => sprockets_helpers_settings,
|
308
|
+
:debug => sprockets_helpers_settings.debug)
|
309
|
+
|
310
|
+
if sprockets_helpers_settings.manifest && options[:manifest] != false
|
311
|
+
manifest_path = sprockets_helpers_settings.manifest.assets[uri.path]
|
344
312
|
return Helpers::ManifestPath.new(uri, manifest_path, options) if manifest_path
|
345
313
|
end
|
346
314
|
|
347
|
-
if Sprockets::Helpers.
|
315
|
+
if Sprockets::Helpers.are_using_sprockets_4_plus
|
316
|
+
resolved, _ = assets_environment.resolve(uri.path, pipeline: options[:debug] ? :debug : nil)
|
317
|
+
|
318
|
+
if resolved
|
319
|
+
return Helpers::AssetPath.new(uri, assets_environment[uri.path, pipeline: options[:debug] ? :debug : nil], assets_environment, options)
|
320
|
+
else
|
321
|
+
return Helpers::FilePath.new(uri, options)
|
322
|
+
end
|
323
|
+
elsif Sprockets::Helpers.are_using_sprockets_3_plus
|
348
324
|
resolved, _ = assets_environment.resolve(uri.path)
|
349
325
|
|
350
326
|
if resolved
|
@@ -7,10 +7,11 @@ module Sprockets
|
|
7
7
|
@uri = uri
|
8
8
|
@asset = asset
|
9
9
|
@environment = environment
|
10
|
+
@options = options
|
10
11
|
@options = {
|
11
12
|
:body => false,
|
12
|
-
:digest =>
|
13
|
-
:prefix =>
|
13
|
+
:digest => sprockets_helpers_settings.digest,
|
14
|
+
:prefix => sprockets_helpers_settings.prefix
|
14
15
|
}.merge options
|
15
16
|
|
16
17
|
@uri.path = @options[:digest] ? asset.digest_path : asset.logical_path
|
@@ -17,6 +17,10 @@ module Sprockets
|
|
17
17
|
@options = options
|
18
18
|
end
|
19
19
|
|
20
|
+
def sprockets_helpers_settings
|
21
|
+
@options[:sprockets_helpers_settings]
|
22
|
+
end
|
23
|
+
|
20
24
|
# Returns the full path to the asset, complete with
|
21
25
|
# timestamp.
|
22
26
|
def to_s
|
@@ -53,7 +57,7 @@ module Sprockets
|
|
53
57
|
def compute_asset_host # :nodoc:
|
54
58
|
return nil if options[:asset_host] == false
|
55
59
|
|
56
|
-
if host = options[:asset_host] ||
|
60
|
+
if host = options[:asset_host] || sprockets_helpers_settings.asset_host
|
57
61
|
if host.respond_to?(:call)
|
58
62
|
host.call(uri.to_s)
|
59
63
|
elsif host =~ /%d/
|
@@ -69,7 +73,7 @@ module Sprockets
|
|
69
73
|
# Pick a scheme for the protocol if we are using
|
70
74
|
# an asset host.
|
71
75
|
def compute_scheme # :nodoc:
|
72
|
-
protocol = options[:protocol] ||
|
76
|
+
protocol = options[:protocol] || sprockets_helpers_settings.protocol
|
73
77
|
|
74
78
|
if protocol.nil? || protocol == :relative
|
75
79
|
nil
|
@@ -25,7 +25,7 @@ module Sprockets
|
|
25
25
|
# Returns the mtime for the given path (relative to
|
26
26
|
# the output path). Returns nil if the file doesn't exist.
|
27
27
|
def compute_mtime # :nodoc:
|
28
|
-
public_path = File.join(
|
28
|
+
public_path = File.join(sprockets_helpers_settings.public_path, uri.path)
|
29
29
|
|
30
30
|
if File.exist?(public_path)
|
31
31
|
File.mtime(public_path)
|
@@ -5,9 +5,10 @@ module Sprockets
|
|
5
5
|
class ManifestPath < AssetPath
|
6
6
|
def initialize(uri, path, options = {})
|
7
7
|
@uri = uri
|
8
|
+
@options = options
|
8
9
|
@options = {
|
9
10
|
:body => false,
|
10
|
-
:prefix =>
|
11
|
+
:prefix => sprockets_helpers_settings.prefix
|
11
12
|
}.merge options
|
12
13
|
|
13
14
|
@uri.path = path.to_s
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Helpers
|
3
|
+
class Settings
|
4
|
+
# Link to assets from a dedicated server.
|
5
|
+
attr_accessor :asset_host
|
6
|
+
|
7
|
+
# When true, the asset paths will return digest paths.
|
8
|
+
attr_accessor :digest
|
9
|
+
|
10
|
+
# When true, expand assets.
|
11
|
+
attr_accessor :expand
|
12
|
+
|
13
|
+
# When true, force debug mode
|
14
|
+
# :debug => true equals
|
15
|
+
# :expand => true (unless using >= Sprockets 4.0)
|
16
|
+
# :digest => false
|
17
|
+
# :manifest => false
|
18
|
+
attr_accessor :debug
|
19
|
+
|
20
|
+
# Set the Sprockets environment to search for assets.
|
21
|
+
# This defaults to the context's #environment method.
|
22
|
+
attr_accessor :environment
|
23
|
+
|
24
|
+
# The manifest file used for lookup
|
25
|
+
attr_accessor :manifest
|
26
|
+
|
27
|
+
# The base URL the Sprocket environment is mapped to.
|
28
|
+
# This defaults to '/assets'.
|
29
|
+
def prefix
|
30
|
+
@prefix ||= '/assets'
|
31
|
+
@prefix.is_a?(Array) ? "/#{@prefix.first}" : @prefix
|
32
|
+
end
|
33
|
+
attr_writer :prefix
|
34
|
+
|
35
|
+
# Customize the protocol when using asset hosts.
|
36
|
+
# If the value is :relative, A relative protocol ('//')
|
37
|
+
# will be used.
|
38
|
+
def protocol
|
39
|
+
@protocol ||= 'http://'
|
40
|
+
end
|
41
|
+
attr_writer :protocol
|
42
|
+
|
43
|
+
# The path to the public directory, where the assets
|
44
|
+
# not managed by Sprockets will be located.
|
45
|
+
# Defaults to './public'
|
46
|
+
def public_path
|
47
|
+
@public_path ||= './public'
|
48
|
+
end
|
49
|
+
attr_writer :public_path
|
50
|
+
|
51
|
+
# The default options for each asset path method. This is where you
|
52
|
+
# can change your default directories for the fallback directory.
|
53
|
+
def default_path_options
|
54
|
+
@default_path_options ||= {
|
55
|
+
:audio_path => { :dir => 'audios' },
|
56
|
+
:font_path => { :dir => 'fonts' },
|
57
|
+
:image_path => { :dir => 'images' },
|
58
|
+
:javascript_path => { :dir => 'javascripts', :ext => 'js' },
|
59
|
+
:stylesheet_path => { :dir => 'stylesheets', :ext => 'css' },
|
60
|
+
:video_path => { :dir => 'videos' }
|
61
|
+
}
|
62
|
+
end
|
63
|
+
attr_writer :default_path_options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pete Browne
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/sprockets/helpers/base_path.rb
|
130
130
|
- lib/sprockets/helpers/file_path.rb
|
131
131
|
- lib/sprockets/helpers/manifest_path.rb
|
132
|
+
- lib/sprockets/helpers/settings.rb
|
132
133
|
- lib/sprockets/helpers/version.rb
|
133
134
|
- spec/spec_helper.rb
|
134
135
|
- spec/sprockets-helpers_spec.rb
|
@@ -137,7 +138,7 @@ homepage: https://github.com/petebrowne/sprockets-helpers
|
|
137
138
|
licenses:
|
138
139
|
- MIT
|
139
140
|
metadata: {}
|
140
|
-
post_install_message:
|
141
|
+
post_install_message:
|
141
142
|
rdoc_options: []
|
142
143
|
require_paths:
|
143
144
|
- lib
|
@@ -152,8 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
153
|
- !ruby/object:Gem::Version
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
|
-
rubygems_version: 3.0.
|
156
|
-
signing_key:
|
156
|
+
rubygems_version: 3.0.3
|
157
|
+
signing_key:
|
157
158
|
specification_version: 4
|
158
159
|
summary: Asset path helpers for Sprockets 2.x & 3.x applications
|
159
160
|
test_files:
|