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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b54d7de577eca6df5b44639df6929423a10b7311
4
+ data.tar.gz: 4df8d48affeec6bb7d1f1b4243a1c58c02642ef1
5
+ SHA512:
6
+ metadata.gz: e28cb27742cf4e57dfdce771577f6040391935b70565c53e8740da42587417f44d15fbdd7d36e34b7cc8dfb986cf6411b150791879bb78de374d1d1696ef6800
7
+ data.tar.gz: 2192f3965ac0b09a84255e2532965be55b47daf504f996c5695b1fa509586957ac625ab48039e39c7854faf48cf2ba8518be43db75f9799b20b87bea95e265c4
data/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # Sprockets Rails
2
+
3
+ Provides Sprockets implementation for Rails 4.x (and beyond) Asset Pipeline.
4
+
5
+
6
+ ## Installation
7
+
8
+ ``` ruby
9
+ gem 'sprockets-rails', :require => 'sprockets/railtie'
10
+ ```
11
+
12
+ Or alternatively `require 'sprockets/railtie'` in your `config/application.rb` if you have Bundler auto-require disabled.
13
+
14
+
15
+ ## Usage
16
+
17
+
18
+ ### Rake task
19
+
20
+ **`rake assets:precompile`**
21
+
22
+ Deployment task that compiles any assets listed in `config.assets.precompile` to `public/assets`.
23
+
24
+ **`rake assets:clean`**
25
+
26
+ Only removes old assets (keeps the most recent 3 copies) from `public/assets`. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.
27
+
28
+ **`rake assets:clobber`**
29
+
30
+ Nuke `public/assets` and clear the Sprockets file system cache.
31
+
32
+ #### Customize
33
+
34
+ If the basic tasks don't do all that you need, it's straight forward to redefine them and replace them with something more specific to your app.
35
+
36
+ You can also redefine the task with the built in task generator.
37
+
38
+ ``` ruby
39
+ require 'sprockets/rails/task'
40
+ Sprockets::Rails::Task.new do |t|
41
+ t.environment = lambda { Rails.application.assets }
42
+ t.assets = %w( application.js application.css )
43
+ t.keep = 5
44
+ end
45
+ ```
46
+
47
+ Each asset task will invoke `assets:environment` first. By default this loads the Rails environment. You can override this task to add or remove dependencies for your specific compilation environment.
48
+
49
+ Also see [Sprockest::Rails::Task](https://github.com/josh/sprockets-rails/blob/master/lib/sprockets/rails/task.rb) and [Rake::SprocketsTask](https://github.com/sstephenson/sprockets/blob/master/lib/rake/sprocketstask.rb).
50
+
51
+
52
+ ### Initializer options
53
+
54
+ **`config.assets.precompile`**
55
+
56
+ Add additional assets to compile on deploy. Defaults to `application.js`, `application.css` and any other non-js/css file under `app/assets`.
57
+
58
+ **`config.assets.paths`**
59
+
60
+ Add additional load paths to this Array. Rails includes `app/assets` and `vendor/assets` for you already. Plugins might want to add their custom paths to to this.
61
+
62
+ **`config.assets.version`**
63
+
64
+ Set a custom cache buster string. Changing it will cause all assets to recompile on the next build.
65
+
66
+ ``` ruby
67
+ config.assets.version = 'v1'
68
+ # after installing a new plugin, change loads paths
69
+ config.assets.version = 'v2'
70
+ ```
71
+
72
+ **`config.assets.prefix`**
73
+
74
+ Defaults to `/assets`. Changes the directory to compile assets to.
75
+
76
+ **`config.assets.digest`**
77
+
78
+ Link to undigest asset filenames. This option will eventually go away. Unless when `compile` is disabled.
79
+
80
+ **`config.assets.debug`**
81
+
82
+ Enable expanded asset debugging mode. Individual files will be served to make referencing filenames in the web console easier. This feature will eventually be deprecated and replaced by Source Maps in Sprockets 3.x.
83
+
84
+ **`config.assets.compile`**
85
+
86
+ Enables Sprockest compile environment. If disabled, `Rails.application.assets` will be unavailable to any ActionView helpers. View helpers will depend on assets being precompiled to `public/assets` in order to link to them. You can still access the environment by directly calling `Rails.application.assets`.
87
+
88
+ **`config.assets.configure`**
89
+
90
+ Invokes block with environment when the environment is initialized. Allows direct access to the environment instance and lets you lazily load libraries only needed for asset compiling.
91
+
92
+ ``` ruby
93
+ config.assets.configure do |env|
94
+ env.js_compressor = :uglify # or :closure, :yui
95
+ env.css_compressor = :sass # or :yui
96
+
97
+ require 'my_processor'
98
+ env.register_preprocessor 'application/javascript', MyProcessor
99
+
100
+ env.logger = Rails.logger
101
+
102
+ env.cache = ActiveSupport::Cache::FileStore.new("tmp/cache/assets")
103
+ end
104
+ ```
105
+
106
+
107
+ ## Complementary plugins
108
+
109
+ The following plugins provide some extras for the Sprockets Asset Pipeline.
110
+
111
+ * [coffee-rails](https://github.com/rails/coffee-rails)
112
+ * [sass-rails](https://github.com/rails/sass-rails)
113
+
114
+ **NOTE** That these plugins are optional. The core coffee-script, sass, less, uglify, (any many more) features are built into Sprockets itself. Many of these plugins only provide generators and extra helpers. You can probably get by without them.
115
+
116
+
117
+ ## Changes from Rails 3.x
118
+
119
+ * Only compiles digest filenames. Static non-digest assets should simply live in public/.
120
+ * Unmanaged asset paths and urls fallback to linking to public/. This should make it easier to work with both compiled assets and simple static assets. As a side effect, there will never be any "asset not precompiled errors" when linking to missing assets. They will just link to a public file which may or may not exist.
121
+ * JS and CSS compressors must be explicitly set. Magic detection has been removed to avoid loading compressors in environments where you want to avoid loading any of the asset libraries. Assign `config.assets.js_compressor = :uglify` or `config.assets.css_compressor = :sass` for the standard compressors.
122
+ * The manifest file is now in a JSON format. Since it lives in public/ by default, the initial filename is also randomized to obfuscate public access to the resource.
123
+ * Two cleanup tasks. `rake assets:clean` is now a safe cleanup that only removes older assets that are no longer used. While `rake assets:clobber` nukes the entire `public/assets` directory and clears your filesystem cache. The clean task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.
124
+
125
+
126
+ ## Contributing
127
+
128
+ Usual bundler workflow.
129
+
130
+ ``` shell
131
+ $ git clone https://github.com/rails/sprockets-rails.git
132
+ $ cd sprockets-rails/
133
+ $ bundle install
134
+ $ bundle exec rake test
135
+ ```
136
+
137
+ [![Build Status](https://secure.travis-ci.org/rails/sprockets-rails.png)](http://travis-ci.org/rails/sprockets-rails)
138
+
139
+
140
+ ## Releases
141
+
142
+ sprockets-rails 2.x will primarily target sprockets 2.x. And future versions will target the corresponding sprockets release line.
143
+
144
+ The minor and patch version will be updated according to [semver](http://semver.org/).
145
+
146
+ * Any new APIs or config options that don't break compatibility will be in a minor release
147
+ * Any time the sprockets depedency is bumped, there will be a new minor release
148
+ * Simple bug fixes will be patch releases
149
+
150
+
151
+ ## License
152
+
153
+ Copyright © 2012 Joshua Peek.
154
+
155
+ Released under the MIT license. See `LICENSE` for details.
@@ -0,0 +1,3 @@
1
+ if defined? Rails::Railtie
2
+ require 'sprockets/railtie'
3
+ end
@@ -0,0 +1,145 @@
1
+ require 'action_view'
2
+ require 'sprockets'
3
+ require 'active_support/core_ext/class/attribute'
4
+
5
+ module Sprockets
6
+ module Rails
7
+ module Helper
8
+ if defined? ActionView::Helpers::AssetUrlHelper
9
+ include ActionView::Helpers::AssetUrlHelper
10
+ else
11
+ require 'sprockets/rails/legacy_asset_tag_helper'
12
+ require 'sprockets/rails/legacy_asset_url_helper'
13
+ include LegacyAssetTagHelper
14
+ include LegacyAssetUrlHelper
15
+ end
16
+
17
+ VIEW_ACCESSORS = [:assets_environment, :assets_manifest,
18
+ :assets_prefix, :digest_assets, :debug_assets]
19
+
20
+ def self.included(klass)
21
+ if klass < Sprockets::Context
22
+ klass.class_eval do
23
+ alias_method :assets_environment, :environment
24
+ def assets_manifest; end
25
+ class_attribute :config, :assets_prefix, :digest_assets
26
+ end
27
+ else
28
+ klass.class_attribute(*VIEW_ACCESSORS)
29
+ end
30
+ end
31
+
32
+ def self.extended(obj)
33
+ obj.class_eval do
34
+ attr_accessor(*VIEW_ACCESSORS)
35
+ end
36
+ end
37
+
38
+ def compute_asset_path(path, options = {})
39
+ if digest_path = asset_digest_path(path)
40
+ path = digest_path if digest_assets
41
+ path += "?body=1" if options[:debug]
42
+ File.join(assets_prefix || "/", path)
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ # Get digest for asset path.
49
+ #
50
+ # path - String path
51
+ # options - Hash options
52
+ #
53
+ # Returns String Hex digest or nil if digests are disabled.
54
+ def asset_digest(path, options = {})
55
+ return unless digest_assets
56
+
57
+ if digest_path = asset_digest_path(path, options)
58
+ digest_path[/-(.+)\./, 1]
59
+ end
60
+ end
61
+
62
+ # Expand asset path to digested form.
63
+ #
64
+ # path - String path
65
+ # options - Hash options
66
+ #
67
+ # Returns String path or nil if no asset was found.
68
+ def asset_digest_path(path, options = {})
69
+ if manifest = assets_manifest
70
+ if digest_path = manifest.assets[path]
71
+ return digest_path
72
+ end
73
+ end
74
+
75
+ if environment = assets_environment
76
+ if asset = environment[path]
77
+ return asset.digest_path
78
+ end
79
+ end
80
+ end
81
+
82
+ # Override javascript tag helper to provide debugging support.
83
+ #
84
+ # Eventually will be deprecated and replaced by source maps.
85
+ def javascript_include_tag(*sources)
86
+ options = sources.extract_options!.stringify_keys
87
+
88
+ if request_debug_assets?
89
+ sources.map { |source|
90
+ if asset = lookup_asset_for_path(source, :type => :javascript)
91
+ asset.to_a.map do |a|
92
+ super(path_to_javascript(a.logical_path, :debug => true), options)
93
+ end
94
+ else
95
+ super(source, options)
96
+ end
97
+ }.flatten.uniq.join("\n").html_safe
98
+ else
99
+ sources.push(options)
100
+ super(*sources)
101
+ end
102
+ end
103
+
104
+ # Override stylesheet tag helper to provide debugging support.
105
+ #
106
+ # Eventually will be deprecated and replaced by source maps.
107
+ def stylesheet_link_tag(*sources)
108
+ options = sources.extract_options!.stringify_keys
109
+
110
+ if request_debug_assets?
111
+ sources.map { |source|
112
+ if asset = lookup_asset_for_path(source, :type => :stylesheet)
113
+ asset.to_a.map do |a|
114
+ super(path_to_stylesheet(a.logical_path, :debug => true), options)
115
+ end
116
+ else
117
+ super(source, options)
118
+ end
119
+ }.flatten.uniq.join("\n").html_safe
120
+ else
121
+ sources.push(options)
122
+ super(*sources)
123
+ end
124
+ end
125
+
126
+ protected
127
+ # Enable split asset debugging. Eventually will be deprecated
128
+ # and replaced by source maps in Sprockets 3.x.
129
+ def request_debug_assets?
130
+ debug_assets || (defined?(controller) && controller && params[:debug_assets])
131
+ end
132
+
133
+ # Internal method to support multifile debugging. Will
134
+ # eventually be removed w/ Sprockets 3.x.
135
+ def lookup_asset_for_path(path, options = {})
136
+ return unless env = assets_environment
137
+ path = path.to_s
138
+ if extname = compute_asset_extname(path, options)
139
+ path = "#{path}#{extname}"
140
+ end
141
+ env[path]
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,32 @@
1
+ require 'sprockets'
2
+
3
+ module Sprockets
4
+ module Rails
5
+ # Backports of AssetTagHelper methods for Rails 2.x and 3.x.
6
+ module LegacyAssetTagHelper
7
+ include ActionView::Helpers::TagHelper
8
+
9
+ def javascript_include_tag(*sources)
10
+ options = sources.extract_options!.stringify_keys
11
+ sources.uniq.map { |source|
12
+ tag_options = {
13
+ "src" => path_to_javascript(source)
14
+ }.merge(options)
15
+ content_tag(:script, "", tag_options)
16
+ }.join("\n").html_safe
17
+ end
18
+
19
+ def stylesheet_link_tag(*sources)
20
+ options = sources.extract_options!.stringify_keys
21
+ sources.uniq.map { |source|
22
+ tag_options = {
23
+ "rel" => "stylesheet",
24
+ "media" => "screen",
25
+ "href" => path_to_stylesheet(source)
26
+ }.merge(options)
27
+ tag(:link, tag_options)
28
+ }.join("\n").html_safe
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,130 @@
1
+ require 'sprockets'
2
+
3
+ module Sprockets
4
+ module Rails
5
+ # Backports of AssetUrlHelper methods for Rails 2.x and 3.x.
6
+ module LegacyAssetUrlHelper
7
+ URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
8
+
9
+ def asset_path(source, options = {})
10
+ source = source.to_s
11
+ return "" unless source.present?
12
+ return source if source =~ URI_REGEXP
13
+
14
+ tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
15
+
16
+ if extname = compute_asset_extname(source, options)
17
+ source = "#{source}#{extname}"
18
+ end
19
+
20
+ if source[0] != ?/
21
+ source = compute_asset_path(source, options)
22
+ end
23
+
24
+ relative_url_root = (defined?(config.relative_url_root) && config.relative_url_root) ||
25
+ (respond_to?(:request) && request.try(:script_name))
26
+ if relative_url_root
27
+ source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
28
+ end
29
+
30
+ if host = compute_asset_host(source, options)
31
+ source = "#{host}#{source}"
32
+ end
33
+
34
+ "#{source}#{tail}"
35
+ end
36
+ alias_method :path_to_asset, :asset_path
37
+
38
+ ASSET_EXTENSIONS = {
39
+ :javascript => '.js',
40
+ :stylesheet => '.css'
41
+ }
42
+
43
+ def compute_asset_extname(source, options = {})
44
+ return if options[:extname] == false
45
+ extname = options[:extname] || ASSET_EXTENSIONS[options[:type]]
46
+ extname if extname && File.extname(source) != extname
47
+ end
48
+
49
+ ASSET_PUBLIC_DIRECTORIES = {
50
+ :audio => '/audios',
51
+ :font => '/fonts',
52
+ :image => '/images',
53
+ :javascript => '/javascripts',
54
+ :stylesheet => '/stylesheets',
55
+ :video => '/videos'
56
+ }
57
+
58
+ def compute_asset_path(source, options = {})
59
+ dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
60
+ File.join(dir, source)
61
+ end
62
+
63
+ def compute_asset_host(source = "", options = {})
64
+ request = self.request if respond_to?(:request)
65
+
66
+ if defined? config
67
+ host = config.asset_host
68
+ elsif defined? ActionController::Base.asset_host
69
+ host = ActionController::Base.asset_host
70
+ end
71
+
72
+ host ||= request.base_url if request && options[:protocol] == :request
73
+ return unless host
74
+
75
+ if host.respond_to?(:call)
76
+ arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity
77
+ args = [source]
78
+ args << request if request && (arity > 1 || arity < 0)
79
+ host = host.call(*args)
80
+ elsif host =~ /%d/
81
+ host = host % (Zlib.crc32(source) % 4)
82
+ end
83
+
84
+ if host =~ URI_REGEXP
85
+ host
86
+ else
87
+ protocol = options[:protocol] || (request ? :request : :relative)
88
+ case protocol
89
+ when :relative
90
+ "//#{host}"
91
+ when :request
92
+ "#{request.protocol}#{host}"
93
+ else
94
+ "#{protocol}://#{host}"
95
+ end
96
+ end
97
+ end
98
+
99
+ def javascript_path(source, options = {})
100
+ path_to_asset(source, {:type => :javascript}.merge(options))
101
+ end
102
+ alias_method :path_to_javascript, :javascript_path
103
+
104
+ def stylesheet_path(source, options = {})
105
+ path_to_asset(source, {:type => :stylesheet}.merge(options))
106
+ end
107
+ alias_method :path_to_stylesheet, :stylesheet_path
108
+
109
+ def image_path(source, options = {})
110
+ path_to_asset(source, {:type => :image}.merge(options))
111
+ end
112
+ alias_method :path_to_image, :image_path
113
+
114
+ def video_path(source, options = {})
115
+ path_to_asset(source, {:type => :video}.merge(options))
116
+ end
117
+ alias_method :path_to_video, :video_path
118
+
119
+ def audio_path(source, options = {})
120
+ path_to_asset(source, {:type => :audio}.merge(options))
121
+ end
122
+ alias_method :path_to_audio, :audio_path
123
+
124
+ def font_path(source, options = {})
125
+ path_to_asset(source, {:type => :font}.merge(options))
126
+ end
127
+ alias_method :path_to_font, :font_path
128
+ end
129
+ end
130
+ end