Class: EJX::Template::Base

Inherits:
Node
  • Object
show all
Defined in:
lib/ejx/template/base.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#children

Instance Method Summary collapse

Methods inherited from Node

#push

Constructor Details

#initialize(**options) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/ejx/template/base.rb', line 5

def initialize(**options)
  super
  @imports = []
end

Instance Attribute Details

#importsObject

Returns the value of attribute imports.



3
4
5
# File 'lib/ejx/template/base.rb', line 3

def imports
  @imports
end

Instance Method Details

#to_moduleObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ejx/template/base.rb', line 10

def to_module
  var_generator = EJX::Template::VarGenerator.new
  
  output = if @escape
    "import {" + @escape.split('.').reverse.join(" as __ejx_append} from '") + "';\n"
  else
    "import {append as __ejx_append} from 'ejx';\n"
  end
  
  @imports.each do |import|
    output << import << "\n"
  end
  
  output << "\nexport default async function (locals) {\n"
  output << "    var __output = [], __promises = [];\n    \n"
  
  @children.each do |child|
    output << case child
    when EJX::Template::String
      "    __output.push(#{child.to_js});\n"
    else
      child.to_js(var_generator: var_generator)
    end
  end
  
  output << "\n    await Promise.all(__promises);"

  output << "\n    return __output;"
  output << "\n}"

  output
end