Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ff96c75edc99
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 43aa49956686
Choose a head ref
  • 7 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 22, 2015

  1. Remove meaningless spec

    That was copied as is from the erb tilt template.
    elia committed Mar 22, 2015
    Copy the full SHA
    affc060 View commit details
  2. Cleanup processor spec setup

    elia committed Mar 22, 2015
    Copy the full SHA
    28f9032 View commit details
  3. Copy the full SHA
    2fa1880 View commit details
  4. Copy the full SHA
    ec2d51d View commit details
  5. Stubbed files should just be marked as required

    Sometimes files stubbed in opal don’t exist at all, but sprockets would
    require them to be there all the same, for this reason we can’t just 
    forward stubs to sprockets.
    elia committed Mar 22, 2015
    Copy the full SHA
    898f99e View commit details
  6. Use a simple interpolated string as default index

    Instead of a complex ERB rendering of a constant with current `binding`.
    elia committed Mar 22, 2015
    Copy the full SHA
    2a37c46 View commit details
  7. Fix Processor.load_asset_code with FP*

    The amount of FP here probably calls out for a new class with its own 
    private methods.
    
    * Functional Programming
    elia committed Mar 22, 2015
    Copy the full SHA
    43aa499 View commit details
Showing with 69 additions and 38 deletions.
  1. +14 −12 lib/opal/sprockets/processor.rb
  2. +14 −12 lib/opal/sprockets/server.rb
  3. +41 −14 spec/lib/sprockets/processor_spec.rb
26 changes: 14 additions & 12 deletions lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ def evaluate(context, locals, &block)
result = compiler.compile

compiler.requires.each do |required|
context.require_asset required
context.require_asset required unless stubbed_files.include? required
end

if self.class.source_map_enabled
@@ -88,28 +88,30 @@ def self.load_asset_code(sprockets, name)
asset = sprockets[name.sub(/(\.js)?$/, '.js')]
return '' if asset.nil?

module_name = -> asset { asset.logical_path.sub(/\.js$/, '').inspect }
module_name = -> asset { asset.logical_path.sub(/\.js$/, '') }
mark_as_loaded = -> path { "Opal.mark_as_loaded(#{path.inspect});" }
processed_by_opal = -> asset, sprockets {
attributes = ::Sprockets::AssetAttributes.new(sprockets, asset.pathname)
attributes.engines.any? { |engine| engine <= ::Opal::Processor }
}

non_opal_assets = ([asset]+asset.dependencies).select do |a|
asset_engines = ::Sprockets::AssetAttributes.new(sprockets, a.pathname).engines
processed_by_opal = asset_engines.any? { |engine| engine <= ::Opal::Processor }
not(processed_by_opal)
end
non_opal_assets = ([asset]+asset.dependencies)
.select { |asset| not(processed_by_opal[asset, sprockets]) }
.map { |asset| module_name[asset] }

mark_as_loaded = non_opal_assets.map do |asset|
"Opal.mark_as_loaded(#{module_name[asset]});"
end
mark_as_loaded = (non_opal_assets + stubbed_files.to_a)
.map { |path| mark_as_loaded[path] }

<<-JS
if (typeof(Opal) !== 'undefined') {
#{mark_as_loaded.join("\n")}
Opal.load(#{module_name[asset]});
Opal.load(#{module_name[asset].inspect});
}
JS
end

def self.stubbed_files
@stubbed_files ||= []
@stubbed_files ||= Set.new
end

def self.stub_file(name)
26 changes: 14 additions & 12 deletions lib/opal/sprockets/server.rb
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ def html
raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
Tilt.new(@index_path).render(self)
else
::ERB.new(SOURCE).result binding
source
end
end

@@ -133,17 +133,19 @@ def javascript_include_tag name
scripts.join "\n"
end

SOURCE = <<-HTML
<!DOCTYPE html>
<html>
<head>
<title>Opal Server</title>
</head>
<body>
<%= javascript_include_tag @server.main %>
</body>
</html>
HTML
def source
<<-HTML
<!DOCTYPE html>
<html>
<head>
<title>Opal Server</title>
</head>
<body>
#{javascript_include_tag @server.main}
</body>
</html>
HTML
end
end
end
end
55 changes: 41 additions & 14 deletions spec/lib/sprockets/processor_spec.rb
Original file line number Diff line number Diff line change
@@ -3,14 +3,17 @@

describe Opal::Processor do
let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
let(:_context) { double('_context', :logical_path => "foo.#{ext}", :pathname => pathname) }
let(:env) { double('env') }

before do
env.stub(:resolve) { pathname.expand_path.to_s }
env.stub(:[])
_context.stub(:environment) { env }
end
let(:environment) { double('environment',
cache: nil,
:[] => nil,
resolve: pathname.expand_path.to_s,
) }
let(:sprockets_context) { double('context',
logical_path: "foo.#{ext}",
environment: environment,
pathname: pathname,
is_a?: true,
) }

%w[rb js.rb opal js.opal].each do |ext|
let(:ext) { ext }
@@ -22,14 +25,38 @@

it "compiles and evaluates the template on #render" do
template = described_class.new { |t| "puts 'Hello, World!'\n" }
expect(template.render(_context)).to include('"Hello, World!"')
end

it "can be rendered more than once" do
template = described_class.new(_context) { |t| "puts 'Hello, World!'\n" }
3.times { expect(template.render(_context)).to include('"Hello, World!"') }
expect(template.render(sprockets_context)).to include('"Hello, World!"')
end
end
end

describe '.stubbed_files' do
around do |e|
described_class.stubbed_files.clear
e.run
described_class.stubbed_files.clear
end

let(:stubbed_file) { 'foo' }
let(:template) { described_class.new { |t| "require #{stubbed_file.inspect}" } }

it 'usually require files' do
sprockets_context.should_receive(:require_asset).with(stubbed_file)
template.render(sprockets_context)
end

it 'skips require of stubbed file' do
described_class.stub_file stubbed_file
sprockets_context.should_not_receive(:require_asset).with(stubbed_file)
template.render(sprockets_context)
end

it 'marks a stubbed file as loaded' do
described_class.stub_file stubbed_file
asset = double(dependencies: [], pathname: Pathname('bar'), logical_path: 'bar')
environment.stub(:[]).with('bar.js') { asset }
code = described_class.load_asset_code(environment, 'bar')
code.should match stubbed_file
end
end
end