class VueSprocketsCompiler

Constants

INDENT
SLIM_OPTS

Public Class Methods

_render(text) click to toggle source
# File lib/vue/sprockets.rb, line 34
def _render(text)
  if defined? Slim::Template
    html = Slim::Template.new(SLIM_OPTS){ text }.render
    puts html if $VERBOSE
    html
  else
    text
  end
end
call(input) click to toggle source
# File lib/vue/sprockets.rb, line 49
def call(input)
  source   = input[:data]
  load_path = input[:load_path]
  filename = input[:filename]

  path = filename[load_path.length..-1]

  if is_in_path(path)
    js = source.gsub( /(?<spaces>\s+)(?<slim>slim_)?template\s*:\s*(?<quote>["`'])(?<code>.*?[^\\])\k<quote>/m ) do |match|
      spaces = strip_newline "#{$1}"
      slim   = $2
      src = $4.gsub("\\n","\n")
      src = src.gsub("\\\"","\"")
      src = src.gsub("\\\'","\'")
      src = src.gsub("\\t","\t")

      if slim
        begin
          src = _render(src)
        rescue Slim::Parser::SyntaxError =>e
          puts "ERROR: vue/sprockets #{filename} ==>\n#{INDENT + e.to_s}"
          src = "*** SLIM TEMPLATE ERROR ***"
        end
      end

      result = Vue::Compiler.compile(src)

      out = "\n" + spaces + "render :" + toFunction(result[:render]) + ",\n"
      out += spaces + "staticRenderFns :[" + (result[:staticRenderFns] || []).map{|f| toFunction(f)}.join(',') + "]"
      puts "ERROR: vue/sprockets #{filename} ==>\n#{output result[:errors]}" if result[:errors] != []
      puts "TIP: vue/sprockets #{filename} ==>\n#{output result[:tips]}"   if result[:tips] != []
      out
    end
    {:data=>js}
  else
    nil
  end
end
is_in_path(path) click to toggle source
# File lib/vue/sprockets.rb, line 21
def is_in_path(path)
  (@path==nil) || @path.any?{|p| path[0..p.length-1] == p}
end
output(list) click to toggle source
# File lib/vue/sprockets.rb, line 30
def output(list)
  list && (INDENT + list.join("\n" + INDENT))
end
set_root(path=nil) click to toggle source

compile templates under this path.

# File lib/vue/sprockets.rb, line 16
def set_root(path=nil)
  path = [path].flatten
  @path = path.map{|p| (p||'')[0,1] == '/' ? p : '/' + (p||'') }
end
strip_newline(str) click to toggle source
# File lib/vue/sprockets.rb, line 44
def strip_newline(str)
    idx = str.rindex("\n")
    (idx && str[idx+1..-1]) || str
end
toFunction(code) click to toggle source
# File lib/vue/sprockets.rb, line 26
def toFunction(code)
   return 'function () {' + code + '}'
end