Skip to content

Commit 86fcf97

Browse files
committedNov 30, 2013
Merge Processor/Server from opal-sprockets
1 parent 3da7a07 commit 86fcf97

File tree

15 files changed

+391
-18
lines changed

15 files changed

+391
-18
lines changed
 

‎Gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
source 'https://rubygems.org'
22
gemspec
33

4-
gem 'opal-sprockets', :github => 'opal/opal-sprockets'
5-
64
# Stick with older racc until
75
# https://github.com/tenderlove/racc/issues/32
86
# is solved.

‎example/Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'opal', :path => '../'

‎example/app/application.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'opal'
2+
require 'user'
3+
4+
module MyApp
5+
class Application
6+
def initialize
7+
@user = User.new('Bill')
8+
@user.authenticated?
9+
end
10+
end
11+
end
12+
13+
MyApp::Application.new

‎example/app/user.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class User
2+
def initialize(name)
3+
@name = name
4+
end
5+
6+
def authenticated?
7+
if admin? or special_persmission?
8+
true
9+
else
10+
raise "not authenticated"
11+
end
12+
end
13+
14+
def admin?
15+
@name == 'Bob'
16+
end
17+
18+
def special_persmission?
19+
false
20+
end
21+
end

‎example/config.ru

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'bundler'
2+
Bundler.require
3+
4+
run Opal::Server.new { |s|
5+
s.main = 'application'
6+
s.append_path 'app'
7+
}

‎example/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>opal-sprockets demo</title>
5+
6+
<%= javascript_include_tag 'application' %>
7+
</head>
8+
<body>
9+
</body>
10+
</html>

‎lib/mspec/opal/rake_task.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Opal::Nodes::CallNode
2121

2222
require 'rack'
2323
require 'webrick'
24-
require 'opal-sprockets'
24+
2525
module MSpec
2626
module Opal
2727
DEFAULT_PATTERN = 'spec/opal/{parser,core,compiler,stdlib}/**/*_spec.rb'

‎lib/opal.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'opal/compiler'
22
require 'opal/builder'
33
require 'opal/erb'
4+
require 'opal/sprockets'
45
require 'opal/version'
56

67
# Opal is a ruby to javascript compiler, with a runtime for running

‎lib/opal/cli.rb

-14
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def show_sexp
123123
# PROCESSOR
124124

125125
def set_processor_options
126-
require_opal_sprockets
127126
processor_options.each do |option|
128127
key = option.to_sym
129128
next unless options.has_key? key
@@ -160,7 +159,6 @@ def sprockets
160159
end
161160

162161
def server
163-
require_opal_sprockets
164162
@server ||= Opal::Server.new do |s|
165163
load_paths.each do |path|
166164
s.append_path path
@@ -169,18 +167,6 @@ def server
169167
end
170168
end
171169

172-
def require_opal_sprockets
173-
begin
174-
require 'opal-sprockets'
175-
rescue LoadError
176-
$stderr.puts 'Opal executable requires opal-sprockets to be fully functional.'
177-
$stderr.puts 'You can install it with rubygems:'
178-
$stderr.puts ''
179-
$stderr.puts ' gem install opal-sprockets'
180-
exit -1
181-
end
182-
end
183-
184170
##
185171
# OUTPUT
186172

‎lib/opal/sprockets.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'opal/sprockets/environment'
2+
require 'opal/sprockets/processor'
3+
require 'opal/sprockets/erb'
4+
require 'opal/sprockets/server'

‎lib/opal/sprockets/environment.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'sprockets'
2+
3+
module Opal
4+
# Environment is a subclass of Sprockets::Environment which already has our opal
5+
# load paths loaded. This makes it easy for stand-alone rack apps, or test runners
6+
# that have opal load paths ready to use. You can also add an existing gem's lib
7+
# directory to our load path to use real gems inside your opal environment.
8+
#
9+
# If you are running rails, then you just need opal-rails instead, which will
10+
# do this for you.
11+
class Environment < ::Sprockets::Environment
12+
def initialize *args
13+
super
14+
Opal.paths.each { |p| append_path p }
15+
end
16+
17+
def use_gem gem_name
18+
append_path File.join(Gem::Specification.find_by_name(gem_name).gem_dir, 'lib')
19+
end
20+
end
21+
end

