Class: Distil::JavascriptFile

Inherits:
SourceFile show all
Includes:
YuiMinifiableFile
Defined in:
lib/distil/source-file/javascript-file.rb

Direct Known Subclasses

JsonFile

Instance Attribute Summary

Attributes inherited from SourceFile

#full_path, #is_asset, #language, #project

Instance Method Summary collapse

Methods included from YuiMinifiableFile

#minified_content

Methods inherited from SourceFile

#add_asset, #add_dependency, #assets, #basename, #content, #content_type, #copy_to, #dirname, #error, #extension, #initialize, #last_modified, #minified_content, #output_path, #path_relative_to, #path_relative_to_folder, #relative_path, #to_s, #to_str, #warning

Methods included from ErrorReporter

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

Constructor Details

This class inherits a constructor from Distil::SourceFile

Instance Method Details

#check_nibObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/distil/source-file/javascript-file.rb', line 14

def check_nib
  nib_name= basename(".js")

  Dir.glob("#{dirname}/**/*") { |asset|
    next if File.directory?(asset) || asset.to_s==full_path
    asset= project.file_from_path(asset)

    case
    when 'js'==asset.content_type || 'css'==asset.content_type
      add_dependency(asset)
    when 'html'==asset.content_type
      add_asset(asset)
      project.add_alias_for_asset("#{nib_name}##{asset.basename}", asset)
    else
      add_asset(asset)
    end
  }
end

#dependenciesObject



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

def dependencies
  @dependencies unless @dependencies.nil?

  @dependencies= []
  is_nib_file= (full_path =~ /([^\.\/]+)\.jsnib\/\1\.js$/)
  
  content.each_with_index do |line, line_num|
    line_num+=1
  
    if is_nib_file && match=line.match(NIB_DECLARATION_REGEX)
      unless match[2]==basename(".*")
        error "NIB name must match match filename, otherwise NIB will be unloadable.", line_num
      end
    end
    
    # handle dependencies
    line.scan(JSL_IMPORT_REGEX) do |match|
      import_file= File.join(dirname, $1)
      if (File.exists?(import_file))
        add_dependency project.file_from_path(import_file)
      else
        dependency= project.find_file($1, :js, :import)
        if (dependency)
          add_dependency project.file_from_path(dependency)
          project.add_alias_for_file($1, dependency)
        else
          error "Missing import file: #{$1}", line_num
        end
      end
    end
    
    line.scan(DISTIL_ASSET_REGEX) do |match|
      asset= project.file_from_path(File.join(dirname, $2))
      if File.exists?(asset)
        add_asset(asset)
      else
        error "Missing asset file: #{$2}", line_num
      end
    end

    line.scan(NIB_ASSET_REGEX) do |match|
      asset= project.file_from_path(File.join(dirname, $2))
      if File.exists?(asset)
        add_asset(asset)
      else
        error "Missing asset file: #{$2}", line_num
      end
    end
  end

  if is_nib_file
    # Handle special asset requirements for NIB files
    check_nib
  else
    html= "#{basename(".js")}.html"
    html_path= File.join(dirname, html)
    
    # Add an alias for an HTML file with the same basename if it exists
    if File.exists?(html_path)
      asset= project.file_from_path(html_path)
      add_asset(asset)
      project.add_alias_for_asset(html, asset)
    end
  end
  
  @dependencies
end

#rewrite_content_relative_to_path(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/distil/source-file/javascript-file.rb', line 33

def rewrite_content_relative_to_path(path)
  text= content.gsub(JSL_IMPORT_REGEX, '')
  text= text.gsub(NIB_ASSET_REGEX) do |match|
      asset= project.file_from_path(File.join(dirname, $2))
      if asset
        "#{$1}(\"#{asset.relative_path}\")"
      else
        match
      end
  end
  text= text.gsub(DISTIL_ASSET_REGEX) do |match|
      asset= project.file_from_path(File.join(dirname, $2))
      if asset
        "#{$1}(\"#{asset.relative_path}\")"
      else
        match
      end
  end
end