sprockets-rails 1.0.0 → 2.0.0.backport1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +155 -0
  3. data/lib/sprockets/rails.rb +3 -0
  4. data/lib/sprockets/rails/helper.rb +145 -0
  5. data/lib/sprockets/rails/legacy_asset_tag_helper.rb +32 -0
  6. data/lib/sprockets/rails/legacy_asset_url_helper.rb +130 -0
  7. data/lib/sprockets/rails/task.rb +82 -0
  8. data/lib/sprockets/railtie.rb +125 -0
  9. metadata +52 -81
  10. data/MIT-LICENSE +0 -20
  11. data/README.rdoc +0 -3
  12. data/Rakefile +0 -24
  13. data/lib/sprockets-rails.rb +0 -7
  14. data/lib/sprockets/rails/bootstrap.rb +0 -41
  15. data/lib/sprockets/rails/compressors.rb +0 -87
  16. data/lib/sprockets/rails/helpers.rb +0 -8
  17. data/lib/sprockets/rails/helpers/isolated_helper.rb +0 -15
  18. data/lib/sprockets/rails/helpers/rails_helper.rb +0 -169
  19. data/lib/sprockets/rails/railtie.rb +0 -64
  20. data/lib/sprockets/rails/static_compiler.rb +0 -64
  21. data/lib/sprockets/rails/version.rb +0 -5
  22. data/lib/tasks/assets.rake +0 -105
  23. data/test/abstract_unit.rb +0 -145
  24. data/test/assets_debugging_test.rb +0 -65
  25. data/test/assets_test.rb +0 -532
  26. data/test/fixtures/alternate/stylesheets/style.css +0 -1
  27. data/test/fixtures/app/fonts/dir/font.ttf +0 -0
  28. data/test/fixtures/app/fonts/font.ttf +0 -0
  29. data/test/fixtures/app/images/logo.png +0 -0
  30. data/test/fixtures/app/javascripts/application.js +0 -1
  31. data/test/fixtures/app/javascripts/dir/xmlhr.js +0 -0
  32. data/test/fixtures/app/javascripts/extra.js +0 -0
  33. data/test/fixtures/app/javascripts/xmlhr.js +0 -0
  34. data/test/fixtures/app/stylesheets/application.css +0 -1
  35. data/test/fixtures/app/stylesheets/dir/style.css +0 -0
  36. data/test/fixtures/app/stylesheets/extra.css +0 -0
  37. data/test/fixtures/app/stylesheets/style.css +0 -0
  38. data/test/sprockets_compressors_test.rb +0 -27
  39. data/test/sprockets_helper_test.rb +0 -345
  40. data/test/sprockets_helper_with_routes_test.rb +0 -60
  41. data/test/test_helper.rb +0 -84
