Module: Marfa::Helpers::Javascript

Defined in:
lib/marfa/helpers/javascript.rb

Overview

Javascript module

Instance Method Summary collapse

Instance Method Details

#js_import(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/marfa/helpers/javascript.rb', line 19

def js_import(path)
  path = settings.views + path

  closure = Closure::Compiler.new(
    compilation_level: 'SIMPLE_OPTIMIZATIONS',
    language_out: 'ES5_STRICT'
  )

  code = js_transpile(path, false)
  code = closure.compile(code) if Marfa.config.minify_js
  '<script>' + code + '</script>'
end

#js_import_from_haml(path, data = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/marfa/helpers/javascript.rb', line 33

def js_import_from_haml(path, data = {})
  closure = Closure::Compiler.new(
    compilation_level: 'SIMPLE_OPTIMIZATIONS',
    language_out: 'ES5_STRICT'
  )

  template = haml :"#{path}", :layout => false, :locals => data
  code = js_transpile(template)
  code = closure.compile(code)
  '<script>' + code + '</script>'
end

#js_transpile(path, is_plain_text = true) ⇒ Object



12
13
14
15
16
# File 'lib/marfa/helpers/javascript.rb', line 12

def js_transpile(path, is_plain_text = true)
  path = File.read(path) unless is_plain_text
  result = Babel::Transpiler.transform(path)
  result['code']
end