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

Commits on Apr 7, 2014

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    13cb652 View commit details
  2. Copy the full SHA
    942e90f View commit details
Showing with 38 additions and 8 deletions.
  1. +9 −3 lib/opal/new_builder.rb
  2. +6 −4 lib/opal/sprockets/processor.rb
  3. +19 −0 spec/cli/new_builder_spec.rb
  4. +4 −1 stdlib/file.rb
12 changes: 9 additions & 3 deletions lib/opal/new_builder.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ class NewBuilder
def initialize(options = {}, path_reader = PathReader.new, compiler_class = CompilerWrapper)
@options = options
@compiler_options = options[:compiler_options] || {}
@stubbed_files = options[:stubbed_files] || []
@path_reader = path_reader
@compiler_class = compiler_class
end
@@ -20,7 +21,9 @@ def build_str(source, path = '(file)', prerequired = [])
sources = []
compiled_requires = {}
prerequired.each {|pr| compiled_requires[pr] = true}

compiler.requires.uniq.each { |r| compile_require(r, sources, compiled_requires) }

sources << compiler.compiled
prerequired.concat(compiled_requires.keys)
sources.join("\n")
@@ -36,8 +39,7 @@ def javascript? path
def compile_require r, sources, compiled_requires
return if compiled_requires.has_key?(r)
compiled_requires[r] = true

require_source = path_reader.read(r)
require_source = stubbed?(r) ? '' : path_reader.read(r)
if javascript?(r)
sources << require_source
else
@@ -47,11 +49,15 @@ def compile_require r, sources, compiled_requires
end
end

def stubbed? file
stubbed_files.include? file
end

def compiler_for(source, options = {})
compiler_class.new(source, compiler_options.merge(options))
end

attr_reader :compiler_class, :path_reader, :compiler_options
attr_reader :compiler_class, :path_reader, :compiler_options, :stubbed_files

class CompilerWrapper
def initialize(source, options)
10 changes: 6 additions & 4 deletions lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -78,10 +78,12 @@ def evaluate(context, locals, &block)
}

path = context.logical_path
requires = stubbed_files.dup.to_a
builder = NewBuilder.new({compiler_options: options})
result = builder.build_str(data, path, requires)
(requires - stubbed_files.to_a).uniq.each { |r| context.depend_on path }
prerequired = []
builder = NewBuilder.new(:compiler_options => options, :stubbed_files => stubbed_files)
result = builder.build_str(data, path, prerequired)

# prerequired is mutated by the builder
prerequired.uniq.each { |r| context.depend_on path }

if self.class.source_map_enabled
$OPAL_SOURCE_MAPS[context.pathname] = '' #compiler.source_map(source_file_url(context)).to_s
19 changes: 19 additions & 0 deletions spec/cli/new_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -74,6 +74,25 @@
end
end

context 'with a stubbed file' do
let(:foo_stubbed) { 'foo stubbed' }

before do
options.merge! stubbed_files: [foo_path]
foo_compiler = double(:compile, :compiled => foo_stubbed, :requires => [])
compiler_class.stub(:new).with('', :file => foo_path, :requirable => true) { foo_compiler }
end

it 'returns an empty source' do
expect(builder.build(filepath)).to eq([
foo_stubbed,
required_bar,
compiled_source,
].join("\n"))
end
end


include_examples :path_reader do
let(:path) {'foo'}
let(:contents) { foo_contents }
5 changes: 4 additions & 1 deletion stdlib/file.rb
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@ class File
SEPARATOR = '/'

def self.expand_path(path, basedir = nil)
[basedir || '.', path].join(SEPARATOR).gsub(%r{/[^/]+/\.\./}, '/')
expand_regexp = %r{(/|^)[^/]+/\.\./}
full_path = [basedir || '.', path].join(SEPARATOR)
full_path = full_path.gsub(expand_regexp, '\1') while full_path =~ expand_regexp
full_path
end
end