Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion commonjs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 12 additions & 5 deletions lib/commonjs/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 5 additions & 5 deletions lib/commonjs/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -36,7 +37,6 @@ def expand(module_id)
end.join('/')
end


class Native

attr_reader :exports
Expand All @@ -46,4 +46,4 @@ def initialize(impl)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/commonjs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CommonJS
VERSION = "0.2.7"
VERSION = "0.2.9"
end
2 changes: 1 addition & 1 deletion spec/commonjs/modules_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 26 additions & 7 deletions spec/commonjs/path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@


require 'spec_helper'

describe "load paths: " do
describe "with a single path" do
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