Class: Volt::AssetFiles

Inherits:
Object show all
Defined in:
lib/volt/server/rack/asset_files.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_url, component_name, component_paths) ⇒ AssetFiles

Returns a new instance of AssetFiles.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/volt/server/rack/asset_files.rb', line 17

def initialize(app_url, component_name, component_paths)
  @app_url = app_url
  @component_paths     = component_paths
  @assets              = []
  @included_components = {}
  @components          = []
  @disable_auto_import = []

  # Include each of the default included components
  Volt.config.default_components.each do |def_comp_name|
    component(def_comp_name)
  end

  component(component_name)
end

Class Method Details

.from_cache(app_url, component_name, component_paths) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/volt/server/rack/asset_files.rb', line 7

def self.from_cache(app_url, component_name, component_paths)
  # @cache ||= {}

  # @cache[component_name] ||= begin
    # not cached, create

    self.new(app_url, component_name, component_paths)
  # end
end

Instance Method Details

#add_assets(path) ⇒ Object



111
112
113
114
# File 'lib/volt/server/rack/asset_files.rb', line 111

def add_assets(path)
  asset_folder = File.join(path, 'assets')
  @assets << [:folder, asset_folder] if File.directory?(asset_folder)
end

#component(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/volt/server/rack/asset_files.rb', line 52

def component(name)
  unless @included_components[name]
    # Track that we added
    @included_components[name] = true

    # Get the path to the component
    component_path = @component_paths.component_paths(name)

    unless component_path
      fail "Unable to find component '#{name}', make sure the gem is included in your Gemfile"
    end

    component_path.each do |path|
      # Load the dependencies
      load_dependencies(path, name)

      # Add any assets
      add_assets(path) unless @disable_auto_import.include?(name)
      @components << [path, name]
    end
  end
end

#component_pathsObject



107
108
109
# File 'lib/volt/server/rack/asset_files.rb', line 107

def component_paths
  @components
end

#componentsObject



83
84
85
# File 'lib/volt/server/rack/asset_files.rb', line 83

def components
  @included_components.keys
end

#cssObject

Returns an array of all css files that should be included.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/volt/server/rack/asset_files.rb', line 172

def css
  css_files = []
  @assets.each do |type, path|
    case type
      when :folder
        # Don't import any css/scss files that start with an underscore, so scss partials
        # aren't imported by default:
        #  http://sass-lang.com/guide
        base_path = base(path)
        css_files += Dir["#{path}/**/[^_]*.{css,scss,sass}"].sort.map do |folder|
          local_path = folder[path.size..-1].gsub(/[.](scss|sass)$/, '')
          css_path = @app_url + '/' + base_path + local_path
          css_path += '.css' unless css_path =~ /[.]css$/
          css_path
        end
      when :css_file
        css_files << path
    end
  end

  css_files.uniq
end

#css_file(locator) ⇒ Object



91
92
93
# File 'lib/volt/server/rack/asset_files.rb', line 91

def css_file(locator)
  @assets << [:css_file, prepare_locator(locator, ['css','scss','sass'])]
end

#css_files(*args) ⇒ Object



120
121
122
# File 'lib/volt/server/rack/asset_files.rb', line 120

def css_files(*args)
  fail "Deprecation: #css_files is deprecated in config/base/index.html, opal 0.8 required a new format.  For an updated config/base/index.html file, see https://gist.github.com/ryanstout/0858cf7dfc32c514f790"
end

#css_tagsObject

Returns the link tags for the css



165
166
167
168
169
# File 'lib/volt/server/rack/asset_files.rb', line 165

def css_tags
  css.map do |url|
    "<link href=\"#{url}\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" />"
  end.join("\n")
end

#disable_auto_importObject



33
34
35
# File 'lib/volt/server/rack/asset_files.rb', line 33

def disable_auto_import
  @disable_auto_import.push(*@current_component).uniq
end

#javascript(volt_app) ⇒ Object

Parses the javascript tags to reutrn the following:

[:src, ‘/somefile.js’], [:body, ‘var inlinejs = true;’]


199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/volt/server/rack/asset_files.rb', line 199

