Skip to content

Commit

Permalink
Merge Processor/Server from opal-sprockets
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 30, 2013
1 parent 3da7a07 commit 86fcf97
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Gemfile
@@ -1,8 +1,6 @@
source 'https://rubygems.org'
gemspec

gem 'opal-sprockets', :github => 'opal/opal-sprockets'

# Stick with older racc until
# https://github.com/tenderlove/racc/issues/32
# is solved.
Expand Down
3 changes: 3 additions & 0 deletions example/Gemfile
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'opal', :path => '../'
13 changes: 13 additions & 0 deletions example/app/application.rb
@@ -0,0 +1,13 @@
require 'opal'
require 'user'

module MyApp
class Application
def initialize
@user = User.new('Bill')
@user.authenticated?
end
end
end

MyApp::Application.new
21 changes: 21 additions & 0 deletions example/app/user.rb
@@ -0,0 +1,21 @@
class User
def initialize(name)
@name = name
end

def authenticated?
if admin? or special_persmission?
true
else
raise "not authenticated"
end
end

def admin?
@name == 'Bob'
end

def special_persmission?
false
end
end
7 changes: 7 additions & 0 deletions example/config.ru
@@ -0,0 +1,7 @@
require 'bundler'
Bundler.require

run Opal::Server.new { |s|
s.main = 'application'
s.append_path 'app'
}
10 changes: 10 additions & 0 deletions example/index.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>opal-sprockets demo</title>

<%= javascript_include_tag 'application' %>
</head>
<body>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/mspec/opal/rake_task.rb
Expand Up @@ -21,7 +21,7 @@ class Opal::Nodes::CallNode

require 'rack'
require 'webrick'
require 'opal-sprockets'

module MSpec
module Opal
DEFAULT_PATTERN = 'spec/opal/{parser,core,compiler,stdlib}/**/*_spec.rb'
Expand Down
1 change: 1 addition & 0 deletions lib/opal.rb
@@ -1,6 +1,7 @@
require 'opal/compiler'
require 'opal/builder'
require 'opal/erb'
require 'opal/sprockets'
require 'opal/version'

# Opal is a ruby to javascript compiler, with a runtime for running
Expand Down
14 changes: 0 additions & 14 deletions lib/opal/cli.rb
Expand Up @@ -123,7 +123,6 @@ def show_sexp
# PROCESSOR

def set_processor_options
require_opal_sprockets
processor_options.each do |option|
key = option.to_sym
next unless options.has_key? key
Expand Down Expand Up @@ -160,7 +159,6 @@ def sprockets
end

def server
require_opal_sprockets
@server ||= Opal::Server.new do |s|
load_paths.each do |path|
s.append_path path
Expand All @@ -169,18 +167,6 @@ def server
end
end

def require_opal_sprockets
begin
require 'opal-sprockets'
rescue LoadError
$stderr.puts 'Opal executable requires opal-sprockets to be fully functional.'
$stderr.puts 'You can install it with rubygems:'
$stderr.puts ''
$stderr.puts ' gem install opal-sprockets'
exit -1
end
end

##
# OUTPUT

Expand Down
4 changes: 4 additions & 0 deletions lib/opal/sprockets.rb
@@ -0,0 +1,4 @@
require 'opal/sprockets/environment'
require 'opal/sprockets/processor'
require 'opal/sprockets/erb'
require 'opal/sprockets/server'
21 changes: 21 additions & 0 deletions lib/opal/sprockets/environment.rb
@@ -0,0 +1,21 @@
require 'sprockets'

module Opal
# Environment is a subclass of Sprockets::Environment which already has our opal
# load paths loaded. This makes it easy for stand-alone rack apps, or test runners
# that have opal load paths ready to use. You can also add an existing gem's lib
# directory to our load path to use real gems inside your opal environment.
#
# If you are running rails, then you just need opal-rails instead, which will
# do this for you.
class Environment < ::Sprockets::Environment
def initialize *args
super
Opal.paths.each { |p| append_path p }
end

