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: 6ace2f614a17
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e388357710a1
Choose a head ref
  • 7 commits
  • 9 files changed
  • 1 contributor

Commits on Apr 18, 2015

  1. Copy the full SHA
    1cb911f View commit details
  2. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    e032746 View commit details
  3. Manually compute logical_path

    fixes rails/sprockets#29
    
    In Sprockets 3 logical_path has an odd behavior when the filename is "index"
    thus we need to bake our own logical_path.
    elia committed Apr 18, 2015
    Copy the full SHA
    1071233 View commit details
  4. Copy the full SHA
    68418bb View commit details
  5. Copy the full SHA
    7902864 View commit details
  6. Branch require_tree processor code for sprockets 3

    Alas, this sucks and needs some refactoring.
    elia committed Apr 18, 2015
    Copy the full SHA
    09f6968 View commit details
  7. Copy the full SHA
    e388357 View commit details
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@ matrix:
allow_failures:
- rvm: 1.8.7
- rvm: 1.9.3
- rvm: 2.0.0
- rvm: rbx
- rvm: jruby
- rvm: jruby-head
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Bundler.require
require 'mspec/opal/rake_task'

::Opal::Processor.arity_check_enabled = true
::Opal::Processor.dynamic_require_severity = :raise
::Opal::Processor.dynamic_require_severity = :error

use Rack::ShowExceptions
use Rack::ShowStatus
2 changes: 1 addition & 1 deletion lib/mspec/opal/rake_task.rb
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ class Runner
def initialize &block
@app = Rack::Builder.new do
::Opal::Processor.arity_check_enabled = true
::Opal::Processor.dynamic_require_severity = :raise
::Opal::Processor.dynamic_require_severity = :error

use Rack::ShowExceptions
use Rack::ShowStatus
5 changes: 4 additions & 1 deletion lib/opal/builder_processors.rb
Original file line number Diff line number Diff line change
@@ -102,7 +102,10 @@ def requires
end

def required_trees
compiled.required_trees
compiled.required_trees.map do |tree|
# Remove any leading ./ after joining to dirname
File.join(File.dirname(@filename), tree).sub(%r{^(\./)*}, '')
end
end

def compiler_class
5 changes: 3 additions & 2 deletions lib/opal/nodes/call.rb
Original file line number Diff line number Diff line change
@@ -192,10 +192,11 @@ def compile_default?
add_special :require_tree do
arg = arglist[1]
if arg[0] == :str
dir = File.dirname(compiler.file)
relative_path = arg[1]
compiler.required_trees << relative_path

dir = File.dirname(compiler.file)
full_path = Pathname(dir).join(relative_path).cleanpath.to_s
compiler.required_trees << full_path
arg[1] = full_path
end
compile_default!
61 changes: 50 additions & 11 deletions lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -69,8 +69,12 @@ class << self
def evaluate(context, locals, &block)
return Opal.compile data, file: file unless context.is_a? ::Sprockets::Context

sprockets = context.environment
logical_path = context.logical_path
@sprockets = sprockets = context.environment

