Sha256: ca43f7d6681d3a7a083adbc436f1700d1154175c693a39526c26d57db2dc26d3

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

require 'wunderbar/render'
require 'ruby2js/filter/vue'

vue = File.expand_path('../vendor/vue.min.js', __FILE__)
Wunderbar::Asset.script name: 'vue.min.js', file: vue, render: true,
  server: File.expand_path('../vendor/vue-server.min.js', __FILE__),
  require: {Vue: 'vue', VueServer: 'vue-server'}, browserify: true

class Wunderbar::Render
  RUBY2JS_OPTIONS = {vue_h: '$h'}

  def self.nodejs
    return @nodejs if @nodejs
    path = `which nodejs`.chomp
    path = `which node`.chomp if path.empty?
    raise RuntimeError.new('Unable to locate nodejs') if path.empty?
    @nodejs = path.untaint
  end

  def self.server(common)
    "VueServer.renderToString(new Vue({render: function($h) {return #{common}}}))"
  end

  def self.client(common, element, target)
    wrap = "$h(#{target.name.inspect}, " +
      "{attrs: {#{target.attrs.map {|name, value|
      "#{name}: #{value.inspect}"}.join(', ')}}}, [#{common}])"
    "new Vue({el: #{element}, render: function($h) {return #{wrap}}})"
  end

  def self.eval(scripts, server)
    stdout, stderr, status = Open3.capture3 self.nodejs,
      stdin_data: scripts.compact.join(";\n") + ";\n" + server

    unless stderr.empty?
      Wunderbar.error stderr
      stdout += "\n<pre>#{CGI.escapeHTML(stderr)}</pre>"
    end

    stdout.untaint
  rescue => e
    Wunderbar.error e
    "<pre>#{CGI.escapeHTML(e.message)}</pre>"
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wunderbar-1.3.1 lib/wunderbar/vue.rb
wunderbar-1.3.0 lib/wunderbar/vue.rb
wunderbar-1.2.10 lib/wunderbar/vue.rb
wunderbar-1.2.9 lib/wunderbar/vue.rb
wunderbar-1.2.8 lib/wunderbar/vue.rb
wunderbar-1.2.7 lib/wunderbar/vue.rb
wunderbar-1.2.6 lib/wunderbar/vue.rb
wunderbar-1.2.5 lib/wunderbar/vue.rb