-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathscript.rb
57 lines (50 loc) · 1.6 KB
/
script.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'active_support/concern'
require "action_view"
module Sprockets::Vue
class Script
class << self
include ActionView::Helpers::JavaScriptHelper
SCRIPT_REGEX = Utils.node_regex('script')
TEMPLATE_REGEX = Utils.node_regex('template')
SCRIPT_COMPILES = {
'coffee' => ->(s, input){
CoffeeScript.compile(s, sourceMap: true, sourceFiles: [input[:source_path]], no_wrap: true)
},
'es6' => ->(s, input){
Babel::Transpiler.transform(data, {}) #TODO
},
nil => ->(s,input){ { 'js' => s } }
}
def call(input)
data = input[:data]
name = input[:name]
input[:cache].fetch([cache_key, input[:source_path], data]) do
script = SCRIPT_REGEX.match(data)
template = TEMPLATE_REGEX.match(data)
output = []
map = nil
if script
result = SCRIPT_COMPILES[script[:lang]].call(script[:content], input)
map = result['sourceMap']
output << "'object' != typeof VComponents && (this.VComponents = {});
var module = { exports: null };
#{result['js']}; VComponents['#{name}'] = module.exports;"
end
if template
output << "VComponents['#{name.sub(/\.tpl$/, "")}'].template = '#{j template[:content]}';"
end
{ data: "#{warp(output.join)}", map: map }
end
end
def warp(s)
"(function(){#{s}}).call(this);"
end
def cache_key
[
self.name,
VERSION,
].freeze
end
end
end
end