opal-sprockets 0.3.0 → 0.4.0.0.10.0.3.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +11 -0
  3. data/.travis.yml +11 -4
  4. data/Gemfile +6 -0
  5. data/README.md +1 -1
  6. data/Rakefile +1 -0
  7. data/example/app/application.rb +11 -2
  8. data/example/app/user.rb +21 -0
  9. data/example/config.ru +1 -1
  10. data/lib/opal/sprockets/version.rb +4 -1
  11. data/lib/opal-sprockets.rb +4 -0
  12. data/opal-sprockets.gemspec +4 -2
  13. data/spec/fixtures/complex_sprockets.js.rb.erb +5 -0
  14. data/spec/fixtures/file_with_directives.js +2 -0
  15. data/spec/fixtures/jst_file.js.jst +1 -0
  16. data/spec/fixtures/no_requires.rb +1 -0
  17. data/spec/fixtures/opal_file.rb +2 -0
  18. data/spec/fixtures/require_tree_test.rb +3 -0
  19. data/spec/fixtures/required_file.js +1 -0
  20. data/spec/fixtures/required_tree_test/required_file1.rb +1 -0
  21. data/spec/fixtures/required_tree_test/required_file2.rb +1 -0
  22. data/spec/fixtures/requires.rb +7 -0
  23. data/spec/fixtures/source_map/subfolder/other_file.rb +1 -0
  24. data/spec/fixtures/source_map.rb +1 -0
  25. data/spec/fixtures/sprockets_file.js.rb +3 -0
  26. data/spec/fixtures/sprockets_require_tree_test.rb +3 -0
  27. data/spec/shared/path_finder_shared.rb +19 -0
  28. data/spec/shared/path_reader_shared.rb +31 -0
  29. data/spec/sprockets/erb_spec.rb +38 -0
  30. data/spec/sprockets/path_reader_spec.rb +41 -0
  31. data/spec/sprockets/processor_spec.rb +79 -0
  32. data/spec/sprockets/server_spec.rb +103 -0
  33. data/spec/sprockets_spec.rb +32 -0
  34. data/spec/tilt/opal_spec.rb +37 -0
  35. metadata +94 -35
  36. data/example/app/foo.rb +0 -7
  37. data/lib/opal/sprockets/environment.rb +0 -22
  38. data/lib/opal/sprockets/erb.rb +0 -29
  39. data/lib/opal/sprockets/processor.rb +0 -124
  40. data/lib/opal/sprockets/server.rb +0 -159
  41. data/lib/opal/sprockets.rb +0 -4
  42. data/spec/fixtures/app/foo.rb +0 -0
  43. data/spec/fixtures/app2/bar.js +0 -0
  44. data/spec/opal/sprockets/server_spec.rb +0 -15
  45. data/spec/processor_spec.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8b04180f54f9eacd9d9747b07f672fd7d0a3eec
4
- data.tar.gz: d99d41d4c89bfee3986ad9c1a3a31116ae72c5f1
3
+ metadata.gz: db4efadd12763aeaa629cc26f864cf137e8bbfeb
4
+ data.tar.gz: 8833ebcf05c70a990f8c6ef7246e7f952ec70cb3
5
5
  SHA512:
6
- metadata.gz: d2a6c9e34bb10d7e70b68b9e4564821cbad9fec5add260261e5a26405b18b8c4d6e8e80008a3b7861db5b9edc6acbc6290bc443ec03b71a3136169d7b30bd796
7
- data.tar.gz: a65a54c75521c62ac96e210dcdc1bed5d68c8e3e45c3bc62a38261364180113f3ca58059972b2ca0a64e18daba9b2a8517eac6ca89e97e77784f219616f9a764
6
+ metadata.gz: bc56ecd71399b8a31ddb3216061e04b8bd698f270679e07b8e3c486883ddb88af1a948ded8f34b5371f8f760905f71e347b9a2f25aaa7a74f62fc986f6da05c8
7
+ data.tar.gz: 6d9eb5fc2430d60abfd8447788aa3ace748de305b65f76703337dfe027c1685826dbbdd9f88477b241d6d942e47cc8032ecab47797b8576b4716a71addc9ea85
data/.gitignore CHANGED
@@ -1,2 +1,13 @@
1
1
  .DS_Store
2
+ *.gem
3
+ .yardoc
4
+ .bundle
2
5
  Gemfile.lock
