Sha256: dbb0e25fe3a5e7ba94d48308c743a7122ba0728e3ee604d61d614ba1cff7e089

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

module Rail
  module Processor
    class CoffeeScript < Base
      def self.input_extension
        'coffee'
      end

      def self.output_extension
        'js'
      end

      def self.mime_type
        'application/javascript'
      end

      def compile(filename, options = {})
        code = process(filename, options)
        code = Uglifier.new.compile(code) if compress?
        code
      end

      private

      def process(filename, options = {})
        output = []

        code = File.read(filename)

        extract_requirements(code).each do |name|
          requirement_filename = find_requirement(name, filename)
          raise NotFoundError unless requirement_filename
          output << compile(requirement_filename, options)
        end

        output << ::CoffeeScript.compile(code, options)

        output.join
      end

      def find_requirement(name, referrer)
        assets = [ name, "#{ name }.coffee", "#{ name }.js.coffee" ]

        if name =~ /^\.\// # relative?
          path = File.dirname(referrer)
          assets.each do |asset|
            filename = File.join(path, asset)
            return filename if File.exist?(filename)
          end
        else
          assets.each do |asset|
            filename = pipeline.find(asset)
            return filename if filename
          end
        end

        nil
      end

      def extract_requirements(code)
        match = /\A(?:\s*(?:\#.*\n?)+)+/.match(code) or return []
        match[0].split(/\n/).map(&:strip).reject(&:empty?).map do |line|
          match = /^\s*\#\s*=\s*require\s+(.*)$/.match(line)
          match ? match[1].strip.gsub(/(^['"])|(['"]$)/, '') : nil
        end.compact
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rail-0.0.7 lib/rail/processor/coffee_script.rb
rail-0.0.6 lib/rail/processor/coffee_script.rb