def javascript(volt_app)
  javascript_tags(volt_app)
  .scan(/[<]script([^>]*)[>](.*?)[<]\/script[^>]*[>]/m)
  .map do |attrs, body|
    src = attrs.match(/[\s|$]src\s*[=]\s*["']([^"']+?)["']/)

    if src
      [:src, src[1]]
    else
      [:body, body]
    end
  end
end

#javascript_file(locator) ⇒ Object



87
88
89
# File 'lib/volt/server/rack/asset_files.rb', line 87

def javascript_file(locator)
  @assets << [:javascript_file, prepare_locator(locator, ['js'])]
end

#javascript_files(*args) ⇒ Object



116
117
118
# File 'lib/volt/server/rack/asset_files.rb', line 116

def javascript_files(*args)
  fail "Deprecation: #javascript_files is deprecated in config/base/index.html, opal 0.8 required a new format.  For an updated config/base/index.html file, see https://gist.github.com/ryanstout/0858cf7dfc32c514f790"
end

#javascript_tags(volt_app) ⇒ Object

Returns script tags that should be included



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
154
155
156
157
158
159
160
161
162
# File 'lib/volt/server/rack/asset_files.rb', line 125

def javascript_tags(volt_app)
  @opal_tag_generator ||= Opal::Server::Index.new(nil, volt_app.opal_files.server)

  javascript_files = []
  @assets.each do |type, path|
    case type
      when :folder
        # for a folder, we search for all .js files and return a tag for them
        base_path = base(path)
        javascript_files += Dir["#{path}/**/*.js"].sort.map do |folder|
          # Grab the component folder/assets/js/file.js
          local_path = folder[path.size..-1]
          @app_url + '/' + base_path + local_path
        end
      when :javascript_file
        # javascript_file is a cdn path to a JS file
        javascript_files << path
    end
  end

  javascript_files = javascript_files.uniq

  scripts = javascript_files.map {|url| "<script src=\"#{url}\"></script>" }

  # Include volt itself.  Unless we are running with MAPS=all, just include
  # the main file without sourcemaps.
  volt_path = 'volt/volt/app'
  if ENV['MAPS'] == 'all'
    scripts << @opal_tag_generator.javascript_include_tag(volt_path)
  else
    scripts << "<script src=\"#{volt_app.app_url}/#{volt_path}.js\"></script>"
    scripts << "<script>#{Opal::Processor.load_asset_code(volt_app.sprockets, volt_path)}</script>"
  end

  scripts << @opal_tag_generator.javascript_include_tag('components/main')

  scripts.join("\n")
end

#load_dependencies(path, component_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/volt/server/rack/asset_files.rb', line 37

def load_dependencies(path, component_name)
  if path
    dependencies_file = File.join(path, 'config/dependencies.rb')
  else
    fail "Unable to find component #{component_name.inspect}"
  end

  if File.exist?(dependencies_file)
    # Run the dependencies file in this asset files context
    code = File.read(dependencies_file)
    @current_component = component_name
    instance_eval(code, dependencies_file, 0)
  end
end

#opal_gem(gem_name) ⇒ Object

Called when you want to add a gem to the opal load path so it can be required on the client side.



77
78
79
80
81
# File 'lib/volt/server/rack/asset_files.rb', line 77

def opal_gem(gem_name)
  Opal.use_gem(gem_name)
  Opal.paths.uniq!
  # require(gem_name)
end

#prepare_locator(locator, valid_extensions) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/volt/server/rack/asset_files.rb', line 95

def prepare_locator(locator, valid_extensions)
  unless url_or_path?(locator)
    locator = File.join(@app_url, @current_component, '/assets', valid_extensions.first, "#{locator}")
    locator += '.css' unless locator =~ /^.*\.(#{valid_extensions.join('|')})$/
  end
  locator
end

#url_or_path?(url) ⇒ Boolean

Returns:



103
104
105
# File 'lib/volt/server/rack/asset_files.rb', line 103

def url_or_path?(url)
  (url =~ URI::regexp || url =~ /^\/(\/)?.*/) ? true : false
end