module Sprockets::Babel

Constants

VERSION

Public Class Methods

context() click to toggle source
# 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 = {}) click to toggle source
# 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

Private Class Methods

resolve_relative_module_id(source_module_id, target_module_id) click to toggle source
# File lib/sprockets/babel.rb, line 191
def self.resolve_relative_module_id(source_module_id, target_module_id)
  target_path_parts = target_module_id.split(/\//)
  basename = target_path_parts.pop
  return target_module_id if target_path_parts.length == 0
  return target_module_id if target_path_parts[0] != '.' && target_path_parts[0] != '..'
  target_path_parts.shift if target_path_parts[0] == '.'

  source_path_parts = source_module_id.split(/\//)
  source_path_parts.pop # ignore filename part
  while target_path_parts.length > 0 && target_path_parts[0] == '..'
    target_path_parts.shift
    source_path_parts.pop if source_path_parts.length > 0
  end
  parts = source_path_parts.concat(target_path_parts)
  if parts.length == 0
    return basename
  end
  parts.join('/') + '/' + basename
end
transform_inline(code, options) click to toggle source
# File lib/sprockets/babel.rb, line 131
    def self.transform_inline(code, options)
      module_var = '$__' + ERB::Util.url_encode(options[:moduleId].gsub(/^\.\//, ''))
        .gsub(/%|-/, '') + '__'
      prefix = <<-JS
        (function(global) {
          var exports = {};
          global.#{module_var} = exports;

          function define(moduleId, importIds, body) {
            function resolveRelativeModuleId(targetId) {
              var targetParts = targetId.split(/\\//),
                basename = targetParts.pop();
              if ((targetParts.length == 0) || (targetParts[0] != '.' && targetParts[0] != '..')) {
                return targetId;
              }
              if (targetParts[0] == '.') {
                targetParts.shift();
              }

              var fromParts = moduleId.split(/\\//);
              fromParts.pop();
              while (targetParts.length > 0 && targetParts[0] == '..') {
                targetParts.shift();
                if (fromParts.length > 0) {
                  fromParts.pop();
                }
              }
              var parts = fromParts.concat(targetParts);
              return (parts.length == 0) ? basename : parts.join('/') + '/' + basename;
            };

            importIds.shift();
            var imports = [exports],
              module = {exports: null};
            for (var i = 0; i < importIds.length; i++) {
              if (importIds[i] == 'module') {
                imports.push(module);
                continue;
              }

              var importId = resolveRelativeModuleId(importIds[i]),
                variable = '$__' + encodeURIComponent(importId.replace(/^\\.\\//, ''))
                  .replace(/%|-/g, '') + '__';
              imports.push(global[variable]);
            }
            body.apply(global, imports);
            if (module.exports != null) {
              global.#{module_var} = module.exports;
            }
          };
      JS

      # code transpiled by babel is inserted here

      suffix = <<-JS
        })(this);
      JS
      prefix + "\n" + code + "\n" + suffix
    end