Class: Sprockets::Vue::Script

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::JavaScriptHelper
Defined in:
lib/sprockets/vue/script.rb

Constant Summary collapse

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 } }
}

Class Method Summary collapse

Class Method Details

.cache_keyObject



49
50
51
52
53
54
# File 'lib/sprockets/vue/script.rb', line 49

def cache_key
  [
    self.name,
    VERSION,
  ].freeze
end

.call(input) ⇒ Object



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
# File 'lib/sprockets/vue/script.rb', line 19

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

.warp(s) ⇒ Object



45
46
47
# File 'lib/sprockets/vue/script.rb', line 45

def warp(s)
  "(function(){#{s}}).call(this);"
end