vue-compiler 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,4 @@
1
- require 'v8'
2
-
3
-
1
+ require 'execjs'
4
2
 
5
3
  module Vue
6
4
 
@@ -8,12 +6,11 @@ module Vue
8
6
 
9
7
  JS_ROOT = File.join(File.dirname(__FILE__),'../..','js')
10
8
 
11
- COMPILER = File.join(JS_ROOT,'vue-compile-template-2.4.js')
12
-
13
9
  class << self
14
10
 
15
11
  def _reset
16
12
  @__ctx = nil
13
+ @options = {}
17
14
  end
18
15
 
19
16
  def set_options(opts={})
@@ -21,39 +18,60 @@ module Vue
21
18
  end
22
19
 
23
20
  def _options
24
- @options || {}
21
+ @options ||= {}
25
22
  end
26
23
 
27
24
  def _compiler
28
25
  version = _options[:version] || _options['version'] || '2.5'
29
- script = File.join(JS_ROOT,"vue-compile-template-#{version}.js")
26
+ script = _options[:script] || File.join(JS_ROOT,"vue-compile-template-#{version}.js")
30
27
  raise "invalid compile script #{script}" unless File.exist?(script)
31
28
  script
32
29
  end
33
30
 
34
31
  def _ctx
35
- @__ctx ||= begin
36
- ctx = V8::Context.new
37
- ctx.load _compiler
38
- ctx
39
- end
32
+ @__ctx ||= ExecJS.compile( File.read(_compiler) + custom)
33
+ end
34
+
35
+
36
+ def custom
37
+ <<-JS
38
+
39
+ VXCompile = function(text){
40
+ resp = VueTemplateCompiler.compile(text);
41
+ return {
42
+ render: resp.render,
43
+ staticRenderFns: resp.staticRenderFns,
44
+ errors:resp.errors,
45
+ tips:resp.tips
46
+ }
47
+ }
48
+
49
+ VXParse = function(text){
50
+ resp = VueTemplateCompiler.parseComponent(text);
51
+ return {
52
+ script: resp.script ,
53
+ template: resp.template ,
54
+ styles:resp.styles
55
+ }
56
+ }
57
+ JS
40
58
  end
41
59
 
42
60
  def compile(template,options={})
43
- obj = _ctx[:VueTemplateCompiler][:compile].call(template.to_s)
61
+ obj = _ctx.call("VXCompile",template.to_s)
44
62
  {
45
- :render=>obj[:render].to_s,
46
- :staticRenderFns=>obj[:staticRenderFns].to_a,
47
- :errors=>obj[:errors].to_s,
48
- :tips=>obj[:tips].to_s
63
+ :render=>obj["render"].to_s,
64
+ :staticRenderFns=>obj["staticRenderFns"].to_a,
65
+ :errors=>obj["errors"].to_a,
66
+ :tips=>obj["tips"].to_a
49
67
  }
50
68
  end
51
69
 
52
70
  def parseComponent(file, options={})