# In Sprockets 3 logical_path has an odd behavior when the filename is "index"
# thus we need to bake our own logical_path
logical_path = context.filename.sub(%r{^#{context.root_path}/?(.*?)#{sprockets_extnames_regexp}}, '\1')

compiler_options = self.compiler_options.merge(file: logical_path)

# Opal will be loaded immediately to as the runtime redefines some crucial
@@ -95,6 +99,15 @@ def evaluate(context, locals, &block)
result.to_s
end

def self.sprockets_extnames_regexp(sprockets)
joined_extnames = sprockets.engines.keys.map { |ext| Regexp.escape(ext) }.join('|')
Regexp.new("(#{joined_extnames})*$")
end

def sprockets_extnames_regexp
@sprockets_extnames_regexp ||= self.class.sprockets_extnames_regexp(@sprockets)
end

def compiler_options
# Not using self.class because otherwise would check subclasses for
# attr_accessors they have but are not set.
@@ -103,6 +116,7 @@ def compiler_options

def process_requires(requires, context)
requires.each do |required|
required = required.sub(sprockets_extnames_regexp, '')
context.require_asset required unless stubbed_files.include? required
end
end
@@ -123,21 +137,46 @@ def process_required_trees(required_trees, context)
raise ArgumentError, "require_tree argument must be a relative path: #{required_tree.inspect}"
end

required_tree = dirname.join(required_tree)
required_tree = dirname.join(file, '..', required_tree)

unless required_tree.directory?
raise ArgumentError, "require_tree argument must be a directory: #{[original_required_tree, required_tree].inspect}"
end

context.depend_on required_tree
context.depend_on required_tree.to_s

context.environment.each_entry(required_tree) do |pathname|
if pathname.to_s == file
next
elsif pathname.directory?
context.depend_on(pathname)
elsif context.asset_requirable?(pathname)
context.require_asset(pathname)
environment = context.environment

if environment.respond_to?(:each_entry)
# Sprockets 2
environment.each_entry(required_tree) do |pathname|
if pathname.to_s == file
next
elsif pathname.directory?
context.depend_on(pathname)
elsif context.asset_requirable?(pathname)
context.require_asset(pathname)
end
end
else
# Sprockets 3
processor = ::Sprockets::DirectiveProcessor.new
processor.instance_variable_set('@dirname', File.dirname(file))
processor.instance_variable_set('@environment', environment)
path = processor.__send__(:expand_relative_dirname, :require_tree, original_required_tree)
absolute_paths = environment.__send__(:stat_sorted_tree_with_dependencies, path).first.map(&:first)

absolute_paths.each do |path|
path = Pathname(path)
pathname = path.relative_path_from(dirname)

if name.to_s == file
next
elsif path.directory?
context.depend_on(path.to_s)
else
context.require_asset(pathname)
end
end
end
end
10 changes: 9 additions & 1 deletion lib/opal/sprockets/server.rb
Original file line number Diff line number Diff line change
@@ -77,7 +77,15 @@ def create_app
use Rack::Deflater
use Rack::ShowExceptions
use Index, server if server.use_index
map(maps_prefix) { run maps_app } if source_map_enabled
if source_map_enabled
map(maps_prefix) do
require 'rack/conditionalget'
require 'rack/etag'
use Rack::ConditionalGet
use Rack::ETag
run maps_app
end
end
map(prefix) { run sprockets }
run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
end
13 changes: 8 additions & 5 deletions spec/lib/sprockets/erb_spec.rb
Original file line number Diff line number Diff line change
@@ -3,20 +3,23 @@

describe Opal::ERB::Processor do
let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
let(:environment) { double('environment',
let(:environment) { double(Sprockets::Environment,
cache: nil,
:[] => nil,
resolve: pathname.expand_path.to_s,
engines: double(keys: %w[.rb .js .erb .opal]),
) }
let(:_context) { double('context',
let(:sprockets_context) { double(Sprockets::Context,
logical_path: "foo.#{ext}",
environment: environment,
pathname: pathname,
filename: pathname.to_s,
root_path: '/Code/app/mylib',
is_a?: true,
) }
let(:required_assets) { [] }
let(:template) { described_class.new { |t| %Q{<a href="<%= url %>"><%= name %></a>} } }
before { _context.stub(:require_asset) {|asset| required_assets << asset } }
before { sprockets_context.stub(:require_asset) {|asset| required_assets << asset } }

let(:ext) { 'opalerb' }

@@ -25,11 +28,11 @@
end

it 'renders the template' do
expect(template.render(_context)).to include('"<a href=\""')
expect(template.render(sprockets_context)).to include('"<a href=\""')
end

it 'implicitly requires "erb"' do
template.render(_context)
template.render(sprockets_context)
expect(required_assets).to eq(['erb'])
end
end
3 changes: 3 additions & 0 deletions spec/lib/sprockets/processor_spec.rb
Original file line number Diff line number Diff line change
@@ -7,11 +7,14 @@
cache: nil,
:[] => nil,
resolve: pathname.expand_path.to_s,
engines: double(keys: %w[.rb .js .opal]),
) }
let(:sprockets_context) { double(Sprockets::Context,
logical_path: "foo.#{ext}",
environment: environment,
pathname: pathname,
filename: pathname.to_s,
root_path: '/Code/app/mylib',
is_a?: true,
) }