diff --git a/.travis.yml b/.travis.yml index 3c0c43c..80e6189 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ rvm: - 1.9.3 - - 1.9.2 - - 1.8.7 - - jruby-18mode + - 2.0 + - 2.1 + - 2.2 - jruby-19mode notifications: recipients: diff --git a/commonjs.gemspec b/commonjs.gemspec index 494a476..afa6c57 100644 --- a/commonjs.gemspec +++ b/commonjs.gemspec @@ -17,5 +17,5 @@ Gem::Specification.new do |gem| gem.version = CommonJS::VERSION gem.add_development_dependency "rake" - gem.add_development_dependency "rspec" + gem.add_development_dependency "rspec", '~> 3.2.0' end diff --git a/lib/commonjs/environment.rb b/lib/commonjs/environment.rb index aff7e97..ec68874 100644 --- a/lib/commonjs/environment.rb +++ b/lib/commonjs/environment.rb @@ -15,7 +15,8 @@ def require(module_id) filepath = find(module_id) or fail LoadError, "no such module '#{module_id}'" load_js = "( function(module, require, exports) {\n#{File.read(filepath)}\n} )" load = @runtime.eval(load_js, filepath.expand_path.to_s) - @modules[module_id] = mod = Module.new(module_id, self) + append = filepath.to_s.split('/')[-1] == 'index.js' + @modules[module_id] = mod = Module.new(module_id, self, append) load.call(mod, mod.require_function, mod.exports) end return mod.exports @@ -29,15 +30,21 @@ def new_object @runtime['Object'].new end + def add_path(path) + @paths << Pathname(path) + end + private def find(module_id) - # Add `.js` extension if neccessary. - target = if File.extname(module_id) == '.js' then module_id else "#{module_id}.js" end - if loadpath = @paths.find { |path| path.join(target).exist? } + try(module_id) || try("#{module_id}.js") || try("#{module_id}/index.js") + end + + def try(target) + if loadpath = @paths.find { |path| path.join(target).file? } loadpath.join(target) end end - + end end diff --git a/lib/commonjs/module.rb b/lib/commonjs/module.rb index 2470e91..7d75fe0 100644 --- a/lib/commonjs/module.rb +++ b/lib/commonjs/module.rb @@ -4,11 +4,12 @@ class Module attr_reader :id attr_accessor :exports - def initialize(id, env) + def initialize(id, env, append = false) @id = id @env = env @exports = env.new_object - @segments = id.split('/') + @segments_init = @segments = id.split('/') + @segments_init << [] if append end def require_function @@ -23,7 +24,7 @@ def require_function def expand(module_id) return module_id unless module_id =~ /(\.|\..)/ - module_id.split('/').inject(@segments[0..-2]) do |path, element| + module_id.split('/').inject(@segments_init[0..-2]) do |path, element| path.tap do if element == '.' #do nothing @@ -36,7 +37,6 @@ def expand(module_id) end.join('/') end - class Native attr_reader :exports @@ -46,4 +46,4 @@ def initialize(impl) end end end -end \ No newline at end of file +end diff --git a/lib/commonjs/version.rb b/lib/commonjs/version.rb index eebcced..9829b50 100644 --- a/lib/commonjs/version.rb +++ b/lib/commonjs/version.rb @@ -1,3 +1,3 @@ module CommonJS - VERSION = "0.2.7" + VERSION = "0.2.9" end diff --git a/spec/commonjs/modules_extensions_spec.rb b/spec/commonjs/modules_extensions_spec.rb index 86e1f3b..c92eaaf 100644 --- a/spec/commonjs/modules_extensions_spec.rb +++ b/spec/commonjs/modules_extensions_spec.rb @@ -5,6 +5,6 @@ @env = env_with_path_value File.expand_path('../libjs', __FILE__) end it "allows the exports object to be completely replaced" do - @env.require('assign_module_exports').call().should eql "I am your exports" + expect(@env.require('assign_module_exports').call()).to eql "I am your exports" end end \ No newline at end of file diff --git a/spec/commonjs/path_spec.rb b/spec/commonjs/path_spec.rb index a5c46b7..d6ef3ec 100644 --- a/spec/commonjs/path_spec.rb +++ b/spec/commonjs/path_spec.rb @@ -1,3 +1,5 @@ + + require 'spec_helper' describe "load paths: " do @@ -5,29 +7,46 @@ before do @env = env_with_path_value File.expand_path('../libjs', __FILE__) end - + it "finds modules in that path" do - @env.require('one').one.should eql 'one' + expect(@env.require('one').one).to eql 'one' end it "fails when a module is not in the path" do - lambda { @env.require('not_here') }.should raise_error LoadError + expect {@env.require('not_here')}.to raise_error LoadError end end - + describe "with multilpe paths" do before do @env = env_with_path_value [File.expand_path('../libjs2', __FILE__), File.expand_path('../libjs', __FILE__)] end - + + it "finds modules in both paths" do + expect(@env.require('two').two).to eql 2 + expect(@env.require('three').three).to eql 'three' + end + + it "respects the order in which paths were specified" do + expect(@env.require('one').one.to_i).to eql 1 + end + end + + describe "adding paths" do + before do + @env = env_with_path_value File.expand_path('../libjs2', __FILE__) + @env.add_path(File.expand_path('../libjs', __FILE__)) + end + it "finds modules in both paths" do @env.require('two').two.should eql 2 @env.require('three').three.should eql 'three' end - + it "respects the order in which paths were specified" do @env.require('one').one.to_i.should eql 1 end - end + end + end