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)


67
68
69
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 67

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
# 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
    wrap_in_closure(self.class.runtime.exec module_generator)
  end
end

#manifest?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 63

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

#module_generatorObject



112
113
114
115
116
117
118
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 112

def module_generator
  "  var Compiler = require(\"%s\").Compiler;\n  var module = (new Compiler(%s, \"%s\"));\n  return module.to%s();\n  SOURCE\nend\n" % [ self.class.transpiler_path, source, module_name, module_type ]

#module_nameObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 93

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



108
109
110
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 108

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

#path_relative_to_rootObject



53
54
55
56
57
58
59
60
61
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 53

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
  # Required to be implemented by Tilt for some reason...
end

#sourceObject



89
90
91
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 89

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

#template?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 71

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

#wrap_in_closure(compiled_code) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/embork/sprockets/es6_module_transpiler.rb', line 75

def wrap_in_closure(compiled_code)
  if self.class.compile_to == :cjs
    "    window.require.define({\"%s\": function(exports, require, module) {\n\n    %s\n\n    }});\n    CJS\n  else\n    compiled_code\n  end\nend\n".strip_heredoc % [ module_name, compiled_code ]