Sha256: c3e428a028eb1d46ce864e835e775b206f09055146cd4aa79bdd156ac87f5501

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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
    @nodejs = `which nodejs`.chomp
    @nodejs = `which node`.chomp if @nodejs.empty?
    raise RuntimeError.new('Unable to locate nodejs') if @nodejs.empty?
    @nodejs.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

1 entries across 1 versions & 1 rubygems

Version Path
wunderbar-1.2.4 lib/wunderbar/vue.rb