Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge Processor/Server from opal-sprockets
- Loading branch information
1 parent
3da7a07
commit 86fcf97
Showing
15 changed files
with
391 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'opal', :path => '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'bundler' | ||
Bundler.require | ||
|
||
run Opal::Server.new { |s| | ||
s.main = 'application' | ||
s.append_path 'app' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>opal-sprockets demo</title> | ||
|
||
<%= javascript_include_tag 'application' %> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'opal/sprockets/environment' | ||
require 'opal/sprockets/processor' | ||
require 'opal/sprockets/erb' | ||
require 'opal/sprockets/server' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.