lib/roda/opal_assets.rb in roda-opal_assets-0.3.1 vs lib/roda/opal_assets.rb in roda-opal_assets-0.3.2

- old
+ new

@@ -27,10 +27,16 @@ r.run sprockets end r.on 'assets/css' do r.run sprockets end + r.on 'assets/images' do + r.run sprockets + end + r.on 'assets' do + r.run sprockets + end end end def js file file << '.js' unless file.end_with? '.js' @@ -66,11 +72,32 @@ asset.filename.to_s.sub(Dir.pwd, '') end %{<link href="#{path}" media="#{media}" rel="stylesheet" />} end + alias css stylesheet + def image file, **attrs + path = if production? + "/assets/#{manifest[file]}" + else + asset = sprockets[file] + + if asset.nil? + raise "File not found: #{file}" + end + + "/assets/images/#{asset.digest_path}" + end + + attrs = attrs.each_with_object('') do |(key, value), string| + string << " #{key}=#{value}" + end + + %{<img src="#{path}"#{attrs}/>} + end + def << asset @assets << asset end def build @@ -78,11 +105,13 @@ @manifest = @assets.each_with_object({}) do |file, hash| print "Compiling #{file}..." asset = sprockets[file] hash[file] = asset.digest_path - compile_file file, "public/assets/#{asset.digest_path}" + filename = "public/assets/#{asset.digest_path}" + FileUtils.mkdir_p File.dirname(filename) + compile_file file, filename puts ' done' end File.write 'assets.yml', YAML.dump(manifest) end @@ -105,9 +134,11 @@ Opal.paths.each do |path| sprockets.append_path path end sprockets.append_path 'assets/js' sprockets.append_path 'assets/css' + sprockets.append_path 'assets/images' + sprockets.append_path 'assets' sprockets.js_compressor = :uglifier if @minify @sprockets = sprockets end