Skip to content

Commit 6389649

Browse files
committed
Merge pull request #259 from Andreis13/sprockets-4
Add support for Sprockets 4.x.
2 parents 34ae184 + fb6f9c6 commit 6389649

File tree

13 files changed

+252
-126
lines changed

13 files changed

+252
-126
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ gemfile:
1414
- gemfiles/Gemfile.rails-4.1.x
1515
- gemfiles/Gemfile.rails-4.2.x
1616
- gemfiles/Gemfile.rails-5.0.x
17+
- gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
1718

1819
matrix:
1920
exclude:
@@ -29,6 +30,12 @@ matrix:
2930
rvm: 2.0.0
3031
- gemfile: gemfiles/Gemfile.rails-5.0.x
3132
rvm: 2.1
33+
- gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
34+
rvm: 1.9.3
35+
- gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
36+
rvm: 2.0.0
37+
- gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
38+
rvm: 2.1
3239

3340
notifications:
3441
email: false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
gemspec :path => ".."
3+
4+
gem "actionpack", :github => "rails/rails", :branch => "master"
5+
gem "railties", :github => "rails/rails", :branch => "master"
6+
gem "sprockets", :github => "rails/sprockets", :branch => "4.x"

lib/sprockets/rails/context.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def compute_asset_path(path, options = {})
1717
@dependencies << 'actioncontroller-asset-url-config'
1818

1919
begin
20-
asset_uri = resolve(path, compat: false)
20+
asset_uri = resolve(path)
2121
rescue FileNotFound
2222
# TODO: eh, we should be able to use a form of locate that returns
2323
# nil instead of raising an exception.
@@ -40,4 +40,9 @@ def compute_asset_path(path, options = {})
4040
[config.relative_url_root,
4141
(config.asset_host unless config.asset_host.respond_to?(:call))]
4242
end
43+
44+
# fallback to the default pipeline when using Sprockets 3.x
45+
unless config[:pipelines].include? :debug
46+
register_pipeline :debug, config[:pipelines][:default]
47+
end
4348
end

lib/sprockets/rails/helper.rb

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'action_view'
22
require 'sprockets'
33
require 'active_support/core_ext/class/attribute'
4+
require 'sprockets/rails/utils'
45

56
module Sprockets
67
module Rails
@@ -17,6 +18,7 @@ def initialize(source)
1718

1819
include ActionView::Helpers::AssetUrlHelper
1920
include ActionView::Helpers::AssetTagHelper
21+
include Sprockets::Rails::Utils
2022

