Class: Distil::JavascriptProduct

Inherits:
Product
  • Object
show all
Includes:
ErrorReporter
Defined in:
lib/distil/product/javascript-product.rb

Constant Summary collapse

MODULE_TEMPLATE =
ERB.new %q{
  distil.module("<%=project.name%>", <%=json_for(definition)%>);
}.gsub(/^\s*/, '')

Instance Attribute Summary

Attributes inherited from Product

#assets, #files, #language, #libraries, #project, #variant

Instance Method Summary collapse

Methods included from ErrorReporter

#error, error, #has_errors?, #has_warnings?, #ignore_warnings, #ignore_warnings=, #report, #total_error_count, #total_warning_count, warning, #warning

Methods inherited from Product

#build, #clean, #filename, #gzip, #gzip_filename, #handles_file?, #include_file, #initialize, #minimise, #minimised_filename, #notice_comment, #output_path, #up_to_date?

Constructor Details

This class inherits a constructor from Distil::Product

Instance Method Details

#build_debugObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/distil/product/javascript-product.rb', line 120

def build_debug
  File.open(output_path, "w") { |output|

    output.puts notice_comment
    
    if (APPLICATION_TYPE==project.project_type)
      output.puts File.read(BOOTSTRAP_SCRIPT)
      if (project.synchronise_assets)
        output.puts "\n\ndistil.sync= true;\n\n"
      end
    end
    
    if project.global_export
      output.puts
      output.puts "window.#{project.global_export}=window.#{project.global_export}||{};"
      output.puts
    end

    libraries.each { |l|
      path= project.relative_output_path_for(l.file_for(content_type, language, variant))
      next if !path
      output.puts "/*jsl:import #{path}*/"
    }
    
    files.each { |f|
      path= project.relative_output_path_for(f)
      next if !path
      output.puts "/*jsl:import #{path}*/"
    }

    output.puts module_definition
      
  }
end

#build_releaseObject



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
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/distil/product/javascript-product.rb', line 68

def build_release
  File.open(output_path, "w") { |output|
    
    output.puts notice_comment
    
    if (APPLICATION_TYPE==project.project_type)
      output.puts File.read(BOOTSTRAP_SCRIPT)
      if (project.synchronise_assets)
        output.puts "\n\ndistil.sync= true;\n\n"
      end
      output.puts FILE_SEPARATOR
    end

    output.puts module_definition

    # emit remote assets first
    libraries.each { |l|
      f= project.file_from_path(l.file_for(content_type, language, variant))
      next if !f
      
      content= f.rewrite_content_relative_to_path(nil)
      next if !content || content.empty?
      
      output.puts content.strip
      output.puts "\n"
      output.puts FILE_SEPARATOR
    }

    if project.global_export
      exports= [project.global_export, *project.additional_globals].join(", ")
      output.puts "(function(#{exports}){"
    end

    files.each { |f|
      content= f.rewrite_content_relative_to_path(nil)

      next if !content || content.empty?
        
      output.puts content.strip
      output.puts "\n"
      output.puts FILE_SEPARATOR
    }
    
    if project.global_export
      exports= ["window.#{project.global_export}=window.#{project.global_export}||{}", *project.additional_globals].join(", ")
      output.puts "})(#{exports});"
    end
    
    output.puts "distil.moduleDidLoad('#{project.name}');"  
  }
end

#can_embed_as_content(file) ⇒ Object



16
17
18
# File 'lib/distil/product/javascript-product.rb', line 16

def can_embed_as_content(file)
  ["css", "html", "json", "sql"].include?(file.extension)
end

#escape_embeded_content(file) ⇒ Object



20
21
22
23
# File 'lib/distil/product/javascript-product.rb', line 20

def escape_embeded_content(file)
  content= file.rewrite_content_relative_to_path(nil)
  file.minified_content(content)
end

#json_for(obj) ⇒ Object



25
26
27
# File 'lib/distil/product/javascript-product.rb', line 25

def json_for(obj)
  JSON.generate(obj)
end

#module_definitionObject



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
# File 'lib/distil/product/javascript-product.rb', line 29

def module_definition
  asset_paths= {}
  asset_data= {}
  definition= {}
  required_files= []
  
  libraries.each { |l|
    f= l.file_for(content_type, language, variant)
    required_files << project.relative_output_path_for(f)
  }
  
  files.each { |f|
    required_files << project.relative_output_path_for(f)
  }
  
  assets.each { |asset|
    next if project.source_files.include?(asset)

    if RELEASE_VARIANT==variant
      key= project.asset_aliases[asset]||asset.relative_path
      if can_embed_as_content(asset)
        asset_data[key]= escape_embeded_content(asset)
      end
    else
      key= project.asset_aliases[asset]
      asset_paths[key]= project.relative_output_path_for(asset) if key
    end
  }
  
  definition[:asset_map]= asset_paths
  if RELEASE_VARIANT==variant
    definition[:assets]= asset_data
  else
    definition[:required]= required_files
  end
  
  MODULE_TEMPLATE.result binding
end