Class: Embork::Sprockets::ES6ModuleTranspiler

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/embork/sprockets/es6_module_transpiler.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.compile_toObject

Returns the value of attribute compile_to.



11
12
13
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 11

def compile_to
  @compile_to
end

.namespaceObject

Returns the value of attribute namespace.



13
14
15
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 13

def namespace
  @namespace
end

.transformObject

Returns the value of attribute transform.



12
13
14
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 12

def transform
  @transform
end

Class Method Details

.runner_pathObject



19
20
21
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 19

def runner_path
  File.expand_path '../support/node_runner.js', __FILE__
end

.runtimeObject



23
24
25
26
27
28
29
30
# 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_pathObject



15
16
17
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 15

def transpiler_path
  File.expand_path '../support/es6-module-transpiler.js', __FILE__
end

Instance Method Details

#component?Boolean

Returns:

  • (Boolean)


73
74
75
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 81

def fixture?
  !!@logical_path.match(/^fixtures/)
end

#manifest?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 69

def manifest?
  !!path_relative_to_root.to_s.match(/^config/)
end

#module_generatorObject



122
123
124
125
126
127
128
# 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_nameObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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_typeObject



118
119
120
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 118

def module_type
  self.class.compile_to.to_s.upcase
end

#path_relative_to_rootObject



59
60
61
62
63
64
65
66
67
# 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

#prepareObject



36
37
38
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 36

def prepare
  @logger = Embork::Logger.new(STDOUT, :simple)
end

#sourceObject



99
100
101
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 99

def source
  ::JSON.generate data, quirks_mode: true
end

#template?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 77

def template?
  !!@logical_path.match(/^templates/)
end

#wrap_in_closure(compiled_code) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# 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