2123
VIEW_ACCESSORS = [:assets_environment, :assets_manifest,
2224
:assets_precompile,
@@ -57,7 +59,7 @@ def assets_environment
5759
def compute_asset_path(path, options = {})
5860
if digest_path = asset_digest_path(path, options)
5961
path = digest_path if digest_assets
60-
path += "?body=1" if options[:debug]
62+
path += "?body=1" if options[:debug] && !using_sprockets4?
6163
File.join(assets_prefix || "/", path)
6264
else
6365
super
@@ -80,7 +82,7 @@ def asset_digest_path(path, options = {})
8082
if environment = assets_environment
8183
if asset = environment[path]
8284
unless options[:debug]
83-
if !precompiled_assets.include?(asset)
85+
if !precompiled_assets.include?(asset.logical_path)
8486
raise AssetNotPrecompiled.new(asset.logical_path)
8587
end
8688
end
@@ -135,9 +137,13 @@ def javascript_include_tag(*sources)
135137

136138
if options["debug"] != false && request_debug_assets?
137139
sources.map { |source|
138-
if asset = lookup_asset_for_path(source, :type => :javascript)
139-
asset.to_a.map do |a|
140-
super(path_to_javascript(a.logical_path, :debug => true), options)
140+
if asset = lookup_debug_asset(source, :type => :javascript)
141+
if asset.respond_to?(:to_a)
142+
asset.to_a.map do |a|
143+
super(path_to_javascript(a.logical_path, :debug => true), options)
144+
end
145+
else
146+
super(path_to_javascript(asset.logical_path, :debug => true), options)
141147
end
142148
else
143149
super(source, options)
@@ -169,9 +175,13 @@ def stylesheet_link_tag(*sources)
169175

170176
if options["debug"] != false && request_debug_assets?
171177
sources.map { |source|
172-
if asset = lookup_asset_for_path(source, :type => :stylesheet)
173-
asset.to_a.map do |a|
174-
super(path_to_stylesheet(a.logical_path, :debug => true), options)
178+
if asset = lookup_debug_asset(source, :type => :stylesheet)
179+
if asset.respond_to?(:to_a)
180+
asset.to_a.map do |a|
181+
super(path_to_stylesheet(a.logical_path, :debug => true), options)
182+
end
183+
else
184+
super(path_to_stylesheet(asset.logical_path, :debug => true), options)
175185
end
176186
else
177187
super(source, options)
@@ -201,50 +211,26 @@ def request_debug_assets?
201211

202212
# Internal method to support multifile debugging. Will
203213
# eventually be removed w/ Sprockets 3.x.
204-
def lookup_asset_for_path(path, options = {})
214+
def lookup_debug_asset(path, options = {})
205215
return unless env = assets_environment
206216
path = path.to_s
207217
if extname = compute_asset_extname(path, options)
208218
path = "#{path}#{extname}"
209219
end
210220

211-
if asset = env[path]
212-
if !precompiled_assets.include?(asset)
213-
raise AssetNotPrecompiled.new(asset.logical_path)
221+
if asset = env[path, pipeline: :debug]
222+
original_path = asset.logical_path.gsub('.debug', '')
223+
unless precompiled_assets.include?(original_path)
224+
raise AssetNotPrecompiled.new(original_path)
214225
end
215226
end
216227

217228
asset
218229
end
219230

220-
# Internal: Generate a Set of all precompiled assets.
231+
# Internal: Generate a Set of all precompiled assets logical paths.
221232
def precompiled_assets
222-
@precompiled_assets ||= begin
223-
assets = Set.new
224-
225-
paths, filters = (assets_precompile || []).flatten.partition { |arg| Sprockets::Manifest.simple_logical_path?(arg) }
226-
filters = filters.map { |arg| Sprockets::Manifest.compile_match_filter(arg) }
227-
228-
env = assets_environment.cached
229-
230-
paths.each do |path|
231-
env.find_all_linked_assets(path) do |asset|
232-
assets << asset
233-
end
234-
end
235-
236-
if filters.any?
237-
env.logical_paths do |logical_path, filename|
238-
if filters.any? { |f| f.call(logical_path, filename) }
239-
env.find_all_linked_assets(filename) do |asset|
240-
assets << asset
241-
end
242-
end
243-
end
244-
end
245-
246-
assets
247-
end
233+
@precompiled_assets ||= assets_manifest.find(assets_precompile || []).map(&:logical_path)
248234
end
249235
end
250236
end

lib/sprockets/rails/utils.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'sprockets'
2+
3+
module Sprockets
4+
module Rails
5+
module Utils
6+
def using_sprockets4?
7+
Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.0.0')
8+
end
9+
end
10+
end
11+
end

lib/sprockets/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Sprockets
22
module Rails
3-
VERSION = "3.0.0.beta1"
3+
VERSION = "3.0.0"
44
end
55
end

lib/sprockets/railtie.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Engine < Railtie
4343

4444
module Sprockets
4545
class Railtie < ::Rails::Railtie
46+
include Sprockets::Rails::Utils
47+
4648
LOOSE_APP_ASSETS = lambda do |logical_path, filename|
4749
filename.start_with?(::Rails.root.join("app/assets").to_s) &&
4850
!%w(.js .css).include?(File.extname(logical_path))
@@ -59,7 +61,11 @@ def configure(&block)
5961
config.assets.paths = []
6062
config.assets.prefix = "/assets"
6163
config.assets.manifest = nil
62-
config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
64+
if using_sprockets4?
65+
config.assets.precompile = %w( manifest.js )
66+
else
67+
config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
68+
end
6369
config.assets.version = ""
6470
config.assets.debug = false
6571
config.assets.compile = true

sprockets-rails.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
1212
s.files = Dir["README.md", "lib/**/*.rb", "LICENSE"]
1313

1414
s.required_ruby_version = '>= 1.9.3'
15-
16-
s.add_dependency "sprockets", ">= 3.0.0", "< 4.0"
15+
16+
s.add_dependency "sprockets", ">= 3.0.0"
1717
s.add_dependency "actionpack", ">= 4.0"
1818
s.add_dependency "activesupport", ">= 4.0"
1919
s.add_development_dependency "railties", ">= 4.0"

test/fixtures/manifest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// manifest.js
2+
//= link foo.css
3+
//= link foo.js
4+
//= link bar.css
5+
//= link bar.js
6+
//= link file1.css
7+
//= link file1.js
8+
//= link file2.css
9+
//= link file2.js

test/fixtures/not_precompiled.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.not-precompiled {}

0 commit comments

Comments
 (0)