@@ -1,7 +0,0 @@
1
- require 'sprockets'
2
- require 'sprockets/rails/railtie'
3
-
4
- module Sprockets
5
- module Rails
6
- end
7
- end
@@ -1,41 +0,0 @@
1
- module Sprockets
2
- module Rails
3
- class Bootstrap
4
- def initialize(app)
5
- @app = app
6
- end
7
-
8
- # TODO: Get rid of config.assets.enabled
9
- def run
10
- app, config = @app, @app.config
11
- return unless app.assets
12
-
13
- config.assets.paths.each { |path| app.assets.append_path(path) }
14
-
15
- if config.assets.compress
16
- # temporarily hardcode default JS compressor to uglify. Soon, it will work
17
- # the same as SCSS, where a default plugin sets the default.
18
- unless config.assets.js_compressor == false
19
- js_compressor = config.assets.js_compressor || :uglifier
20
- app.assets.js_compressor = LazyCompressor.new { Sprockets::Rails::Compressors.registered_js_compressor(js_compressor) }
21
- end
22
-
23
- unless config.assets.css_compressor == false
24
- css_compressor = config.assets.css_compressor
25
- app.assets.css_compressor = LazyCompressor.new { Sprockets::Rails::Compressors.registered_css_compressor(css_compressor) }
26
- end
27
- end
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
- end
40
- end
41
- end
@@ -1,87 +0,0 @@
1
- module Sprockets
2
- module Rails
3
- module Compressors
4
- extend self
5
-
6
- @@css_compressors = {}
7
- @@js_compressors = {}
8
- @@default_css_compressor = nil
9
- @@default_js_compressor = nil
10
-
11
- def register_css_compressor(name, klass, options = {})
12
- @@default_css_compressor = name.to_sym if options[:default] || @@default_css_compressor.nil?
13
- @@css_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] }
14
- end
15
-
16
- def register_js_compressor(name, klass, options = {})
17
- @@default_js_compressor = name.to_sym if options[:default] || @@default_js_compressor.nil?
18
- @@js_compressors[name.to_sym] = { :klass => klass.to_s, :require => options[:require] }
19
- end
20
-
21
- def registered_css_compressor(name)
22
- find_registered_compressor name, @@css_compressors, @@default_css_compressor
23
- end
24
-
25
- def registered_js_compressor(name)
26
- find_registered_compressor name, @@js_compressors, @@default_js_compressor
27
- end
28
-
29
- # The default compressors must be registered in default plugins (ex. Sass-Rails)
30
- register_css_compressor(:scss, 'Sass::Rails::Compressor', :require => 'sass/rails/compressor', :default => true)
31
- register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier', :default => true)
32
-
33
- # Automaticaly register some compressors
34
- register_css_compressor(:yui, 'YUI::CssCompressor', :require => 'yui/compressor')
35
- register_js_compressor(:closure, 'Closure::Compiler', :require => 'closure-compiler')
36
- register_js_compressor(:yui, 'YUI::JavaScriptCompressor', :require => 'yui/compressor')
37
-
38
- private
39
-
40
- def find_registered_compressor(name, compressors_hash, default_compressor_name)
41
- if name.respond_to?(:to_sym)
42
- compressor = compressors_hash[name.to_sym] || compressors_hash[default_compressor_name]
43
- require compressor[:require] if compressor[:require]
44
- compressor[:klass].constantize.new
45
- else
46
- name
47
- end
48
- end
49
- end
50
-
51
- # An asset compressor which does nothing.
52
- #
53
- # This compressor simply returns the asset as-is, without any compression
54
- # whatsoever. It is useful in development mode, when compression isn't
55
- # needed but using the same asset pipeline as production is desired.
56
- class NullCompressor #:nodoc:
57
- def compress(content)
58
- content
59
- end
60
- end
61
-
62
- # An asset compressor which only initializes the underlying compression
63
- # engine when needed.
64
- #
65
- # This postpones the initialization of the compressor until
66
- # <code>#compress</code> is called the first time.
67
- class LazyCompressor #:nodoc:
68
- # Initializes a new LazyCompressor.
69
- #
70
- # The block should return a compressor when called, i.e. an object
71
- # which responds to <code>#compress</code>.
72
- def initialize(&block)
73
- @block = block
74
- end
75
-
76
- def compress(content)
77
- compressor.compress(content)
78
- end
79
-
80
- private
81
-
82
- def compressor
83
- @compressor ||= (@block.call || NullCompressor.new)
84
- end
85
- end
86
- end
87
- end
@@ -1,8 +0,0 @@
1
- module Sprockets
2
- module Rails
3
- module Helpers
4
- autoload :RailsHelper, "sprockets/rails/helpers/rails_helper"
5
- autoload :IsolatedHelper, "sprockets/rails/helpers/isolated_helper"
6
- end
7
- end
8
- end
@@ -1,15 +0,0 @@
1
- module Sprockets
2
- module Rails
3
- module Helpers
4
- module IsolatedHelper
5
- def controller
6
- nil
7
- end
8
-
9
- def config
10
- ::Rails.application.config.action_controller
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,169 +0,0 @@
1
- require "action_view"
2
-
3
- module Sprockets
4
- module Rails
5
- module Helpers
6
- module RailsHelper
7
- extend ActiveSupport::Concern
8
- include ActionView::Helpers::AssetTagHelper
9
-
10
- def asset_paths
11
- @asset_paths ||= begin
12
- paths = RailsHelper::AssetPaths.new(config, controller)
13
- paths.asset_environment = asset_environment
14
- paths.asset_digests = asset_digests
15
- paths.compile_assets = compile_assets?
16
- paths.digest_assets = digest_assets?
17
- paths
18
- end
19
- end
20
-
21
- def javascript_include_tag(*sources)
22
- options = sources.extract_options!
23
- debug = options.delete(:debug) { debug_assets? }
24
- body = options.delete(:body) { false }
25
- digest = options.delete(:digest) { digest_assets? }
26
-
27
- sources.collect do |source|
28
- if debug && asset = asset_paths.asset_for(source, 'js')
29
- asset.to_a.map { |dep|
30
- super(dep.pathname.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
31
- }
32
- else
33
- super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
34
- end
35
- end.join("\n").html_safe
36
- end
37
-
38
- def stylesheet_link_tag(*sources)
39
- options = sources.extract_options!
40
- debug = options.delete(:debug) { debug_assets? }
41
- body = options.delete(:body) { false }
42
- digest = options.delete(:digest) { digest_assets? }
43
-
44
- sources.collect do |source|
45
- if debug && asset = asset_paths.asset_for(source, 'css')
46
- asset.to_a.map { |dep|
47
- super(dep.pathname.to_s, { :href => path_to_asset(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
48
- }
49
- else
50
- super(source.to_s, { :href => path_to_asset(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
51
- end
52
- end.join("\n").html_safe
53
- end
54
-
55
- def asset_path(source, options = {})
56
- source = source.logical_path if source.respond_to?(:logical_path)
57
- path = asset_paths.compute_public_path(source, asset_prefix, options.merge(:body => true))
58
- options[:body] ? "#{path}?body=1" : path
59
- end
60
- alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
61
-
62
- def image_path(source)
63
- path_to_asset(source)
64
- end
65
- alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
66
-
67
- def font_path(source)
68
- path_to_asset(source)
69
- end
70
- alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
71
-
72
- def javascript_path(source)
73
- path_to_asset(source, :ext => 'js')
74
- end
75
- alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route
76
-
77
- def stylesheet_path(source)
78
- path_to_asset(source, :ext => 'css')
79
- end
80
- alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route
81
-
82
- private
83
- def debug_assets?
84
- compile_assets? && (::Rails.application.config.assets.debug || params[:debug_assets])
85
- rescue NameError
86
- false
87
- end
88
-
89
- # Override to specify an alternative prefix for asset path generation.
90
- # When combined with a custom +asset_environment+, this can be used to
91
- # implement themes that can take advantage of the asset pipeline.
92
- #
93
- # If you only want to change where the assets are mounted, refer to
94
- # +config.assets.prefix+ instead.
95
- def asset_prefix
96
- ::Rails.application.config.assets.prefix
97
- end
98
-
99
- def asset_digests
100
- ::Rails.application.config.assets.digests
101
- end
102
-
103
- def compile_assets?
104
- ::Rails.application.config.assets.compile
105
- end
106
-
107
- def digest_assets?
108
- ::Rails.application.config.assets.digest
109
- end
110
-
111
- # Override to specify an alternative asset environment for asset
112
- # path generation. The environment should already have been mounted
113
- # at the prefix returned by +asset_prefix+.
114
- def asset_environment
115
- ::Rails.application.assets
116
- end
117
-
118
- class AssetPaths < ::ActionView::AssetPaths #:nodoc:
119
- attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets
120
-
121
- class AssetNotPrecompiledError < StandardError; end
122
-
123
- def asset_for(source, ext)
124
- source = source.to_s
125
- return nil if is_uri?(source)
126
- source = rewrite_extension(source, nil, ext)
127
- asset_environment[source]
128
- rescue Sprockets::FileOutsidePaths
129
- nil
130
- end
131
-
132
- def digest_for(logical_path)
133
- if digest_assets && asset_digests && (digest = asset_digests[logical_path])
134
- return digest
135
- end
136
-
137
- if compile_assets
138
- if digest_assets && asset = asset_environment[logical_path]
139
- return asset.digest_path
140
- end
141
- return logical_path
142
- else
143
- raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
144
- end
145
- end
146
-
147
- def rewrite_asset_path(source, dir, options = {})
148
- if source[0] == ?/
149
- source
150
- else
151
- source = digest_for(source) unless options[:digest] == false
152
- source = File.join(dir, source)
153
- source = "/#{source}" unless source =~ /^\//
154
- source
155
- end
156
- end
157
-
158
- def rewrite_extension(source, dir, ext)
159
- if ext && File.extname(source) != ".#{ext}"
160
- "#{source}.#{ext}"
161
- else
162
- source
163
- end
164
- end
165
- end
166
- end
167
- end
168
- end
169
- end
@@ -1,64 +0,0 @@
1
- require "action_controller/railtie"
2
-
3
- module Sprockets
4
- module Rails
5
- autoload :Bootstrap, "sprockets/rails/bootstrap"
6
- autoload :Helpers, "sprockets/rails/helpers"
7
- autoload :Compressors, "sprockets/rails/compressors"
8
- autoload :LazyCompressor, "sprockets/rails/compressors"
9
- autoload :NullCompressor, "sprockets/rails/compressors"
10
- autoload :StaticCompiler, "sprockets/rails/static_compiler"
11
-
12
- # TODO: Get rid of config.assets.enabled
13
- class Railtie < ::Rails::Railtie
14
- rake_tasks do
15
- load "tasks/assets.rake"
16
- end
17
-
18
- initializer "sprockets.environment", :group => :all do |app|
19
- config = app.config
20
- next unless config.assets.enabled
21
-
22
- require 'sprockets'
23
-
24
- app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
25
- env.version = ::Rails.env + "-#{config.assets.version}"
26
-
27
- if config.assets.logger != false
28
- env.logger = config.assets.logger || ::Rails.logger
29
- end
30
-
31
- if config.assets.cache_store != false
32
- env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
33
- end
34
- end
35
-
36
- if config.assets.manifest
37
- path = File.join(config.assets.manifest, "manifest.yml")
38
- else
39
- path = File.join(::Rails.public_path, config.assets.prefix, "manifest.yml")
40
- end
41
-
42
- if File.exist?(path)
43
- config.assets.digests = YAML.load_file(path)
44
- end
45
-
46
- ActiveSupport.on_load(:action_view) do
47
- include ::Sprockets::Rails::Helpers::RailsHelper
48
- app.assets.context_class.instance_eval do
49
- include ::Sprockets::Rails::Helpers::IsolatedHelper
50
- include ::Sprockets::Rails::Helpers::RailsHelper
51
- end
52
- end
53
- end
54
-
55
- # We need to configure this after initialization to ensure we collect
56
- # paths from all engines. This hook is invoked exactly before routes
57
- # are compiled, and so that other Railties have an opportunity to
58
- # register compressors.
59
- config.after_initialize do |app|
60
- Sprockets::Rails::Bootstrap.new(app).run
61
- end
62
- end
63
- end
64
- end
@@ -1,64 +0,0 @@
1
- require 'fileutils'
2
-
3
- module Sprockets
4
- module Rails
5
- class StaticCompiler
6
- attr_accessor :env, :target, :paths
7
-
8
- def initialize(env, target, paths, options = {})
9
- @env = env
10
- @target = target
11
- @paths = paths
12
- @digest = options.fetch(:digest, true)
13
- @manifest = options.fetch(:manifest, true)
14
- @manifest_path = options.delete(:manifest_path) || target
15
- @zip_files = options.delete(:zip_files) || /\.(?:css|html|js|svg|txt|xml)$/
16
- end
17
-
18
- def compile
19
- manifest = {}
20
- env.each_logical_path do |logical_path|
21
- next unless compile_path?(logical_path)
22
- if asset = env.find_asset(logical_path)
23
- manifest[logical_path] = write_asset(asset)
24
- end
25
- end
26
- write_manifest(manifest) if @manifest
27
- end
28
-
29
- def write_manifest(manifest)
30
- FileUtils.mkdir_p(@manifest_path)
31
- File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
32
- YAML.dump(manifest, f)
33
- end
34
- end
35
-
36
- def write_asset(asset)
37
- path_for(asset).tap do |path|
38
- filename = File.join(target, path)
39
- FileUtils.mkdir_p File.dirname(filename)
40
- asset.write_to(filename)
41
- asset.write_to("#{filename}.gz") if filename.to_s =~ @zip_files
42
- end
43
- end
44
-
45
- def compile_path?(logical_path)
46
- paths.each do |path|
47
- case path
48
- when Regexp
49
- return true if path.match(logical_path)
50
- when Proc
51
- return true if path.call(logical_path)
52
- else
53
- return true if File.fnmatch(path.to_s, logical_path)
54
- end
55
- end
56
- false
57
- end
58
-
59
- def path_for(asset)
60
- @digest ? asset.digest_path : asset.logical_path
61
- end
62
- end
63
- end
64
- end