Class: VueRails::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/vue_rails/renderer.rb

Constant Summary collapse

GLOBAL_WRAPPER =
<<-JS
    var global = global || this;
    global['process'] = global['process'] || {}
    global['process']['env']  = {
       VUE_ENV: 'server',
       NODE_ENV: 'production'
    }
    var self = self || this;
    var window = undefined;
JS
CONSOLE_POLYFILL =
<<-JS
  var console = { history: [] };
  ['error', 'log', 'info', 'warn'].forEach(function (fn) {
    console[fn] = function () {
      console.history.push({level: fn, arguments: Array.prototype.slice.call(arguments)});
    };
  });
JS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component, props, router_push_to = nil, state = {}) ⇒ Renderer

Returns a new instance of Renderer.



28
29
30
31
32
33
# File 'lib/vue_rails/renderer.rb', line 28

def initialize component, props, router_push_to=nil, state={}
  self.component = component
  self.props = props
  self.router_push_to = router_push_to
  self.state = state
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



26
27
28
# File 'lib/vue_rails/renderer.rb', line 26

def component
  @component
end

#propsObject

Returns the value of attribute props.



26
27
28
# File 'lib/vue_rails/renderer.rb', line 26

def props
  @props
end

#router_push_toObject

Returns the value of attribute router_push_to.



26
27
28
# File 'lib/vue_rails/renderer.rb', line 26

def router_push_to
  @router_push_to
end

#stateObject

Returns the value of attribute state.



26
27
28
# File 'lib/vue_rails/renderer.rb', line 26

def state
  @state
end

Class Method Details

.server_render(*args) ⇒ Object



35
36
37
# File 'lib/vue_rails/renderer.rb', line 35

def self.server_render *args
  new(*args).render
end

Instance Method Details

#renderObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vue_rails/renderer.rb', line 39

def render
  if Rails.env.production?
    self.context ||= (
    js_code = VueRails::WebpackerAssetFinder.new.find_asset("vue_server_render.js")
    ExecJS.compile(GLOBAL_WRAPPER + CONSOLE_POLYFILL + js_code)
    )
  else
    self.context = (
    js_code = VueRails::WebpackerAssetFinder.new.find_asset("vue_server_render.js")
    ExecJS.compile(GLOBAL_WRAPPER + CONSOLE_POLYFILL + js_code)
    )
  end

  self.context.eval("RailsVueUJS.serverRender('#{component}', #{props}, '#{router_push_to}', #{state})")
end