53
- obj = _ctx[:VueTemplateCompiler][:parseComponent].call(file.to_s)
54
- {:script=>obj[:script] && obj[:script][:content],
55
- :template=>obj[:template] && obj[:template][:content],
56
- :styles=>obj[:styles] && obj[:styles].map{ |s| s[:content] }
71
+ obj = _ctx.call("VXParse",file.to_s)
72
+ {:script=>obj["script"] && obj["script"]["content"],
73
+ :template=>obj["template"] && obj["template"]["content"],
74
+ :styles=>obj["styles"] && obj["styles"].map{ |s| s["content"] }
57
75
  }
58
76
  end
59
77
 
@@ -1,46 +1,90 @@
1
1
  # transform vue objects by compiling the templates into render functions.
2
2
 
3
- require 'vue/compiler'
3
+ require 'sprockets'
4
+ require_relative 'compiler'
4
5
 
5
6
  class VueSprocketsCompiler
6
7
 
7
- # compile templates under this path.
8
- def self.set_root(path=nil)
9
- path = '/' + path unless path[0,1] == '/'
10
- @root = path
11
- end
8
+ class << self
12
9
 
13
- def self.toFunction(code)
14
- return 'function () {' + code + '}'
15
- end
10
+ INDENT = " "
11
+ SLIM_OPTS = {:code_attr_delims=>{'(' => ')','[' => ']'},
12
+ :attr_list_delims=>{'(' => ')','[' => ']'},
13
+ :disable_escape=>true }
14
+
15
+ # compile templates under this path.
16
+ def set_root(path=nil)
17
+ path = [path].flatten
18
+ @path = path.map{|p| (p||'')[0,1] == '/' ? p : '/' + (p||'') }
19
+ end
20
+
21
+ def is_in_path(path)
22
+ (@path==nil) || @path.any?{|p| path[0..p.length-1] == p}
23
+ end
24
+
25
+
26
+ def toFunction(code)
27
+ return 'function () {' + code + '}'
28
+ end
16
29
 
17
- def self.call(input)
18
- source = input[:data]
19
- load_path = input[:load_path]
20
- filename = input[:filename]
30
+ def output(list)
31
+ list && (INDENT + list.join("\n" + INDENT))
32
+ end
21
33
 
22
- path = filename[load_path.length..-1]
34
+ def _render(text)
35
+ if defined? Slim::Template
36
+ html = Slim::Template.new(SLIM_OPTS){ text }.render
37
+ puts html if $VERBOSE
38
+ html
39
+ else
40
+ text
41
+ end
42
+ end
43
+
44
+ def strip_newline(str)
45
+ idx = str.rindex("\n")
46
+ (idx && str[idx+1..-1]) || str
47
+ end
23
48
 
24
- if !@root || (path[0..@root.length-1] == @root)
25
- js = source.gsub( /(?<spaces>\s+)template\s*:\s*(?<quote>["`'])(?<code>.*?[^\\])\k<quote>/m ) do |match|
26
- spaces = "#{$1}"
27
- src = $3.gsub("\\n","\n")
28
- src = src.gsub("\\\"","\"")
29
- src = src.gsub("\\\'","\'")
30
- src = src.gsub("\\t","\t")
49
+ def call(input)
50
+ source = input[:data]
51
+ load_path = input[:load_path]
52
+ filename = input[:filename]
31
53
 
32
- result = Vue::Compiler.compile(src)
54
+ path = filename[load_path.length..-1]
33
55
 
34
- out = spaces + "render :" + toFunction(result[:render]) + ",\n"
35
- out += spaces + "staticRenderFns :[" + (result[:staticRenderFns] || []).map{|f| toFunction(f)}.join(',') + "]"
36
- puts "ERROR: vue/sprockets #{filename} ==>\n#{result[:errors]}" if result[:errors].to_s != ""
37
- puts "TIP: vue/sprockets #{filename} ==>\n#{result[:tips]}" if result[:tips].to_s != ""
38
- out
56
+ if is_in_path(path)
57
+ js = source.gsub( /(?<spaces>\s+)(?<slim>slim_)?template\s*:\s*(?<quote>["`'])(?<code>.*?[^\\])\k<quote>/m ) do |match|
58
+ spaces = strip_newline "#{$1}"
59
+ slim = $2
60
+ src = $4.gsub("\\n","\n")
61
+ src = src.gsub("\\\"","\"")
62
+ src = src.gsub("\\\'","\'")
63
+ src = src.gsub("\\t","\t")
64
+
65
+ if slim
66
+ begin
67
+ src = _render(src)
68
+ rescue Slim::Parser::SyntaxError =>e
69
+ puts "ERROR: vue/sprockets #{filename} ==>\n#{INDENT + e.to_s}"
70
+ src = "*** SLIM TEMPLATE ERROR ***"
71
+ end
72
+ end
73
+
74
+ result = Vue::Compiler.compile(src)
75
+
76
+ out = "\n" + spaces + "render :" + toFunction(result[:render]) + ",\n"
77
+ out += spaces + "staticRenderFns :[" + (result[:staticRenderFns] || []).map{|f| toFunction(f)}.join(',') + "]"
78
+ puts "ERROR: vue/sprockets #{filename} ==>\n#{output result[:errors]}" if result[:errors] != []
79
+ puts "TIP: vue/sprockets #{filename} ==>\n#{output result[:tips]}" if result[:tips] != []
80
+ out
81
+ end
82
+ {:data=>js}
83
+ else
84
+ nil
39
85
  end
40
- {:data=>js}
41
- else
42
- nil
43
86
  end
87
+
44
88
  end
45
89
 
46
90
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vue-compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clive Andrews
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-20 00:00:00.000000000 Z
11
+ date: 2020-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: therubyracer
14
+ name: execjs
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -38,6 +38,7 @@ files:
38
38
  - README.md
39
39
  - js/vue-compile-template-2.4.js
40
40
  - js/vue-compile-template-2.5.js
41
+ - js/vue-compile-template-2.6.js
41
42
  - lib/vue/compiler.rb
42
43
  - lib/vue/sprockets.rb
43
44
  homepage: https://github.com/realbite/vue-compiler
@@ -59,8 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
62
  requirements: []
62
- rubyforge_project:
63
- rubygems_version: 2.6.11
63
+ rubygems_version: 3.1.4
64
64
  signing_key:
65
65
  specification_version: 4
66
66
  summary: vue-template-compiler functionality in a gem