Class: Less::Loader

Inherits:
Object
  • Object
show all
Includes:
CallJS
Defined in:
lib/less/loader.rb

Defined Under Namespace

Classes: Console, Fs, Path, Process, Sys

Instance Method Summary collapse

Methods included from CallJS

#calljs, #lock

Constructor Details

#initializeLoader

Returns a new instance of Loader.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/less/loader.rb', line 6

def initialize
  lock do
    @cxt = V8::Context.new
    @path = Pathname(__FILE__).dirname.join('js','lib')
    @exports = {
      "path" => Path.new,
      "sys" => Sys.new,
      "fs" => Fs.new
    }
    @process = Process.new
    @cxt['console'] = Console.new
  end
end

Instance Method Details

#require(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/less/loader.rb', line 20

def require(path)
  unless exports = @exports[path]
    filename = path =~ /\.js$/ ? path : "#{path}.js"
    filepath = @path.join(filename)
    fail LoadError, "no such file: #{filename}" unless filepath.exist?
    lock do
      load = @cxt.eval("(function(process, require, exports, __dirname) {require.paths = [];#{File.read(filepath)}})", filepath.expand_path)
      @exports[path] = exports = @cxt['Object'].new
      load.call(@process, method(:require), exports, Dir.pwd)
    end
  end
  return exports
end