Module: Sprockets::Babel

Defined in:
lib/sprockets/babel.rb,
lib/sprockets/babel/version.rb

Defined Under Namespace

Classes: Console, Template

Constant Summary collapse

VERSION =
'0.0.8'

Class Method Summary collapse

Class Method Details

.contextObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sprockets/babel.rb', line 109

def self.context
  @context ||= begin
    if defined? V8
      ctx = V8::Context.new
      ctx.eval('var self = this; ' + File.read(::Babel::Transpiler.script_path))
      ctx['console'] = Console.new
      ctx
    else
      # assume mini-racer
#          ctx = MiniRacer::Context.new
#          ctx.eval()
#          ctx.attach('console', Console.new)

      src = 'var self = this; ' + File.read(::Babel::Transpiler.script_path)
      ctx = ExecJS.compile(src)
     # ctx.attach('console', Console.new)
    end
  end
end

.transform(code, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sprockets/babel.rb', line 81

def self.transform(code, options = {})
  modules = options[:modules] || 'inline'

  if defined? V8
    babel_transform = context[:babel][:transform]
    result = babel_transform.call(
      code,
      options.merge(
        'ast' => false,
        'modules' => modules == 'inline' ? 'amd' : modules
      ))
  else
    result = context.call(
      'babel.transform',
      code,
      options.merge(
        'ast' => false,
        'modules' => modules == 'inline' ? 'amd' : modules
      ))
  end


  if modules == 'inline'
    result['code'] = transform_inline(result['code'], options)
  end
  result
end