|
| 1 | +require 'set' |
| 2 | +require 'sprockets' |
| 3 | + |
| 4 | +$OPAL_SOURCE_MAPS = {} |
| 5 | + |
| 6 | +module Opal |
| 7 | + # Proccess using Sprockets |
| 8 | + # |
| 9 | + # Opal.process('opal-jquery') # => String |
| 10 | + def self.process asset |
| 11 | + Environment.new[asset].to_s |
| 12 | + end |
| 13 | + |
| 14 | + # The Processor class is used to make ruby files (with rb or opal extensions) |
| 15 | + # available to any sprockets based server. Processor will then get passed any |
| 16 | + # ruby source file to build. There are some options you can override globally |
| 17 | + # which effect how certain ruby features are handled: |
| 18 | + # |
| 19 | + # * method_missing_enabled [true by default] |
| 20 | + # * optimized_operators_enabled [true by default] |
| 21 | + # * arity_check_enabled [false by default] |
| 22 | + # * const_missing_enabled [true by default] |
| 23 | + # * dynamic_require_severity [true by default] |
| 24 | + # * source_map_enabled [true by default] |
| 25 | + # * irb_enabled [false by default] |
| 26 | + # |
| 27 | + class Processor < Tilt::Template |
| 28 | + self.default_mime_type = 'application/javascript' |
| 29 | + |
| 30 | + def self.engine_initialized? |
| 31 | + true |
| 32 | + end |
| 33 | + |
| 34 | + class << self |
| 35 | + attr_accessor :method_missing_enabled |
| 36 | + attr_accessor :arity_check_enabled |
| 37 | + attr_accessor :const_missing_enabled |
| 38 | + attr_accessor :dynamic_require_severity |
| 39 | + attr_accessor :source_map_enabled |
| 40 | + attr_accessor :irb_enabled |
| 41 | + end |
| 42 | + |
| 43 | + self.method_missing_enabled = true |
| 44 | + self.arity_check_enabled = false |
| 45 | + self.const_missing_enabled = true |
| 46 | + self.dynamic_require_severity = :error # :error, :warning or :ignore |
| 47 | + self.source_map_enabled = true |
| 48 | + self.irb_enabled = false |
| 49 | + |
| 50 | + def self.stub_file(name) |
| 51 | + stubbed_files << name.to_s |
| 52 | + end |
| 53 | + |
| 54 | + def self.stubbed_files |
| 55 | + @stubbed_files ||= Set.new |
| 56 | + end |
| 57 | + |
| 58 | + def initialize_engine |
| 59 | + require_template_library 'opal' |
| 60 | + end |
| 61 | + |
| 62 | + def prepare |
| 63 | + end |
| 64 | + |
| 65 | + def evaluate(context, locals, &block) |
| 66 | + options = { |
| 67 | + :method_missing => self.class.method_missing_enabled, |
| 68 | + :arity_check => self.class.arity_check_enabled, |
| 69 | + :const_missing => self.class.const_missing_enabled, |
| 70 | + :dynamic_require_severity => self.class.dynamic_require_severity, |
| 71 | + :irb => self.class.irb_enabled, |
| 72 | + :file => context.logical_path, |
| 73 | + } |
| 74 | + |
| 75 | + compiler = Opal::Compiler.new |
| 76 | + result = compiler.compile data, options |
| 77 | + |
| 78 | + compiler.requires.each do |r| |
| 79 | + next if stubbed_file? r |
| 80 | + path = find_opal_require context.environment, r |
| 81 | + context.require_asset path |
| 82 | + end |
| 83 | + |
| 84 | + if self.class.source_map_enabled |
| 85 | + $OPAL_SOURCE_MAPS[context.pathname] = compiler.source_map(source_file_url(context)).to_s |
| 86 | + "#{result}\n//@ sourceMappingURL=#{source_map_url(context)}\n" |
| 87 | + else |
| 88 | + result |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + def source_map_url(context) |
| 93 | + "#{prefix}/#{context.logical_path}.js.map" |
| 94 | + end |
| 95 | + |
| 96 | + def source_file_url(context) |
| 97 | + "#{prefix}/#{context.logical_path.to_s}" |
| 98 | + end |
| 99 | + |
| 100 | + def prefix |
| 101 | + "/__opal_source_maps__" |
| 102 | + end |
| 103 | + |
| 104 | + def stubbed_file?(name) |
| 105 | + self.class.stubbed_files.include? name |
| 106 | + end |
| 107 | + |
| 108 | + def find_opal_require(environment, r) |
| 109 | + path = environment.paths.find do |p| |
| 110 | + File.exist?(File.join(p, "#{r}.rb")) |
| 111 | + end |
| 112 | + |
| 113 | + path ? File.join(path, "#{r}.rb") : r |
| 114 | + end |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +Tilt.register 'rb', Opal::Processor |
| 119 | +Sprockets.register_engine '.rb', Opal::Processor |
| 120 | + |
| 121 | +Tilt.register 'opal', Opal::Processor |
| 122 | +Sprockets.register_engine '.opal', Opal::Processor |
0 commit comments