Class: Less::Loader

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

Defined Under Namespace

Classes: Console, Fs, Path, Process, Sys

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



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

def initialize
  @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

Instance Method Details

#require(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/less/loader.rb', line 17

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?
    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
  return exports
end