coffee_script.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. require 'execjs'
  2. require 'coffee_script/source'
  3. module CoffeeScript
  4. EngineError = ExecJS::RuntimeError
  5. CompilationError = ExecJS::ProgramError
  6. module Source
  7. def self.path
  8. @path ||= ENV['COFFEESCRIPT_SOURCE_PATH'] || bundled_path
  9. end
  10. def self.path=(path)
  11. @contents = @version = @bare_option = @context = nil
  12. @path = path
  13. end
  14. def self.contents
  15. @contents ||= File.read(path)
  16. end
  17. def self.version
  18. @version ||= contents[/CoffeeScript Compiler v([\d.]+)/, 1]
  19. end
  20. def self.bare_option
  21. @bare_option ||= contents.match(/noWrap/) ? 'noWrap' : 'bare'
  22. end
  23. def self.context
  24. @context ||= ExecJS.compile(contents)
  25. end
  26. end
  27. class << self
  28. def engine
  29. end
  30. def engine=(engine)
  31. end
  32. def version
  33. Source.version
  34. end
  35. # Compile a script (String or IO) to JavaScript.
  36. def compile(script, options = {})
  37. script = script.read if script.respond_to?(:read)
  38. if options.key?(:bare)
  39. elsif options.key?(:no_wrap)
  40. options[:bare] = options[:no_wrap]
  41. else
  42. options[:bare] = false
  43. end
  44. Source.context.call("CoffeeScript.compile", script, options)
  45. end
  46. end
  47. end