Class: Requirejs::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/requirejs/runtime/runtime.rb

Overview

Internal: Module responsible for the ExecJS interaction. Besides handling the compilation execution, this module provide a runtime validation to ensure that the Node.JS binary is available to use.

Instance Method Summary collapse

Constructor Details

#initialize(build_script) ⇒ Runtime

Internal: Module responsible for the ExecJS interaction. Besides handling the compilation execution, this module provide a runtime validation to ensure that the Node.JS binary is available to use.



12
13
14
# File 'lib/requirejs/runtime/runtime.rb', line 12

def initialize(build_script)
  @build_script = build_script
end

Instance Method Details

#check_availability!Object

private Internal: Queries the runtime for it’s availability and raises a ‘RuntimeError’ if the runtime isn’t available. Otherwise, this is a noop.



30
31
32
33
34
35
36
# File 'lib/requirejs/runtime/runtime.rb', line 30

def check_availability!
  unless runtime.available?
    message = 'The Node.JS runtime is not available to RequireJS.'
    message << 'Ensure that the "node" (or "nodejs") executable is present in your $PATH.'
    raise RuntimeError, message
  end
end

#contextObject

Internal: Compile the Stylus compilation script into a execution context to execute functions into.

Returns the compiled context.



42
43
44
# File 'lib/requirejs/runtime/runtime.rb', line 42

def context
  @context ||= runtime.compile(@build_script)
end

#exec(*arguments) ⇒ Object

Internal: Calls a specific function on the Node.JS context.

Example

exec('version', 2) # => '2'

Returns The function returned value.



22
23
24
25
# File 'lib/requirejs/runtime/runtime.rb', line 22

def exec(*arguments)
  check_availability!
  context.call('optimize')
end

#runtimeObject

Internal: Create the ExecJS external runtime with a old runner script that maintains the state of ‘require’, so we can use it to load modules like on any Node.JS program.



49
50
51
52
53
54
55
56
57
# File 'lib/requirejs/runtime/runtime.rb', line 49

def runtime
  #@runtime ||= ExecJS::Runtimes::Node
  @runtime ||= ExecJS::ExternalRuntime.new(
      :name => 'Node.js (V8)',
      :command => %w{nodejs node},
      :runner_path => File.expand_path('../node_runner.js', __FILE__),
      :encoding => 'UTF-8'
  )
end