6
+ build/
7
+ /.github_access_token
8
+ /tmp
9
+ .ruby-gemset
10
+ .ruby-version
11
+ .rvmrc
12
+ .rspec-local
13
+ /coverage
data/.travis.yml CHANGED
@@ -1,9 +1,16 @@
1
1
  language: ruby
2
2
 
3
- rvm:
4
- - 1.8.7
5
- - 1.9.3
6
- - 2.0.0
3
+ matrix:
4
+ include:
5
+ - rvm: 2.0.0
6
+ env: RACK_VERSION='< 2.0.0' SPROCKETS_VERSION='~> 3.4.1'
7
+ - rvm: 2.1.10
8
+ env: RACK_VERSION='< 2.0.0' SPROCKETS_VERSION='~> 3.5.2'
9
+ - rvm: 2.2.5
10
+ env: SPROCKETS_VERSION='~> 3.6.3'
11
+ - rvm: 2.3.1
12
+ env: SPROCKETS_VERSION='~> 3.7.0'
13
+ - rvm: 2.4.0-preview1
7
14
 
8
15
  notifications:
9
16
  irc: "irc.freenode.org#opal"
data/Gemfile CHANGED
@@ -1,2 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
+
4
+ rack_version = ENV['RACK_VERSION']
5
+ sprockets_version = ENV['SPROCKETS_VERSION']
6
+
7
+ gem 'rack', rack_version if rack_version
8
+ gem 'sprockets', sprockets_version if sprockets_version
data/README.md CHANGED
@@ -79,7 +79,7 @@ configure applications inside `config.ru`:
79
79
  require 'bundler'
80
80
  Bundler.require
81
81
 
