Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bdbfad8

Browse files
committedMar 24, 2015
Avoid use of Sprockets::AssetAttributes (2.x only)
ref: rails/sprockets#10
1 parent 4bbe997 commit bdbfad8

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed
 

Diff for: ‎lib/opal/sprockets/processor.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,20 @@ def evaluate(context, locals, &block)
8585
end
8686

8787
def self.load_asset_code(sprockets, name)
88-
asset = sprockets[name.sub(/(\.js)?$/, '.js')]
88+
asset = sprockets[name.sub(/(\.(js|rb|opal))*$/, '.js')]
8989
return '' if asset.nil?
9090

91+
opal_extnames = sprockets.engines.map do |ext, engine|
92+
ext if engine <= ::Opal::Processor
93+
end.compact
94+
9195
module_name = -> asset { asset.logical_path.sub(/\.js$/, '') }
96+
path_extnames = -> path { File.basename(path).scan(/\.[^.]+/) }
9297
mark_as_loaded = -> path { "Opal.mark_as_loaded(#{path.inspect});" }
93-
processed_by_opal = -> asset, sprockets {
94-
attributes = ::Sprockets::AssetAttributes.new(sprockets, asset.pathname)
95-
attributes.engines.any? { |engine| engine <= ::Opal::Processor }
96-
}
98+
processed_by_opal = -> asset { (path_extnames[asset.pathname] & opal_extnames).any? }
9799

98100
non_opal_assets = ([asset]+asset.dependencies)
99-
.select { |asset| not(processed_by_opal[asset, sprockets]) }
101+
.select { |asset| not(processed_by_opal[asset]) }
100102
.map { |asset| module_name[asset] }
101103

102104
mark_as_loaded = (non_opal_assets + stubbed_files.to_a)

Diff for: ‎spec/lib/sprockets/processor_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
describe Opal::Processor do
55
let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
6-
let(:environment) { double('environment',
6+
let(:environment) { double(Sprockets::Environment,
77
cache: nil,
88
:[] => nil,
99
resolve: pathname.expand_path.to_s,
1010
) }
11-
let(:sprockets_context) { double('context',
11+
let(:sprockets_context) { double(Sprockets::Context,
1212
logical_path: "foo.#{ext}",
1313
environment: environment,
1414
pathname: pathname,
@@ -55,6 +55,8 @@
5555
described_class.stub_file stubbed_file
5656
asset = double(dependencies: [], pathname: Pathname('bar'), logical_path: 'bar')
5757
environment.stub(:[]).with('bar.js') { asset }
58+
environment.stub(:engines) { {'.rb' => described_class, '.opal' => described_class} }
59+
5860
code = described_class.load_asset_code(environment, 'bar')
5961
code.should match stubbed_file
6062
end

0 commit comments

Comments
 (0)
Please sign in to comment.