Sha256: f0547ac0fd54a780276505489be58e2aa2ce6d9112bd39890970d13a6972c999

Contents?: true

Size: 1.06 KB

Versions: 12

Compression:

Stored size: 1.06 KB

Contents

require 'rack/asset_compiler'
require 'coffee-script'

module Rack
  class CoffeeCompiler < AssetCompiler
    LOCK = Mutex.new

    def initialize(app, options={})
      options = {
        :url => '/javascripts',
        :content_type => 'text/javascript',
        :source_extension => 'coffee',
        :alert_on_error => ENV['RACK_ENV'] != 'production',
        :lock => LOCK
      }.merge(options)

      @alert_on_error = options[:alert_on_error]
      @lock = options[:lock]
      super
    end

    def compile(source_file)
      if @lock
        @lock.synchronize{ unsynchronized_compile(source_file) }
      else
        unsynchronized_compile(source_file)
      end
    end

    def unsynchronized_compile(source_file)
      begin
        CoffeeScript.compile(::File.read(source_file))
      rescue CoffeeScript::CompilationError => e
        if @alert_on_error
          error_msg = "CoffeeScript compilation error in #{source_file}.coffee:\n\n #{e.to_s}"
          "window.alert(#{error_msg.to_json});"
        else
          raise e
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
half-pipe-0.3.0.beta.2 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.beta.1 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.alpha.5 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.alpha.4 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.alpha.3 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.alpha.2 lib/rack/coffee_compiler.rb
half-pipe-0.3.0.alpha.1 lib/rack/coffee_compiler.rb
half-pipe-0.2.4 lib/rack/coffee_compiler.rb
half-pipe-0.2.3 lib/rack/coffee_compiler.rb
half-pipe-0.2.2 lib/rack/coffee_compiler.rb
half-pipe-0.2.1 lib/rack/coffee_compiler.rb
half-pipe-0.2.0 lib/rack/coffee_compiler.rb