82
- run Opal::Server.new { |s|
82
+ run Opal::Sprockets::Server.new { |s|
83
83
  s.append_path 'app'
84
84
 
85
85
  s.main = 'application'
data/Rakefile CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
+ Bundler::GemHelper.install_tasks
3
4
 
4
5
  require 'rspec/core/rake_task'
5
6
 
@@ -1,4 +1,13 @@
1
1
  require 'opal'
2
- require 'foo'
2
+ require 'user'
3
3
 
4
- puts "wow!"
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
@@ -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
data/example/config.ru CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
3
 
4
- run Opal::Server.new { |s|
4
+ run Opal::Sprockets::Server.new { |s|
5
5
  s.main = 'application'
6
6
  s.append_path 'app'
7
7
  }
@@ -1,5 +1,8 @@
1
1
  module Opal
2
2
  module Sprockets
3
- VERSION = '0.3.0'
3
+ BASE_VERSION = '0.4.0'
4
+ OPAL_VERSION = '0.10.0'
5
+ SPROCKETS_VERSION = '3.0'
6
+ VERSION = "#{BASE_VERSION}.#{OPAL_VERSION}.#{SPROCKETS_VERSION}.beta1"
4
7
  end
5
8
  end
@@ -1 +1,5 @@
1
+ require 'opal'
1
2
  require 'opal/sprockets'
3
+ require 'opal/sprockets/version'
4
+
5
+ Opal::Sprockets::Server = ::Opal::Server
@@ -14,9 +14,11 @@ Gem::Specification.new do |s|
14
14
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
15
  s.require_paths = ['lib']
16
16
 
17
- s.add_dependency 'sprockets'
18
- s.add_dependency 'opal', '~> 0.5.0'
17
+ s.add_dependency 'sprockets', "~> #{Opal::Sprockets::SPROCKETS_VERSION}"
18
+ s.add_dependency 'opal', "~> #{Opal::Sprockets::OPAL_VERSION}"
19
+ s.add_dependency 'tilt', '>= 1.4'
19
20
 
20
21
  s.add_development_dependency 'rake'
21
22
  s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'rack-test'
22
24
  end
@@ -0,0 +1,5 @@
1
+ #= require no_requires
2
+ //= require jst_file
3
+ #= require_tree ./required_tree_test
4
+ require 'file_with_directives'
5
+ require <%= "base64".inspect %>
@@ -0,0 +1,2 @@
1
+ //= require required_file
2
+ console.log('file with directives');
@@ -0,0 +1 @@
1
+ #= require sprockets_file
@@ -0,0 +1 @@
1
+ puts 'hi there'
@@ -0,0 +1,2 @@
1
+ require 'opal'
2
+ puts 'hi from opal!'
@@ -0,0 +1,3 @@
1
+ require_tree '../fixtures/required_tree_test'
2
+
3
+ puts 5
@@ -0,0 +1 @@
1
+ console.log('required file');
@@ -0,0 +1,7 @@
1
+ require 'foo'
2
+
3
+ puts 'hello'
4
+
5
+ require 'bar'
6
+
7
+ puts 'goodbye'
@@ -0,0 +1 @@
1
+ puts 'other!'
@@ -0,0 +1 @@
1
+ puts 'smap!'
@@ -0,0 +1,3 @@
1
+ require 'opal'
2
+ require 'native'
3
+ puts 'sprockets!'
@@ -0,0 +1,3 @@
1
+ #=require_tree ./required_tree_test
2
+
3
+ puts 5
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples :path_finder do
4
+ # @param path
5
+ # the path we want to read
6
+ #
7
+ # @param full_path
8
+ # the expanded path that should be found
9
+ #
10
+ it 'returns the full path if the path exists' do
11
+ expect(path_finder.find(path)).to eq(full_path)
12
+ end
13
+
14
+ it 'returns nil if the path is missing' do
15
+ expect(path_finder.find('unexpected-path')).to eq(nil)
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ # Below the helpers expected from a spec that
4
+ # includes these shared examples:
5
+ #
6
+ # @object [PathReader] path_reader the object under test
7
+ # @method [String] path the path we want to read
8
+ # @method [String] contents the contents we expect to be read
9
+ #
10
+ shared_examples :path_reader do
11
+ describe '#paths' do
12
+ it 'is an Enumberable' do
13
+ expect(path_reader.paths).to be_an(Enumerable)
14
+ end
15
+
16
+ it 'includes Opal.paths' do
17
+ paths = path_reader.paths.to_a
18
+ Opal.paths.each { |path| expect(paths).to include(path) }
19
+ end
20
+ end
21
+
22
+ describe '#read' do
23
+ it 'responds to #path' do
24
+ expect(path_reader.read(path)).to eq(contents)
25
+ end
26
+
27
+ it 'returns nil if the file is missing' do
28
+ expect(path_reader.read('unexpected-path!')).to be_nil
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'opal/sprockets/erb'
3
+
4
+ describe Opal::ERB::Processor do
5
+ let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
6
+ let(:environment) { double(Sprockets::Environment,
7
+ cache: nil,
8
+ :[] => nil,
9
+ resolve: pathname.expand_path.to_s,
10
+ engines: double(keys: %w[.rb .js .erb .opal]),
11
+ ) }
12
+ let(:sprockets_context) { double(Sprockets::Context,
13
+ logical_path: "foo.#{ext}",
14
+ environment: environment,
15
+ pathname: pathname,
16
+ filename: pathname.to_s,
17
+ root_path: '/Code/app/mylib',
18
+ is_a?: true,
19
+ ) }
20
+ let(:required_assets) { [] }
21
+ let(:template) { described_class.new { |t| %Q{<% print("") %><a href="<%= url %>"><%= name %></a>} } }
22
+ before { allow(sprockets_context).to receive(:require_asset) {|asset| required_assets << asset } }
23
+
24
+ let(:ext) { 'opalerb' }
25
+
26
+ it "is registered for '.opalerb' files" do
27
+ expect(Tilt["test.#{ext}"]).to eq(described_class)
28
+ end
29
+
30
+ it 'renders the template' do
31
+ expect(template.render(sprockets_context)).to include('"<a href=\""')
32
+ end
33
+
34
+ it 'implicitly requires "erb"' do
35
+ template.render(sprockets_context)
36
+ expect(required_assets).to eq(['erb'])
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'shared/path_reader_shared'
3
+ require 'opal/sprockets/path_reader'
4
+
5
+ describe Opal::Sprockets::PathReader do
6
+ let(:env) { Sprockets::Environment.new }
7
+ let(:context) { double('context', depend_on: nil, depend_on_asset: nil) }
8
+ let(:contents) { File.read(full_path) }
9
+ let(:full_path) { fixtures_dir.join(logical_path+'.js.rb') }
10
+ let(:logical_path) { 'sprockets_file' }
11
+ let(:fixtures_dir) { Pathname('../../fixtures/').expand_path(__FILE__) }
12
+
13
+ subject(:path_reader) { described_class.new(env, context) }
14
+
15
+ before do
16
+ Opal.paths.each {|p| env.append_path(p)}
17
+ env.append_path fixtures_dir
18
+ end
19
+
20
+ include_examples :path_reader do
21
+ let(:path) { logical_path }
22
+ end
23
+
24
+ it 'can read stuff from sprockets env' do
25
+ expect(path_reader.read(logical_path)).to eq(contents)
26
+ end
27
+
28
+ it 'reads js files processing their directives' do
29
+ path = 'file_with_directives.js'
30
+ full_path = fixtures_dir.join(path)
31
+ required_contents = File.read(fixtures_dir.join('required_file.js')).strip
32
+ read_contents = path_reader.read(path)
33
+ actual_contents = full_path.read
34
+
35
+ expect(actual_contents).to include('//= require')
36
+ expect(read_contents).not_to include('//= require')
37
+
38
+ expect(read_contents).to include(required_contents)
39
+ expect(actual_contents).not_to include(required_contents)
40
+ end
41
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+ require 'opal/sprockets/processor'
3
+
4
+ describe Opal::Processor do
5
+ let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
6
+ let(:environment) { double(Sprockets::Environment,
7
+ cache: nil,
8
+ :[] => nil,
9
+ resolve: pathname.expand_path.to_s,
10
+ engines: double(keys: %w[.rb .js .opal]),
11
+ ) }
12
+ let(:sprockets_context) { double(Sprockets::Context,
13
+ logical_path: "foo.#{ext}",
14
+ environment: environment,
15
+ pathname: pathname,
16
+ filename: pathname.to_s,
17
+ root_path: '/Code/app/mylib',
18
+ is_a?: true,
19
+ ) }
20
+
21
+ %w[.rb .opal].each do |ext|
22
+ let(:ext) { ext }
23
+
24
+ describe %Q{with extension "#{ext}"} do
25
+ it "is registered for '#{ext}' files" do
26
+ expect(Sprockets.engines[ext]).to eq(described_class)
27
+ end
28
+
29
+ it "compiles and evaluates the template on #render" do
30
+ template = described_class.new { |t| "puts 'Hello, World!'\n" }
31
+ expect(template.render(sprockets_context)).to include('"Hello, World!"')
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.stubbed_files' do
37
+ around do |e|
38
+ config.stubbed_files.clear
39
+ e.run
40
+ config.stubbed_files.clear
41
+ end
42
+
43
+ let(:stubbed_file) { 'foo' }
44
+ let(:template) { described_class.new { |t| "require #{stubbed_file.inspect}" } }
45
+ let(:config) { Opal::Config }
46
+
47
+ it 'usually require files' do
48
+ expect(sprockets_context).to receive(:require_asset).with(stubbed_file)
49
+ template.render(sprockets_context)
50
+ end
51
+
52
+ it 'skips require of stubbed file' do
53
+ config.stubbed_files << stubbed_file.to_s
54
+ expect(sprockets_context).not_to receive(:require_asset).with(stubbed_file)
55
+ template.render(sprockets_context)
56
+ end
57
+
58
+ it 'marks a stubbed file as loaded' do
59
+ config.stubbed_files << stubbed_file.to_s
60
+ asset = double(dependencies: [], pathname: Pathname('bar'), logical_path: 'bar')
61
+ allow(environment).to receive(:[]).with('bar.js') { asset }
62
+ allow(environment).to receive(:engines) { {'.rb' => described_class, '.opal' => described_class} }
63
+
64
+ code = ::Opal::Sprockets.load_asset('bar', environment)
65
+ expect(code).to match(stubbed_file)
66
+ end
67
+ end
68
+
69
+ describe '.cache_key' do
70
+ it 'can be reset' do
71
+ Opal::Config.arity_check_enabled = true
72
+ old_cache_key = described_class.cache_key
73
+ Opal::Config.arity_check_enabled = false
74
+ expect(described_class.cache_key).to eq(old_cache_key)
75
+ described_class.reset_cache_key!
76
+ expect(described_class.cache_key).not_to eq(old_cache_key)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require 'sourcemap'
3
+ require 'rack/test'
4
+
5
+ describe Opal::Sprockets::Server do
6
+ include Rack::Test::Methods
7
+
8
+ let(:maps_prefix) { described_class::SOURCE_MAPS_PREFIX_PATH }
9
+
10
+ def app
11
+ described_class.new { |s|
12
+ s.main = 'opal'
13
+ s.debug = false
14
+ s.append_path File.expand_path('../../fixtures', __FILE__)
15
+ s.sprockets.logger = Logger.new(nil)
16
+ }
17
+ end
18
+
19
+ it 'serves assets from /assets' do
20
+ get '/assets/opal.js'
21
+ expect(last_response).to be_ok
22
+ end
23
+
24
+ it 'serves assets with complex sprockets requires' do
25
+ asset = app.sprockets['complex_sprockets']
26
+ expect(asset).to be_truthy
27
+
28
+ assets = asset.to_a.map do |sub_asset|
29
+ sub_asset.logical_path.gsub(/\.self\.js$/, '.js')
30
+ end
31
+
32
+ %w[
33
+ base64.js
34
+ no_requires.js
35
+ jst_file.js
36
+ required_tree_test/required_file1.js
37
+ required_tree_test/required_file2.js
38
+ file_with_directives.js
39
+ ].each do |logical_path|
40
+ expect(assets).to include(logical_path)
41
+ end
42
+ end
43
+
44
+ describe 'source maps' do
45
+ it 'serves map on a top level file' do
46
+ get '/assets/source_map.js'
47
+ expect(last_response).to be_ok
48
+
49
+ get maps_prefix+'/source_map.self.map'
50
+ expect(last_response).to be_ok
51
+ end
52
+
53
+ it 'serves map on a subfolder file' do
54
+ js_path = '/assets/source_map/subfolder/other_file.self.js'
55
+ map_path = maps_prefix+'/source_map/subfolder/other_file.self.map'
56
+
57
+ get js_path
58
+
59
+ expect(last_response).to be_ok
60
+ received_map_path = extract_map_path(last_response)
61
+ expect(expand_path(received_map_path, js_path+'/..')).to eq(map_path)
62
+
63
+ get maps_prefix+'/source_map/subfolder/other_file.self.map'
64
+ expect(last_response).to be_ok
65
+ end
66
+
67
+ it 'serves map on a subfolder file' do
68
+ js_path = '/assets/source_map/subfolder/other_file.self.js'
69
+ map_path = maps_prefix+'/source_map/subfolder/other_file.self.map'
70
+
71
+ get js_path
72
+
73
+ expect(last_response).to be_ok
74
+ received_map_path = extract_map_path(last_response)
75
+ expect(expand_path(received_map_path, js_path+'/..')).to eq(map_path)
76
+
77
+ get maps_prefix+'/source_map/subfolder/other_file.self.map'
78
+ expect(last_response).to be_ok
79
+ map = ::SourceMap::Map.from_json(last_response.body)
80
+ expect(map.sources).to include(maps_prefix+'/source_map/subfolder/other_file.rb')
81
+ end
82
+ end
83
+
84
+ def extract_map_path(response)
85
+ source_map_comment_regexp = %r{//# sourceMappingURL=(.*)$}
86
+
87
+ case
88
+ when response.body =~ source_map_comment_regexp
89
+ body.scan(source_map_comment_regexp).first.first
90
+ when response.headers['X-SourceMap']
91
+ response.headers['X-SourceMap']
92
+ else
93
+ raise "cannot find source map in response: #{response.inspect}"
94
+ end
95
+ end
96
+
97
+ def expand_path(file_name, dir_string)
98
+ path = File.expand_path(file_name, dir_string)
99
+ # Remove Windows letter and colon (eg. C:) from path
100
+ path = path[2..-1] if !(RUBY_PLATFORM =~ /mswin|mingw/).nil?
101
+ path
102
+ end
103
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ require 'opal/sprockets'
3
+
4
+ describe Opal::Sprockets do
5
+ let(:env) { Sprockets::Environment.new }
6
+ before { Opal.paths.each { |path| env.append_path path } }
7
+
8
+ describe '.load_asset' do
9
+ it 'loads the main asset' do
10
+ code = described_class.load_asset('console', env)
11
+ expect(code).to include('Opal.load("console");')
12
+ end
13
+
14
+ it 'marks as loaded "opal" plus all non opal assets' do
15
+ code = described_class.load_asset('corelib/runtime', env)
16
+ expect(code).to include('Opal.loaded(["opal","corelib/runtime"]);')
17
+ end
18
+
19
+ it 'returns an empty string if the asset is not found' do
20
+ code = described_class.load_asset('foo', env)
21
+ expect(code).to eq('')
22
+ end
23
+ end
24
+
25
+ describe '.javascript_include_tag' do
26
+ it 'works with trailing / in the prefix' do
27
+ code = described_class.javascript_include_tag('corelib/runtime', prefix: '/', sprockets: env, debug: false)
28
+ expect(code).to include('src="/corelib/runtime.')
29
+ expect(code).not_to include('//')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'tilt/opal'
3
+
4
+ describe Opal::TiltTemplate do
5
+ %w[rb js.rb opal js.opal].each do |ext|
6
+ let(:ext) { ext }
7
+
8
+ describe %Q{with extension ".#{ext}"} do
9
+ it "is registered for '.#{ext}' files" do
10
+ expect(Tilt["test.#{ext}"]).to be <= described_class
11
+ end
12
+
13
+ it "compiles and evaluates the template on #render" do
14
+ template = described_class.new('file') { |t| "puts 'Hello, World!'\n" }
15
+ expect(template.render(Object.new)).to include('"Hello, World!"')
16
+ end
17
+ end
18
+ end
19
+
20
+ it "support :build option" do
21
+ template = described_class.new('./spec/fixtures/opal_file.rb', :build=>true)
22
+ output = template.render
23
+ expect(output).to include('"hi from opal!"')
24
+ expect(output).to include('self.$require("corelib/runtime");')
25
+ end
26
+
27
+ it "support :builder option" do
28
+ builder = Opal::Builder.new(:stubs=>['opal'])
29
+ template = described_class.new('./spec/fixtures/opal_file.rb', :builder=>builder)
30
+
31
+ 2.times do
32
+ output = template.render
33
+ expect(output.scan(/hi from opal!/).length).to eql(1)
34
+ expect(output).not_to include('self.$require("corelib/runtime");')
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,69 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-sprockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0.0.10.0.3.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Beynon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-03 00:00:00.000000000 Z
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: opal
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tilt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 0.5.0
47
+ version: '1.4'
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ~>
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: 0.5.0
54
+ version: '1.4'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - '>='
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - '>='
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - '>='
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
67
95
  - !ruby/object:Gem::Version
68
96
  version: '0'
69
97
  description: Sprockets suppoer for opal.
@@ -72,30 +100,43 @@ executables: []
72
100
  extensions: []
73
101
  extra_rdoc_files: []
74
102
  files:
75
- - .gitignore
76
- - .rspec
77
- - .travis.yml
103
+ - ".gitignore"
104
+ - ".rspec"
105
+ - ".travis.yml"
78
106
  - Gemfile
79
107
  - README.md
80
108
  - Rakefile
81
109
  - example/Gemfile
82
110
  - example/app/application.rb
83
- - example/app/foo.rb
111
+ - example/app/user.rb
84
112
  - example/config.ru
85
113
  - example/index.html
86
114
  - lib/opal-sprockets.rb
87
- - lib/opal/sprockets.rb
88
- - lib/opal/sprockets/environment.rb
89
- - lib/opal/sprockets/erb.rb
90
- - lib/opal/sprockets/processor.rb
91
- - lib/opal/sprockets/server.rb
92
115
  - lib/opal/sprockets/version.rb
93
116
  - opal-sprockets.gemspec
94
- - spec/fixtures/app/foo.rb
95
- - spec/fixtures/app2/bar.js
96
- - spec/opal/sprockets/server_spec.rb
97
- - spec/processor_spec.rb
117
+ - spec/fixtures/complex_sprockets.js.rb.erb
118
+ - spec/fixtures/file_with_directives.js
119
+ - spec/fixtures/jst_file.js.jst
120
+ - spec/fixtures/no_requires.rb
121
+ - spec/fixtures/opal_file.rb
122
+ - spec/fixtures/require_tree_test.rb
123
+ - spec/fixtures/required_file.js
124
+ - spec/fixtures/required_tree_test/required_file1.rb
125
+ - spec/fixtures/required_tree_test/required_file2.rb
126
+ - spec/fixtures/requires.rb
127
+ - spec/fixtures/source_map.rb
128
+ - spec/fixtures/source_map/subfolder/other_file.rb
129
+ - spec/fixtures/sprockets_file.js.rb
130
+ - spec/fixtures/sprockets_require_tree_test.rb
131
+ - spec/shared/path_finder_shared.rb
132
+ - spec/shared/path_reader_shared.rb
98
133
  - spec/spec_helper.rb
134
+ - spec/sprockets/erb_spec.rb
135
+ - spec/sprockets/path_reader_spec.rb
136
+ - spec/sprockets/processor_spec.rb
137
+ - spec/sprockets/server_spec.rb
138
+ - spec/sprockets_spec.rb
139
+ - spec/tilt/opal_spec.rb
99
140
  homepage: http://opalrb.org
100
141
  licenses: []
101
142
  metadata: {}
@@ -105,23 +146,41 @@ require_paths:
105
146
  - lib
106
147
  required_ruby_version: !ruby/object:Gem::Requirement
107
148
  requirements:
108
- - - '>='
149
+ - - ">="
109
150
  - !ruby/object:Gem::Version
110
151
  version: '0'
111
152
  required_rubygems_version: !ruby/object:Gem::Requirement
112
153
  requirements:
113
- - - '>='
154
+ - - ">"
114
155
  - !ruby/object:Gem::Version
115
- version: '0'
156
+ version: 1.3.1
116
157
  requirements: []
117
158
  rubyforge_project:
118
- rubygems_version: 2.1.9
159
+ rubygems_version: 2.6.4
119
160
  signing_key:
120
161
  specification_version: 4
121
162
  summary: Sprockets support for opal
122
163
  test_files:
123
- - spec/fixtures/app/foo.rb
124
- - spec/fixtures/app2/bar.js
125
- - spec/opal/sprockets/server_spec.rb
126
- - spec/processor_spec.rb
164
+ - spec/fixtures/complex_sprockets.js.rb.erb
165
+ - spec/fixtures/file_with_directives.js
166
+ - spec/fixtures/jst_file.js.jst
167
+ - spec/fixtures/no_requires.rb
168
+ - spec/fixtures/opal_file.rb
169
+ - spec/fixtures/require_tree_test.rb
170
+ - spec/fixtures/required_file.js
171
+ - spec/fixtures/required_tree_test/required_file1.rb
172
+ - spec/fixtures/required_tree_test/required_file2.rb
173
+ - spec/fixtures/requires.rb
174
+ - spec/fixtures/source_map.rb
175
+ - spec/fixtures/source_map/subfolder/other_file.rb
176
+ - spec/fixtures/sprockets_file.js.rb
177
+ - spec/fixtures/sprockets_require_tree_test.rb
178
+ - spec/shared/path_finder_shared.rb
179
+ - spec/shared/path_reader_shared.rb
127
180
  - spec/spec_helper.rb
181
+ - spec/sprockets/erb_spec.rb
182
+ - spec/sprockets/path_reader_spec.rb
183
+ - spec/sprockets/processor_spec.rb
184
+ - spec/sprockets/server_spec.rb
185
+ - spec/sprockets_spec.rb
186
+ - spec/tilt/opal_spec.rb
data/example/app/foo.rb DELETED
@@ -1,7 +0,0 @@
1
- class Adam
2
- def bar
3
- raise "foo"
4
- end
5
- end
6
-
7
- Adam.new.bar
@@ -1,22 +0,0 @@
1
- require 'sprockets'
2
-
3
- module Opal
4
-
5
- # Environment is a subclass of Sprockets::Environment which already has our opal
6
- # load paths loaded. This makes it easy for stand-alone rack apps, or test runners
7
- # that have opal load paths ready to use. You can also add an existing gem's lib
8
- # directory to our load path to use real gems inside your opal environment.
9
- #
10
- # If you are running rails, then you just need opal-rails instead, which will
11
- # do this for you.
12
- class Environment < ::Sprockets::Environment
13
- def initialize *args
14
- super
15
- Opal.paths.each { |p| append_path p }
16
- end
17
-
18
- def use_gem gem_name
19
- append_path File.join(Gem::Specification.find_by_name(gem_name).gem_dir, 'lib')
20
- end
21
- end
22
- end
@@ -1,29 +0,0 @@
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
@@ -1,124 +0,0 @@
1
- require 'set'
2
- require 'sprockets'
3
-
4
- $OPAL_SOURCE_MAPS = {}
5
-
6
- module Opal
7
-
8
- # Proccess using Sprockets
9
- #
10
- # Opal.process('opal-jquery') # => String
11
- def self.process asset
12
- Environment.new[asset].to_s
13
- end
14
-
15
- # The Processor class is used to make ruby files (with rb or opal extensions)
16
- # available to any sprockets based server. Processor will then get passed any
17
- # ruby source file to build. There are some options you can override globally
18
- # which effect how certain ruby features are handled:
19
- #
20
- # * method_missing_enabled [true by default]
21
- # * optimized_operators_enabled [true by default]
22
- # * arity_check_enabled [false by default]
23
- # * const_missing_enabled [true by default]
24
- # * dynamic_require_severity [true by default]
25
- # * source_map_enabled [true by default]
26
- # * irb_enabled [false by default]
27
- #
28
- class Processor < Tilt::Template
29
- self.default_mime_type = 'application/javascript'
30
-
31
- def self.engine_initialized?
32
- true
33
- end
34
-
35
- class << self
36
- attr_accessor :method_missing_enabled
37
- attr_accessor :arity_check_enabled
38
- attr_accessor :const_missing_enabled
39
- attr_accessor :dynamic_require_severity
40
- attr_accessor :source_map_enabled
41
- attr_accessor :irb_enabled
42
- end
43
-
44
- self.method_missing_enabled = true
45
- self.arity_check_enabled = false
46
- self.const_missing_enabled = true
47
- self.dynamic_require_severity = :error # :error, :warning or :ignore
48
- self.source_map_enabled = true
49
- self.irb_enabled = false
50
-
51
- def self.stub_file(name)
52
- stubbed_files << name.to_s
53
- end
54
-
55
- def self.stubbed_files
56
- @stubbed_files ||= Set.new
57
- end
58
-
59
- def initialize_engine
60
- require_template_library 'opal'
61
- end
62
-
63
- def prepare
64
- end
65
-
66
- def evaluate(context, locals, &block)
67
- options = {
68
- :method_missing => self.class.method_missing_enabled,
69
- :arity_check => self.class.arity_check_enabled,
70
- :const_missing => self.class.const_missing_enabled,
71
- :dynamic_require_severity => self.class.dynamic_require_severity,
72
- :irb => self.class.irb_enabled,
73
- :file => context.logical_path,
74
- }
75
-
76
- compiler = Opal::Compiler.new
77
- result = compiler.compile data, options
78
-
79
- compiler.requires.each do |r|
80
- next if stubbed_file? r
81
- path = find_opal_require context.environment, r
82
- context.require_asset path
83
- end
84
-
85
- if self.class.source_map_enabled
86
- $OPAL_SOURCE_MAPS[context.pathname] = compiler.source_map(source_file_url(context)).to_s
87
- "#{result}\n//@ sourceMappingURL=#{source_map_url(context)}\n"
88
- else
89
- result
90
- end
91
- end
92
-
93
- def source_map_url(context)
94
- "#{prefix}/#{context.logical_path}.js.map"
95
- end
96
-
97
- def source_file_url(context)
98
- "#{prefix}/#{context.logical_path.to_s}"
99
- end
100
-
101
- def prefix
102
- "/__opal_source_maps__"
103
- end
104
-
105
- def stubbed_file?(name)
106
- self.class.stubbed_files.include? name
107
- end
108
-
109
- def find_opal_require(environment, r)
110
- path = environment.paths.find do |p|
111
- File.exist?(File.join(p, "#{r}.rb"))
112
- end
113
-
114
- path ? File.join(path, "#{r}.rb") : r
115
- end
116
- end
117
- end
118
-
119
- Tilt.register 'rb', Opal::Processor
120
- Sprockets.register_engine '.rb', Opal::Processor
121
-
122
- Tilt.register 'opal', Opal::Processor
123
- Sprockets.register_engine '.opal', Opal::Processor
124
-
@@ -1,159 +0,0 @@
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
159
-
@@ -1,4 +0,0 @@
1
- require 'opal/sprockets/environment'
2
- require 'opal/sprockets/processor'
3
- require 'opal/sprockets/erb'
4
- require 'opal/sprockets/server'
File without changes
File without changes
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Opal::Server do
4
- it 'serves source maps only if they are enbled on the Processor' do
5
- original_value = Opal::Processor.source_map_enabled
6
- begin
7
- [true, false].each do |bool|
8
- Opal::Processor.source_map_enabled = bool
9
- expect(subject.source_map_enabled).to eq(bool)
10
- end
11
- ensure
12
- Opal::Processor.source_map_enabled = original_value
13
- end
14
- end
15
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Opal::Processor do
4
- let(:processor) do
5
- Opal::Processor.new(File.expand_path('spec/fixtures/app/foo.rb'))
6
- end
7
-
8
- let(:sprockets) do
9
- Sprockets::Environment.new.tap do |s|
10
- s.append_path 'spec/fixtures/app'
11
- s.append_path 'spec/fixtures/app2'
12
- end
13
- end
14
-
15
- describe "#find_opal_require" do
16
- it "returns the full path of a matching ruby file" do
17
- processor.find_opal_require(sprockets, 'foo').should eq(File.expand_path('spec/fixtures/app/foo.rb'))
18
- end
19
-
20
- it "returns original asset name if no matching ruby file" do
21
- processor.find_opal_require(sprockets, 'doesnt_exist').should eq("doesnt_exist")
22
- end
23
-
24
- it "only matches ruby files (not js files)" do
25
- processor.find_opal_require(sprockets, "bar").should eq("bar")
26
- end
27
- end
28
- end