Class: SC::Builder::ModuleInfo

Inherits:
Base show all
Defined in:
lib/sproutcore/builders/module.rb

Overview

Builds a module_info.js file which MUST be run before the framework is loaded by the application or framework doing the loading.

Instance Attribute Summary

Attributes inherited from Base

#entry

Instance Method Summary collapse

Methods inherited from Base

build, #initialize, #joinlines, #read, #readlines, #replace_static_url, #sc_static_match, #static_url, #writeline, #writelinebinary, #writelines

Constructor Details

This class inherits a constructor from SC::Builder::Base

Instance Method Details

#build(dst_path) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sproutcore/builders/module.rb', line 16

def build(dst_path)
  begin
    require 'erubis'
  rescue
    raise "Cannot render module_info.js because erubis is not installed. Try running 'sudo gem install erubis' and try again."
  end

  eruby = Erubis::Eruby.new <<-EOT
    ;(function() {
      var target_name = '<%= @target_name %>' ;
      if (!SC.MODULE_INFO) throw "SC.MODULE_INFO is not defined!" ;
      if (SC.MODULE_INFO[target_name]) return ; <%# not an error... %>

      <%# first time, so add a Hash with this target's module_info %>
      SC.MODULE_INFO[target_name] = SC.Object.create({
        dependencies:[<%= @dependencies.join(',') %>],
        styles:[<%= @styles.join(',') %>],
        styles2x: [<%= @styles2x.join(',') %>],
        scriptURL:'<%= @script %>',
        stringURL:'<%= @string %>'<% if @prefetched %>,
        isPrefetched: YES
        <% end %><% if @inlined %>,
        isLoaded: YES,
        source: <%= @source %>
        <% end %><% if @css_source %>,
        cssSource: <%= @css_source %>
        <% end %><% if @css_2x_source %>,
        css2xSource: <%= @css_2x_source %>
        <% end %>
      })
    })();
  EOT

  output = ""

  entry.targets.each do |t|
    next unless t[:target_type] == :module

    manifest = t.manifest_for(entry.manifest.variation)

    script_entry = manifest.find_entry('javascript.js')
    next if not script_entry
    script_url = script_entry.cacheable_url

    string_entry = manifest.find_entry('javascript-strings.js')
    next if not string_entry
    string_url = string_entry.cacheable_url

    module_info = t.module_info({ :variation => entry[:variation] })

    # Handle inlined modules. Inlined modules get included as strings
    # in the module info.
    source = nil, css_source = nil, css_2x_source = nil

    # if (and only if) the module is inlined, we must include the source
    # for the JS AND CSS inline as well (as strings)
    if t[:inlined_module]
      source = File.read(script_entry.stage![:staging_path]).to_json

      css_entry = manifest.find_entry('stylesheet.css')
      if css_entry
        css_path = css_entry.stage![:staging_path]

        # We must check if the file exists because there are cases where we can have
        # an entry but no file: no file is added if the file is empty, but the file
        # could be empty if all input files are empty or full of comments.
        css_source = File.read(css_path).to_json if File.exist? css_path
      end

      css_2x_entry = manifest.find_entry('[email protected]')
      if css_2x_entry
        css_2x_path = css_2x_entry.stage![:staging_path]
        css_2x_source = File.read(css_2x_path).to_json if File.exist? css_2x_path
      end
    end

    output << eruby.evaluate({
      :target_name => t[:target_name].to_s.sub(/^\//,''),
      :dependencies    => module_info[:requires].map{ |t| "'#{t[:target_name].to_s.sub(/^\//,'')}'" },
      :styles      => module_info[:css_urls].map{ |url| "'#{url}'" },
      :styles2x    => module_info[:css_2x_urls].map {|url| "'#{url}'"},
      :script      => script_url,
      :string      => string_url,
      :source      => source,
      :inlined     => t[:inlined_module],
      :prefetched  => t[:prefetched_module],
      :css_source  => css_source,
      :css_2x_source => css_2x_source
    })
  end
  writelines dst_path, [output]
end