class Embork::Sprockets::ES6ModuleTranspiler

Attributes

compile_to[RW]
namespace[RW]
transform[RW]

Public Class Methods

runner_path() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 19
def runner_path
  File.expand_path '../support/node_runner.js', __FILE__
end
runtime() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 23
def runtime
  ExecJS::ExternalRuntime.new(
    name: 'Node.js (V8)',
    command: [ 'nodejs', 'node' ],
    encoding: 'UTF-8',
    runner_path: runner_path
  )
end
transpiler_path() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 15
def transpiler_path
  File.expand_path '../support/es6-module-transpiler.js', __FILE__
end

Public Instance Methods

component?() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 73
def component?
  !!path_relative_to_root.to_s.match(/^components/)
end
evaluate(scope, locals, &block) click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 40
def evaluate(scope, locals, &block)
  @environment = scope.environment
  @logical_path = scope.logical_path
  @context = @environment.context_class.new(@environment, @logical_path, scope.pathname)

  # If this is a manifest, don't compile it
  if manifest? || component? || template?
    data
  else
    begin
      compiled_module = wrap_in_closure(self.class.runtime.exec module_generator)
      compiled_module.gsub! %("use strict";), '' if fixture?
      compiled_module
    rescue
      @logger.fatal 'ES6 Module error in file %s' % @logical_path
    end
  end
end
fixture?() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 81
def fixture?
  !!@logical_path.match(/^fixtures/)
end
manifest?() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 69
def manifest?
  !!path_relative_to_root.to_s.match(/^config/)
end
module_generator() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 122
  def module_generator
    <<-SOURCE % [ self.class.transpiler_path, source, module_name, module_type ]
    var Compiler = require("%s").Compiler;
    var module = (new Compiler(%s, "%s"));
    return module.to%s();
    SOURCE
  end
module_name() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 103
def module_name
  if self.class.transform
    name = self.class.transform.call @logical_path
  else
    name = @logical_path
  end

  # Attach the namespace
  if !self.class.namespace.nil?
    "%s/%s" % [ self.class.namespace.to_s, name ]
  else
    name
  end
end
module_type() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 118
def module_type
  self.class.compile_to.to_s.upcase
end
path_relative_to_root() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 59
def path_relative_to_root
  if @path_relative_to_root
    @path_relative_to_root
  else
    file_root_pathname = Pathname.new(@context.root_path)
    environment_root_pathname = Pathname.new(@environment.root)
    @path_relative_to_root = file_root_pathname.relative_path_from environment_root_pathname
  end
end
prepare() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 36
def prepare
  @logger = Embork::Logger.new(STDOUT, :simple)
end
source() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 99
def source
  ::JSON.generate data, quirks_mode: true
end
template?() click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 77
def template?
  !!@logical_path.match(/^templates/)
end
wrap_in_closure(compiled_code) click to toggle source
# File lib/embork/sprockets/es6_module_transpiler.rb, line 85
  def wrap_in_closure(compiled_code)
    if self.class.compile_to == :cjs
      <<-CJS.strip_heredoc % [ module_name, compiled_code ]
      window.require.define({"%s": function(exports, require, module) {

      %s

      }});
      CJS
    else
      compiled_code
    end
  end