Class: VueSprocketsCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/vue/sprockets.rb

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vue/sprockets.rb', line 17

def self.call(input)
  source   = input[:data]
  load_path = input[:load_path]
  filename = input[:filename]

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

  if !@root || (path[0..@root.length-1] == @root)
    js = source.gsub( /(?<spaces>\s+)template\s*:\s*(?<quote>["`'])(?<code>.*?[^\\])\k<quote>/m ) do |match|
      spaces = "#{$1}"
      src = $3.gsub("\\n","\n")
      src = src.gsub("\\\"","\"")
      src = src.gsub("\\\'","\'")
      src = src.gsub("\\t","\t")

      result = Vue::Compiler.compile(src)

      out = spaces + "render :" + toFunction(result[:render]) + ",\n"
      out += spaces + "staticRenderFns :[" + (result[:staticRenderFns] || []).map{|f| toFunction(f)}.join(',') + "]"
      puts "ERROR: vue/sprockets #{filename} ==>\n#{result[:errors]}" if result[:errors].to_s != ""
      puts "TIP: vue/sprockets #{filename} ==>\n#{result[:tips]}"   if result[:tips].to_s != ""
      out
    end
    {:data=>js}
  else
    nil
  end
end

.set_root(path = nil) ⇒ Object

compile templates under this path.



8
9
10
11
# File 'lib/vue/sprockets.rb', line 8

def self.set_root(path=nil)
  path = '/' + path unless path[0,1] == '/'
  @root = path
end

.toFunction(code) ⇒ Object



13
14
15
# File 'lib/vue/sprockets.rb', line 13

def self.toFunction(code)
   return 'function () {' + code + '}'
end