Sha256: 419f73af125f8764dfc56412f8459331d0747fbc010e7875900917594dadf128

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require 'commonjs'
require 'pathname'

module Csso
  class Loader
    include CallJS

    attr_reader :environment

    class ModifiedEnvironment < CommonJS::Environment
      # CommonJS' require does append .js to modules no matter if it is there already
      def require module_id
        path = module_id.gsub(/^\.\//, "")
        path = path.gsub(/\.js$/, "")
        super path
      end
    end

    def initialize js_path=nil
      @js_path = js_path || Pathname(__FILE__).dirname.join('js').to_s
      @cxt = V8::Context.new
      @environment = ModifiedEnvironment.new(@cxt, :path => @js_path)

      [Util, Path, Fs].each do |native|
        @environment.native(native.to_s.downcase, native.new)
      end

      [Process, Console].each do|replace|
        @cxt[replace.to_s.downcase] = replace.new
      end
    end

    def require(module_id)
      @environment.require(module_id)
    end

    class Path
      def join(*components)
        File.join(*components)
      end

      def dirname(path)
        File.dirname(path)
      end
    end

    class Util # sys
      def error(*errors)
        raise errors.join(' ')
      end
    end

    class Fs
      def statSync(path)
        File.stat(path)
      end

      def readFile(path, encoding, callback)
        callback.call(nil, File.read(path))
      end
    end

    class Process
      def exit(*args)
      end
    end

    class Console
      def log(*msgs)
        puts msgs.join(',')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csso-rails-0.0.3 lib/csso/loader.rb
csso-rails-0.0.2 lib/csso/loader.rb