def use_gem gem_name
append_path File.join(Gem::Specification.find_by_name(gem_name).gem_dir, 'lib')
end
end
end
29 changes: 29 additions & 0 deletions lib/opal/sprockets/erb.rb
@@ -0,0 +1,29 @@
require 'opal'
require 'opal/compiler'
require 'sprockets'

module Opal
module ERB
class Processor < Tilt::Template
self.default_mime_type = 'application/javascript'

def self.engine_initialized?
true
end

def initialize_engine
require_template_library 'opal'
end

def prepare
end

def evaluate(scope, locals, &block)
Opal::ERB.compile data, scope.logical_path.sub(/^templates\//, '')
end
end
end
end

Tilt.register 'opalerb', Opal::ERB::Processor
Sprockets.register_engine '.opalerb', Opal::ERB::Processor
122 changes: 122 additions & 0 deletions lib/opal/sprockets/processor.rb
@@ -0,0 +1,122 @@
require 'set'
require 'sprockets'

$OPAL_SOURCE_MAPS = {}

module Opal
# Proccess using Sprockets
#
# Opal.process('opal-jquery') # => String
def self.process asset
Environment.new[asset].to_s
end

# The Processor class is used to make ruby files (with rb or opal extensions)
# available to any sprockets based server. Processor will then get passed any
# ruby source file to build. There are some options you can override globally
# which effect how certain ruby features are handled:
#
# * method_missing_enabled [true by default]
# * optimized_operators_enabled [true by default]
# * arity_check_enabled [false by default]
# * const_missing_enabled [true by default]
# * dynamic_require_severity [true by default]
# * source_map_enabled [true by default]
# * irb_enabled [false by default]
#
class Processor < Tilt::Template
self.default_mime_type = 'application/javascript'

def self.engine_initialized?
true
end

class << self
attr_accessor :method_missing_enabled
attr_accessor :arity_check_enabled
attr_accessor :const_missing_enabled
attr_accessor :dynamic_require_severity
attr_accessor :source_map_enabled
attr_accessor :irb_enabled
end

self.method_missing_enabled = true
self.arity_check_enabled = false
self.const_missing_enabled = true
self.dynamic_require_severity = :error # :error, :warning or :ignore
self.source_map_enabled = true
self.irb_enabled = false

def self.stub_file(name)
stubbed_files << name.to_s
end

def self.stubbed_files
@stubbed_files ||= Set.new
end

def initialize_engine
require_template_library 'opal'
end

def prepare
end

def evaluate(context, locals, &block)
options = {
:method_missing => self.class.method_missing_enabled,
:arity_check => self.class.arity_check_enabled,
:const_missing => self.class.const_missing_enabled,
:dynamic_require_severity => self.class.dynamic_require_severity,
:irb => self.class.irb_enabled,
:file => context.logical_path,
}

compiler = Opal::Compiler.new
result = compiler.compile data, options

compiler.requires.each do |r|
next if stubbed_file? r
path = find_opal_require context.environment, r
context.require_asset path
end

if self.class.source_map_enabled
$OPAL_SOURCE_MAPS[context.pathname] = compiler.source_map(source_file_url(context)).to_s
"#{result}\n//@ sourceMappingURL=#{source_map_url(context)}\n"
else
result
end
end

def source_map_url(context)
"#{prefix}/#{context.logical_path}.js.map"
end

def source_file_url(context)
"#{prefix}/#{context.logical_path.to_s}"
end

def prefix
"/__opal_source_maps__"
end

def stubbed_file?(name)
self.class.stubbed_files.include? name
end

def find_opal_require(environment, r)
path = environment.paths.find do |p|
File.exist?(File.join(p, "#{r}.rb"))
end

path ? File.join(path, "#{r}.rb") : r
end
end
end

Tilt.register 'rb', Opal::Processor
Sprockets.register_engine '.rb', Opal::Processor

Tilt.register 'opal', Opal::Processor
Sprockets.register_engine '.opal', Opal::Processor

0 comments on commit 86fcf97

Please sign in to comment.