‎lib/opal/sprockets/erb.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'opal'
2+
require 'opal/compiler'
3+
require 'sprockets'
4+
5+
module Opal
6+
module ERB
7+
class Processor < Tilt::Template
8+
self.default_mime_type = 'application/javascript'
9+
10+
def self.engine_initialized?
11+
true
12+
end
13+
14+
def initialize_engine
15+
require_template_library 'opal'
16+
end
17+
18+
def prepare
19+
end
20+
21+
def evaluate(scope, locals, &block)
22+
Opal::ERB.compile data, scope.logical_path.sub(/^templates\//, '')
23+
end
24+
end
25+
end
26+
end
27+
28+
Tilt.register 'opalerb', Opal::ERB::Processor
29+
Sprockets.register_engine '.opalerb', Opal::ERB::Processor

‎lib/opal/sprockets/processor.rb

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

‎lib/opal/sprockets/server.rb

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
require 'rack/file'
2+
require 'rack/urlmap'
3+
require 'rack/builder'
4+
require 'rack/directory'
5+
require 'rack/showexceptions'
6+
require 'opal/source_map'
7+
require 'opal/sprockets/environment'
8+
require 'erb'
9+
10+
module Opal
11+
12+
class SourceMapServer
13+
def initialize sprockets
14+
@sprockets = sprockets
15+
end
16+
17+
attr_reader :sprockets
18+
19+
attr_writer :prefix
20+
21+
def prefix
22+
@prefix ||= '/__opal_source_maps__'
23+
end
24+
25+
def inspect
26+
"#<#{self.class}:#{object_id}>"
27+
end
28+
29+
def call(env)
30+
path_info = env['PATH_INFO']
31+
32+
if path_info =~ /\.js\.map$/
33+
path = env['PATH_INFO'].gsub(/^\/|\.js\.map$/, '')
34+
asset = sprockets[path]
35+
return [404, {}, []] if asset.nil?
36+
37+
return [200, {"Content-Type" => "text/json"}, [$OPAL_SOURCE_MAPS[asset.pathname].to_s]]
38+
else
39+
return [200, {"Content-Type" => "text/text"}, [File.read(sprockets.resolve(path_info))]]
40+
end
41+
end
42+
end
43+
44+
class Server
45+
46+
attr_accessor :debug, :index_path, :main, :public_dir, :sprockets
47+
48+
def initialize debug_or_options = {}
49+
unless Hash === debug_or_options
50+
warn "passing a boolean to control debug is deprecated.\n"+
51+
"Please pass an Hash instead: Server.new(debug: true)"
52+
options = {:debug => debug_or_options}
53+
else
54+
options = debug_or_options
55+
end
56+
57+
@public_dir = '.'
58+
@sprockets = Environment.new
59+
@debug = options.fetch(:debug, true)
60+
61+
yield self if block_given?
62+
create_app
63+
end
64+
65+
def source_map_enabled
66+
Opal::Processor.source_map_enabled
67+
end
68+
69+
def append_path path
70+
@sprockets.append_path path
71+
end
72+
73+
def use_gem gem_name
74+
@sprockets.use_gem gem_name
75+
end
76+
77+
def create_app
78+
server, sprockets = self, @sprockets
79+
80+
@app = Rack::Builder.app do
81+
use Rack::ShowExceptions
82+
map('/assets') { run sprockets }
83+
map(server.source_maps.prefix) { run server.source_maps } if server.source_map_enabled
84+
use Index, server
85+
run Rack::Directory.new(server.public_dir)
86+
end
87+
end
88+
89+
def source_maps
90+
@source_maps ||= SourceMapServer.new(@sprockets)
91+
end
92+
93+
def call(env)
94+
@app.call env
95+
end
96+
97+
class Index
98+
99+
def initialize(app, server)
100+
@app = app
101+
@server = server
102+
@index_path = server.index_path
103+
end
104+
105+
def call(env)
106+
if %w[/ /index.html].include? env['PATH_INFO']
107+
[200, { 'Content-Type' => 'text/html' }, [html]]
108+
else
109+
@app.call env
110+
end
111+
end
112+
113+
# Returns the html content for the root path. Supports ERB
114+
def html
115+
source = if @index_path
116+
raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
117+
File.read @index_path
118+
elsif File.exist? 'index.html'
119+
File.read 'index.html'
120+
elsif File.exist? 'index.html.erb'
121+
File.read 'index.html.erb'
122+
else
123+
SOURCE
124+
end
125+
126+
::ERB.new(source).result binding
127+
end
128+
129+
def javascript_include_tag source
130+
if @server.debug
131+
assets = @server.sprockets[source].to_a
132+
133+
raise "Cannot find asset: #{source}" if assets.empty?
134+
135+
scripts = assets.map do |a|
136+
%Q{<script src="/assets/#{ a.logical_path }?body=1"></script>}
137+
end
138+
139+
scripts.join "\n"
140+
else
141+
"<script src=\"/assets/#{source}.js\"></script>"
142+
end
143+
end
144+
145+
SOURCE = <<-HTML
146+
<!DOCTYPE html>
147+
<html>
148+
<head>
149+
<title>Opal Server</title>
150+
</head>
151+
<body>
152+
<%= javascript_include_tag @server.main %>
153+
</body>
154+
</html>
155+
HTML
156+
end
157+
end
158+
end

‎opal.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Gem::Specification.new do |s|
1919
s.require_paths = ['lib']
2020

2121
s.add_dependency 'source_map'
22+
s.add_dependency 'sprockets'
2223

2324
s.add_development_dependency 'mspec', '1.5.20'
2425
s.add_development_dependency 'rake'
2526
s.add_development_dependency 'racc'
26-
s.add_development_dependency 'opal-sprockets', '~> 0.4.0'
2727
s.add_development_dependency 'rspec', '~> 2.14'
2828
s.add_development_dependency 'octokit', '~> 2.4.0'
2929
end

0 commit comments

Comments
 (0)
Please sign in to comment.