Class: SC::Builder::JavaScript

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

Overview

This build is used to process a single javascript file. It will substitute any calls to sc_super and sc_static() (or static_url()). It does NOT product a combined JavaScript for production. See the Builder::CombinedJavaScript for more.

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



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
# File 'lib/sproutcore/builders/javascript.rb', line 18

def build(dst_path)
  lines = ""
  target_name = entry.target[:target_name].to_s.sub(/^\//,'')

  if entry[:lazy_instantiation] && entry[:notify_onload]
  lines << ";
if ((typeof SC !== 'undefined') && SC && !SC.LAZY_INSTANTIATION) {
  SC.LAZY_INSTANTIATION = {};
}
if(!SC.LAZY_INSTANTIATION['#{target_name}']) {
  SC.LAZY_INSTANTIATION['#{target_name}'] = [];
}
SC.LAZY_INSTANTIATION['#{target_name}'].push(
  (
function() {
"
  end

  code = rewrite_inline_code(read(entry[:source_path]))
  code = handle_debug_code(code)
  lines << code

  # Try to load dependencies if we're not combining javascript.
  if entry[:notify_onload]
    lines << "; if ((typeof SC !== 'undefined') && SC && SC.Module && SC.Module.scriptDidLoad) SC.Module.scriptDidLoad('#{target_name}');"
  end

  if entry[:lazy_instantiation] && entry[:notify_onload]
    lines << "
}
  )
);
"
  end

  writelines dst_path, lines
end

#handle_debug_code(code) ⇒ Object

Strips or replaces @if(debug) blocks, depending on build mode.



65
66
67
68
69
70
71
72
73
# File 'lib/sproutcore/builders/javascript.rb', line 65

def handle_debug_code(code)
  code.gsub(/\/\/\s*@if\s*\(\s*debug\s*\)([^\0]*?)\/\/\s*@endif/) {|match|
    if CONFIG[:load_debug]
      $1
    else
      "\n"
    end
  }
end

#localized_strings?Boolean

Returns true if the current entry is a localized strings file. These files receive some specialized processing to allow for server-side only strings. – You can name a string key beginning with “@@” and it will be removed.

Returns:

  • (Boolean)


60
61
62
# File 'lib/sproutcore/builders/javascript.rb', line 60

def localized_strings?
  @lstrings ||= entry[:localized] && entry[:filename] =~ /strings.js$/
end

#rewrite_inline_code(code) ⇒ Object

Rewrites inline content for a single line



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sproutcore/builders/javascript.rb', line 76

def rewrite_inline_code(code)

  # Fors strings file, remove server-side keys (i.e '@@foo' = 'bar')
  if localized_strings?
    code.gsub!(/["']@@.*["']\s*?:\s*?["'].*["']\s*,\s*$/,'')

  # Otherwise process sc_super
  else
    code.gsub!(/sc_super\(\s*\)/, 'arguments.callee.base.apply(this,arguments)')
    code.gsub!(/sc_super\((.+?)\)/) do
      SC.logger.warn "\nWARNING: Calling sc_super() with arguments is DEPRECATED. Please use sc_super() only.\n\n"
      "arguments.callee.base.apply(this, #{$1})"
    end
  end

  # and finally rewrite static_url
  replace_static_url(code)
  code
end