sprockets_rails3_backport 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +41 -0
- data/lib/action_view/asset_paths.rb +137 -0
- data/lib/extensions/application_ext.rb +5 -0
- data/lib/sprockets/assets.rake +95 -0
- data/lib/sprockets/bootstrap.rb +68 -0
- data/lib/sprockets/compressors.rb +21 -0
- data/lib/sprockets/helpers.rb +7 -0
- data/lib/sprockets/helpers/isolated_helper.rb +13 -0
- data/lib/sprockets/helpers/rails_helper.rb +171 -0
- data/lib/sprockets/railtie.rb +76 -0
- data/lib/sprockets/static_compiler.rb +61 -0
- data/lib/sprockets_rails3_backport.rb +1 -0
- metadata +82 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Shopify
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Backport of Rails 3.1.x Sprockets integration to Rails 3.0.x
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
In your `Gemfile`:
|
6
|
+
|
7
|
+
gem "sprockets_rails3_backport"
|
8
|
+
|
9
|
+
In your `routes.rb`:
|
10
|
+
|
11
|
+
MyApp::Application.routes.draw do
|
12
|
+
if (app = Rails.application).config.assets.compile
|
13
|
+
mount app.assets => app.config.assets.prefix
|
14
|
+
end
|
15
|
+
|
16
|
+
# ...
|
17
|
+
end
|
18
|
+
|
19
|
+
Here are the various `config.assets` options and their defaults:
|
20
|
+
|
21
|
+
config.assets.paths = []
|
22
|
+
config.assets.precompile = [ Proc.new{ |path| !['.js', '.css'].include?(File.extname(path)) },
|
23
|
+
/(?:\/|\\|\A)application\.(css|js)$/ ]
|
24
|
+
config.assets.prefix = "/assets"
|
25
|
+
config.assets.version = ''
|
26
|
+
config.assets.debug = false
|
27
|
+
config.assets.compile = true
|
28
|
+
config.assets.digest = false
|
29
|
+
config.assets.manifest = nil
|
30
|
+
config.assets.cache_store = false
|
31
|
+
config.assets.js_compressor = nil
|
32
|
+
config.assets.css_compressor = nil
|
33
|
+
config.assets.initialize_on_precompile = true
|
34
|
+
|
35
|
+
|
36
|
+
## Differences from Rails 3.1.3
|
37
|
+
|
38
|
+
* no `config.assets.enabled`
|
39
|
+
* `config.assets.cache_store` defaults to false, so you probably want to set it yourself
|
40
|
+
|
41
|
+
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
require 'active_support/core_ext/file'
|
3
|
+
|
4
|
+
module ActionView
|
5
|
+
|
6
|
+
class AssetPaths #:nodoc:
|
7
|
+
attr_reader :config, :controller
|
8
|
+
|
9
|
+
def initialize(config, controller = nil)
|
10
|
+
@config = config
|
11
|
+
@controller = controller
|
12
|
+
end
|
13
|
+
|
14
|
+
# Add the extension +ext+ if not present. Return full or scheme-relative URLs otherwise untouched.
|
15
|
+
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
|
16
|
+
# roots. Rewrite the asset path for cache-busting asset ids. Include
|
17
|
+
# asset host, if configured, with the correct request protocol.
|
18
|
+
#
|
19
|
+
# When :relative (default), the protocol will be determined by the client using current protocol
|
20
|
+
# When :request, the protocol will be the request protocol
|
21
|
+
# Otherwise, the protocol is used (E.g. :http, :https, etc)
|
22
|
+
def compute_public_path(source, dir, options = {})
|
23
|
+
source = source.to_s
|
24
|
+
return source if is_uri?(source)
|
25
|
+
|
26
|
+
source = rewrite_extension(source, dir, options[:ext]) if options[:ext]
|
27
|
+
source = rewrite_asset_path(source, dir, options)
|
28
|
+
source = rewrite_relative_url_root(source, relative_url_root)
|
29
|
+
source = rewrite_host_and_protocol(source, options[:protocol])
|
30
|
+
source
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return the filesystem path for the source
|
34
|
+
def compute_source_path(source, dir, ext)
|
35
|
+
source = rewrite_extension(source, dir, ext) if ext
|
36
|
+
File.join(config.assets_dir, dir, source)
|
37
|
+
end
|
38
|
+
|
39
|
+
def is_uri?(path)
|
40
|
+
path =~ %r{^[-a-z]+://|^cid:|^//}
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def rewrite_extension(source, dir, ext)
|
46
|
+
raise NotImplementedError
|
47
|
+
end
|
48
|
+
|
49
|
+
def rewrite_asset_path(source, path = nil)
|
50
|
+
raise NotImplementedError
|
51
|
+
end
|
52
|
+
|
53
|
+
def rewrite_relative_url_root(source, relative_url_root)
|
54
|
+
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
|
55
|
+
end
|
56
|
+
|
57
|
+
def has_request?
|
58
|
+
controller.respond_to?(:request)
|
59
|
+
end
|
60
|
+
|
61
|
+
def rewrite_host_and_protocol(source, protocol = nil)
|
62
|
+
host = compute_asset_host(source)
|
63
|
+
if host && !is_uri?(host)
|
64
|
+
if (protocol || default_protocol) == :request && !has_request?
|
65
|
+
host = nil
|
66
|
+
else
|
67
|
+
host = "#{compute_protocol(protocol)}#{host}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
host.nil? ? source : "#{host}#{source}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def compute_protocol(protocol)
|
74
|
+
protocol ||= default_protocol
|
75
|
+
case protocol
|
76
|
+
when :relative
|
77
|
+
"//"
|
78
|
+
when :request
|
79
|
+
unless @controller
|
80
|
+
invalid_asset_host!("The protocol requested was :request. Consider using :relative instead.")
|
81
|
+
end
|
82
|
+
@controller.request.protocol
|
83
|
+
else
|
84
|
+
"#{protocol}://"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def default_protocol
|
89
|
+
@config.default_asset_host_protocol || (has_request? ? :request : :relative)
|
90
|
+
end
|
91
|
+
|
92
|
+
def invalid_asset_host!(help_message)
|
93
|
+
raise ActionController::RoutingError, "This asset host cannot be computed without a request in scope. #{help_message}"
|
94
|
+
end
|
95
|
+
|
96
|
+
# Pick an asset host for this source. Returns +nil+ if no host is set,
|
97
|
+
# the host if no wildcard is set, the host interpolated with the
|
98
|
+
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
|
99
|
+
# or the value returned from invoking the proc if it's a proc or the value from
|
100
|
+
# invoking call if it's an object responding to call.
|
101
|
+
def compute_asset_host(source)
|
102
|
+
if host = asset_host_config
|
103
|
+
if host.respond_to?(:call)
|
104
|
+
args = [source]
|
105
|
+
arity = arity_of(host)
|
106
|
+
if arity > 1 && !has_request?
|
107
|
+
invalid_asset_host!("Remove the second argument to your asset_host Proc if you do not need the request.")
|
108
|
+
end
|
109
|
+
args << current_request if (arity > 1 || arity < 0) && has_request?
|
110
|
+
host.call(*args)
|
111
|
+
else
|
112
|
+
(host =~ /%d/) ? host % (Zlib.crc32(source) % 4) : host
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def relative_url_root
|
118
|
+
config.relative_url_root
|
119
|
+
end
|
120
|
+
|
121
|
+
def asset_host_config
|
122
|
+
config.asset_host
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns the current request if one exists.
|
126
|
+
def current_request
|
127
|
+
controller.request if has_request?
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns the arity of a callable
|
131
|
+
def arity_of(callable)
|
132
|
+
callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "fileutils"
|
2
|
+
|
3
|
+
namespace :assets do
|
4
|
+
def ruby_rake_task(task)
|
5
|
+
env = ENV['RAILS_ENV'] || 'production'
|
6
|
+
groups = ENV['RAILS_GROUPS'] || 'assets'
|
7
|
+
args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
|
8
|
+
args << "--trace" if Rake.application.options.trace
|
9
|
+
ruby *args
|
10
|
+
end
|
11
|
+
|
12
|
+
# We are currently running with no explicit bundler group
|
13
|
+
# and/or no explicit environment - we have to reinvoke rake to
|
14
|
+
# execute this task.
|
15
|
+
def invoke_or_reboot_rake_task(task)
|
16
|
+
if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
|
17
|
+
ruby_rake_task task
|
18
|
+
else
|
19
|
+
Rake::Task[task].invoke
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Compile all the assets named in config.assets.precompile"
|
24
|
+
task :precompile do
|
25
|
+
invoke_or_reboot_rake_task "assets:precompile:all"
|
26
|
+
end
|
27
|
+
|
28
|
+
namespace :precompile do
|
29
|
+
def internal_precompile(digest=nil)
|
30
|
+
# unless Rails.application.config.assets.enabled
|
31
|
+
# warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
|
32
|
+
# exit
|
33
|
+
# end
|
34
|
+
|
35
|
+
# Ensure that action view is loaded and the appropriate
|
36
|
+
# sprockets hooks get executed
|
37
|
+
_ = ActionView::Base
|
38
|
+
|
39
|
+
config = Rails.application.config
|
40
|
+
config.assets.compile = true
|
41
|
+
config.assets.digest = digest unless digest.nil?
|
42
|
+
config.assets.digests = {}
|
43
|
+
|
44
|
+
env = Rails.application.assets
|
45
|
+
target = File.join(Rails.public_path, config.assets.prefix)
|
46
|
+
compiler = Sprockets::StaticCompiler.new(env,
|
47
|
+
target,
|
48
|
+
config.assets.precompile,
|
49
|
+
:manifest_path => config.assets.manifest,
|
50
|
+
:digest => config.assets.digest,
|
51
|
+
:manifest => digest.nil?)
|
52
|
+
compiler.compile
|
53
|
+
end
|
54
|
+
|
55
|
+
task :all do
|
56
|
+
Rake::Task["assets:precompile:primary"].invoke
|
57
|
+
# We need to reinvoke in order to run the secondary digestless
|
58
|
+
# asset compilation run - a fresh Sprockets environment is
|
59
|
+
# required in order to compile digestless assets as the
|
60
|
+
# environment has already cached the assets on the primary
|
61
|
+
# run.
|
62
|
+
ruby_rake_task "assets:precompile:nondigest" if Rails.application.config.assets.digest
|
63
|
+
end
|
64
|
+
|
65
|
+
task :primary => ["assets:environment", "tmp:cache:clear"] do
|
66
|
+
internal_precompile
|
67
|
+
end
|
68
|
+
|
69
|
+
task :nondigest => ["assets:environment", "tmp:cache:clear"] do
|
70
|
+
internal_precompile(false)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
desc "Remove compiled assets"
|
75
|
+
task :clean do
|
76
|
+
invoke_or_reboot_rake_task "assets:clean:all"
|
77
|
+
end
|
78
|
+
|
79
|
+
namespace :clean do
|
80
|
+
task :all => ["assets:environment", "tmp:cache:clear"] do
|
81
|
+
config = Rails.application.config
|
82
|
+
public_asset_path = File.join(Rails.public_path, config.assets.prefix)
|
83
|
+
rm_rf public_asset_path, :secure => true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
task :environment do
|
88
|
+
if Rails.application.config.assets.initialize_on_precompile
|
89
|
+
Rake::Task["environment"].invoke
|
90
|
+
else
|
91
|
+
Rails.application.initialize!(:assets)
|
92
|
+
Sprockets::Bootstrap.new(Rails.application).run
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class Bootstrap
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
# TODO: Get rid of config.assets.enabled
|
8
|
+
def run
|
9
|
+
app, config = @app, @app.config
|
10
|
+
return unless app.assets
|
11
|
+
|
12
|
+
config.assets.paths.each { |path| app.assets.append_path(path) }
|
13
|
+
|
14
|
+
if config.assets.compress
|
15
|
+
# temporarily hardcode default JS compressor to uglify. Soon, it will work
|
16
|
+
# the same as SCSS, where a default plugin sets the default.
|
17
|
+
unless config.assets.js_compressor == false
|
18
|
+
app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
|
19
|
+
end
|
20
|
+
|
21
|
+
unless config.assets.css_compressor == false
|
22
|
+
app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
## I can't figure out how to do this properly in Rails 3.0, so you
|
27
|
+
## need to add the mount explicitly in your routes.rb (see README)
|
28
|
+
#
|
29
|
+
# if config.assets.compile
|
30
|
+
# app.routes.prepend do
|
31
|
+
# mount app.assets => config.assets.prefix
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
|
35
|
+
if config.assets.digest
|
36
|
+
app.assets = app.assets.index
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def expand_js_compressor(sym)
|
43
|
+
case sym
|
44
|
+
when :closure
|
45
|
+
require 'closure-compiler'
|
46
|
+
Closure::Compiler.new
|
47
|
+
when :uglifier
|
48
|
+
require 'uglifier'
|
49
|
+
Uglifier.new
|
50
|
+
when :yui
|
51
|
+
require 'yui/compressor'
|
52
|
+
YUI::JavaScriptCompressor.new
|
53
|
+
else
|
54
|
+
sym
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def expand_css_compressor(sym)
|
59
|
+
case sym
|
60
|
+
when :yui
|
61
|
+
require 'yui/compressor'
|
62
|
+
YUI::CssCompressor.new
|
63
|
+
else
|
64
|
+
sym
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Sprockets
|
2
|
+
class NullCompressor
|
3
|
+
def compress(content)
|
4
|
+
content
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class LazyCompressor
|
9
|
+
def initialize(&block)
|
10
|
+
@block = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def compressor
|
14
|
+
@compressor ||= @block.call || NullCompressor.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def compress(content)
|
18
|
+
compressor.compress(content)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'action_view/asset_paths'
|
2
|
+
|
3
|
+
module Sprockets
|
4
|
+
module Helpers
|
5
|
+
module RailsHelper
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include ActionView::Helpers::AssetTagHelper
|
8
|
+
|
9
|
+
def asset_paths
|
10
|
+
@asset_paths ||= begin
|
11
|
+
paths = RailsHelper::AssetPaths.new(config, controller)
|
12
|
+
paths.asset_environment = asset_environment
|
13
|
+
paths.asset_digests = asset_digests
|
14
|
+
paths.compile_assets = compile_assets?
|
15
|
+
paths.digest_assets = digest_assets?
|
16
|
+
paths
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def javascript_include_tag(*sources)
|
21
|
+
options = sources.extract_options!
|
22
|
+
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
|
23
|
+
body = options.key?(:body) ? options.delete(:body) : false
|
24
|
+
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
|
25
|
+
|
26
|
+
sources.collect do |source|
|
27
|
+
if debug && asset = asset_paths.asset_for(source, 'js')
|
28
|
+
asset.to_a.map { |dep|
|
29
|
+
super(dep.to_s, { :src => asset_path(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
|
30
|
+
}
|
31
|
+
else
|
32
|
+
super(source.to_s, { :src => asset_path(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
|
33
|
+
end
|
34
|
+
end.join("\n").html_safe
|
35
|
+
end
|
36
|
+
|
37
|
+
def stylesheet_link_tag(*sources)
|
38
|
+
options = sources.extract_options!
|
39
|
+
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
|
40
|
+
body = options.key?(:body) ? options.delete(:body) : false
|
41
|
+
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
|
42
|
+
|
43
|
+
sources.collect do |source|
|
44
|
+
if debug && asset = asset_paths.asset_for(source, 'css')
|
45
|
+
asset.to_a.map { |dep|
|
46
|
+
super(dep.to_s, { :href => asset_path(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
|
47
|
+
}
|
48
|
+
else
|
49
|
+
super(source.to_s, { :href => asset_path(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
|
50
|
+
end
|
51
|
+
end.join("\n").html_safe
|
52
|
+
end
|
53
|
+
|
54
|
+
def asset_path(source, options = {})
|
55
|
+
source = source.logical_path if source.respond_to?(:logical_path)
|
56
|
+
path = asset_paths.compute_public_path(source, asset_prefix, options.merge(:body => true))
|
57
|
+
options[:body] ? "#{path}?body=1" : path
|
58
|
+
end
|
59
|
+
|
60
|
+
def image_path(source)
|
61
|
+
asset_path(source)
|
62
|
+
end
|
63
|
+
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
|
64
|
+
|
65
|
+
def javascript_path(source)
|
66
|
+
asset_path(source)
|
67
|
+
end
|
68
|
+
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route
|
69
|
+
|
70
|
+
def stylesheet_path(source)
|
71
|
+
asset_path(source)
|
72
|
+
end
|
73
|
+
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route
|
74
|
+
|
75
|
+
private
|
76
|
+
def debug_assets?
|
77
|
+
begin
|
78
|
+
compile_assets? &&
|
79
|
+
(Rails.application.config.assets.debug ||
|
80
|
+
params[:debug_assets] == '1' ||
|
81
|
+
params[:debug_assets] == 'true')
|
82
|
+
rescue NoMethodError
|
83
|
+
false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Override to specify an alternative prefix for asset path generation.
|
88
|
+
# When combined with a custom +asset_environment+, this can be used to
|
89
|
+
# implement themes that can take advantage of the asset pipeline.
|
90
|
+
#
|
91
|
+
# If you only want to change where the assets are mounted, refer to
|
92
|
+
# +config.assets.prefix+ instead.
|
93
|
+
def asset_prefix
|
94
|
+
Rails.application.config.assets.prefix
|
95
|
+
end
|
96
|
+
|
97
|
+
def asset_digests
|
98
|
+
Rails.application.config.assets.digests
|
99
|
+
end
|
100
|
+
|
101
|
+
def compile_assets?
|
102
|
+
Rails.application.config.assets.compile
|
103
|
+
end
|
104
|
+
|
105
|
+
def digest_assets?
|
106
|
+
Rails.application.config.assets.digest
|
107
|
+
end
|
108
|
+
|
109
|
+
# Override to specify an alternative asset environment for asset
|
110
|
+
# path generation. The environment should already have been mounted
|
111
|
+
# at the prefix returned by +asset_prefix+.
|
112
|
+
def asset_environment
|
113
|
+
Rails.application.assets
|
114
|
+
end
|
115
|
+
|
116
|
+
class AssetPaths < ::ActionView::AssetPaths #:nodoc:
|
117
|
+
attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets
|
118
|
+
|
119
|
+
class AssetNotPrecompiledError < StandardError; end
|
120
|
+
|
121
|
+
# Return the filesystem path for the source
|
122
|
+
def compute_source_path(source, ext)
|
123
|
+
asset_for(source, ext)
|
124
|
+
end
|
125
|
+
|
126
|
+
def asset_for(source, ext)
|
127
|
+
source = source.to_s
|
128
|
+
return nil if is_uri?(source)
|
129
|
+
source = rewrite_extension(source, nil, ext)
|
130
|
+
asset_environment[source]
|
131
|
+
rescue Sprockets::FileOutsidePaths
|
132
|
+
nil
|
133
|
+
end
|
134
|
+
|
135
|
+
def digest_for(logical_path)
|
136
|
+
if digest_assets && asset_digests && (digest = asset_digests[logical_path])
|
137
|
+
return digest
|
138
|
+
end
|
139
|
+
|
140
|
+
if compile_assets
|
141
|
+
if digest_assets && asset = asset_environment[logical_path]
|
142
|
+
return asset.digest_path
|
143
|
+
end
|
144
|
+
return logical_path
|
145
|
+
else
|
146
|
+
raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def rewrite_asset_path(source, dir, options = {})
|
151
|
+
if source[0] == ?/
|
152
|
+
source
|
153
|
+
else
|
154
|
+
source = digest_for(source) unless options[:digest] == false
|
155
|
+
source = File.join(dir, source)
|
156
|
+
source = "/#{source}" unless source =~ /^\//
|
157
|
+
source
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def rewrite_extension(source, dir, ext)
|
162
|
+
if ext && File.extname(source).empty?
|
163
|
+
"#{source}.#{ext}"
|
164
|
+
else
|
165
|
+
source
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# require "action_controller/railtie"
|
2
|
+
require "extensions/application_ext"
|
3
|
+
|
4
|
+
module Sprockets
|
5
|
+
autoload :Bootstrap, "sprockets/bootstrap"
|
6
|
+
autoload :Helpers, "sprockets/helpers"
|
7
|
+
autoload :LazyCompressor, "sprockets/compressors"
|
8
|
+
autoload :NullCompressor, "sprockets/compressors"
|
9
|
+
autoload :StaticCompiler, "sprockets/static_compiler"
|
10
|
+
|
11
|
+
# TODO: Get rid of config.assets.enabled
|
12
|
+
class Railtie < ::Rails::Railtie
|
13
|
+
# config.action_controller.default_asset_host_protocol = :relative
|
14
|
+
config.assets = ActiveSupport::OrderedOptions.new
|
15
|
+
# config.assets.enabled = false
|
16
|
+
config.assets.paths = []
|
17
|
+
config.assets.precompile = [ Proc.new{ |path| !['.js', '.css'].include?(File.extname(path)) },
|
18
|
+
/(?:\/|\\|\A)application\.(css|js)$/ ]
|
19
|
+
config.assets.prefix = "/assets"
|
20
|
+
config.assets.version = ''
|
21
|
+
config.assets.debug = false
|
22
|
+
config.assets.compile = true
|
23
|
+
config.assets.digest = false
|
24
|
+
config.assets.manifest = nil
|
25
|
+
config.assets.cache_store = false
|
26
|
+
config.assets.js_compressor = nil
|
27
|
+
config.assets.css_compressor = nil
|
28
|
+
config.assets.initialize_on_precompile = true
|
29
|
+
|
30
|
+
rake_tasks do
|
31
|
+
load "sprockets/assets.rake"
|
32
|
+
end
|
33
|
+
|
34
|
+
initializer "sprockets.environment", :group => :all do |app|
|
35
|
+
config = app.config
|
36
|
+
# next unless config.assets.enabled
|
37
|
+
|
38
|
+
require 'sprockets'
|
39
|
+
|
40
|
+
app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
|
41
|
+
env.logger = ::Rails.logger
|
42
|
+
env.version = ::Rails.env + "-#{config.assets.version}"
|
43
|
+
|
44
|
+
if config.assets.cache_store != false
|
45
|
+
env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
if config.assets.manifest
|
50
|
+
path = File.join(config.assets.manifest, "manifest.yml")
|
51
|
+
else
|
52
|
+
path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml")
|
53
|
+
end
|
54
|
+
|
55
|
+
if File.exist?(path)
|
56
|
+
config.assets.digests = YAML.load_file(path)
|
57
|
+
end
|
58
|
+
|
59
|
+
ActiveSupport.on_load(:action_view) do
|
60
|
+
include ::Sprockets::Helpers::RailsHelper
|
61
|
+
app.assets.context_class.instance_eval do
|
62
|
+
include ::Sprockets::Helpers::IsolatedHelper
|
63
|
+
include ::Sprockets::Helpers::RailsHelper
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# We need to configure this after initialization to ensure we collect
|
69
|
+
# paths from all engines. This hook is invoked exactly before routes
|
70
|
+
# are compiled, and so that other Railties have an opportunity to
|
71
|
+
# register compressors.
|
72
|
+
config.after_initialize do |app|
|
73
|
+
Sprockets::Bootstrap.new(app).run
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
module Sprockets
|
4
|
+
class StaticCompiler
|
5
|
+
attr_accessor :env, :target, :paths
|
6
|
+
|
7
|
+
def initialize(env, target, paths, options = {})
|
8
|
+
@env = env
|
9
|
+
@target = target
|
10
|
+
@paths = paths
|
11
|
+
@digest = options.key?(:digest) ? options.delete(:digest) : true
|
12
|
+
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
|
13
|
+
@manifest_path = options.delete(:manifest_path) || target
|
14
|
+
end
|
15
|
+
|
16
|
+
def compile
|
17
|
+
manifest = {}
|
18
|
+
env.each_logical_path do |logical_path|
|
19
|
+
next unless compile_path?(logical_path)
|
20
|
+
if asset = env.find_asset(logical_path)
|
21
|
+
manifest[logical_path] = write_asset(asset)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
write_manifest(manifest) if @manifest
|
25
|
+
end
|
26
|
+
|
27
|
+
def write_manifest(manifest)
|
28
|
+
FileUtils.mkdir_p(@manifest_path)
|
29
|
+
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
|
30
|
+
YAML.dump(manifest, f)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_asset(asset)
|
35
|
+
path_for(asset).tap do |path|
|
36
|
+
filename = File.join(target, path)
|
37
|
+
FileUtils.mkdir_p File.dirname(filename)
|
38
|
+
asset.write_to(filename)
|
39
|
+
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def compile_path?(logical_path)
|
44
|
+
paths.each do |path|
|
45
|
+
case path
|
46
|
+
when Regexp
|
47
|
+
return true if path.match(logical_path)
|
48
|
+
when Proc
|
49
|
+
return true if path.call(logical_path)
|
50
|
+
else
|
51
|
+
return true if File.fnmatch(path.to_s, logical_path)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
def path_for(asset)
|
58
|
+
@digest ? asset.digest_path : asset.logical_path
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "sprockets/railtie"
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sprockets_rails3_backport
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James MacAulay
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-01 00:00:00.000000000 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
requirement: &2156849120 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2156849120
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: sprockets
|
28
|
+
requirement: &2156848660 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - =
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2156848660
|
37
|
+
description:
|
38
|
+
email:
|
39
|
+
- james@shopify.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- lib/action_view/asset_paths.rb
|
45
|
+
- lib/extensions/application_ext.rb
|
46
|
+
- lib/sprockets/assets.rake
|
47
|
+
- lib/sprockets/bootstrap.rb
|
48
|
+
- lib/sprockets/compressors.rb
|
49
|
+
- lib/sprockets/helpers/isolated_helper.rb
|
50
|
+
- lib/sprockets/helpers/rails_helper.rb
|
51
|
+
- lib/sprockets/helpers.rb
|
52
|
+
- lib/sprockets/railtie.rb
|
53
|
+
- lib/sprockets/static_compiler.rb
|
54
|
+
- lib/sprockets_rails3_backport.rb
|
55
|
+
- MIT-LICENSE
|
56
|
+
- README.markdown
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/jamesmacaulay/sprockets_rails3_backport
|
59
|
+
licenses: []
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.6
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.6.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Rails 3.1.x Sprockets functionality for Rails 3.0.x
|
82
|
+
test_files: []
|