opal-sprockets 0.4.9.1.0.3.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,115 +2,111 @@ require 'erb'
2
2
  require 'rack'
3
3
  require 'sprockets'
4
4
 
5
- module Opal
6
- module Sprockets
7
- class Server
8
- attr_accessor :debug, :use_index, :index_path, :main, :public_root,
9
- :public_urls, :sprockets, :prefix
10
-
11
- def initialize options = {}
12
- @use_index = true
13
- @public_root = nil
14
- @public_urls = ['/']
15
- @sprockets = options.fetch(:sprockets, ::Sprockets::Environment.new)
16
- @debug = options.fetch(:debug, true)
17
- @prefix = options.fetch(:prefix, '/assets')
18
-
19
- Opal.paths.each { |p| @sprockets.append_path(p) }
20
-
21
- yield self if block_given?
22
- create_app
23
- end
5
+ class Opal::Sprockets::Server
6
+ attr_accessor :debug, :use_index, :index_path, :main, :public_root,
7
+ :public_urls, :sprockets, :prefix
8
+
9
+ def initialize options = {}
10
+ @use_index = true
11
+ @public_root = nil
12
+ @public_urls = ['/']
13
+ @sprockets = options.fetch(:sprockets, ::Sprockets::Environment.new)
14
+ @debug = options.fetch(:debug, true)
15
+ @prefix = options.fetch(:prefix, '/assets')
16
+
17
+ Opal.paths.each { |p| @sprockets.append_path(p) }
18
+
19
+ yield self if block_given?
20
+ create_app
21
+ end
24
22
 
25
- def public_dir=(dir)
26
- @public_root = dir
27
- @public_urls = ["/"]
28
- end
23
+ def public_dir=(dir)
24
+ @public_root = dir
25
+ @public_urls = ["/"]
26
+ end
29
27
 
30
- def source_map=(enabled)
31
- Opal::Config.source_map_enabled = enabled
32
- end
28
+ def source_map=(enabled)
29
+ Opal::Config.source_map_enabled = enabled
30
+ end
33
31
 
34
- def source_map_enabled
35
- Opal::Config.source_map_enabled
36
- end
32
+ def source_map_enabled
33
+ Opal::Config.source_map_enabled
34
+ end
37
35
 
38
- def append_path path
39
- @sprockets.append_path path
40
- end
36
+ def append_path path
37
+ @sprockets.append_path path
38
+ end
41
39
 
42
- def use_gem gem_name
43
- @sprockets.use_gem gem_name
44
- end
40
+ def use_gem gem_name
41
+ @sprockets.use_gem gem_name
42
+ end
45
43
 
46
- def create_app
47
- server, sprockets, prefix = self, @sprockets, self.prefix
48
- sprockets.logger.level ||= Logger::DEBUG
49
-
50
- @app = Rack::Builder.app do
51
- not_found = lambda { |env| [404, {}, []] }
52
- use Rack::Deflater
53
- use Rack::ShowExceptions
54
- use Index, server if server.use_index
55
- map(prefix) { run sprockets }
56
- run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
57
- end
58
- end
44
+ def create_app
45
+ server, sprockets, prefix = self, @sprockets, self.prefix
46
+ sprockets.logger.level ||= Logger::DEBUG
47
+
48
+ @app = Rack::Builder.app do
49
+ not_found = lambda { |env| [404, {}, []] }
50
+ use Rack::Deflater
51
+ use Rack::ShowExceptions
52
+ use Index, server if server.use_index
53
+ map(prefix) { run sprockets }
54
+ run Rack::Static.new(not_found, root: server.public_root, urls: server.public_urls)
55
+ end
56
+ end
57
+
58
+ def call(env)
59
+ @app.call env
60
+ end
61
+
62
+ class Index
63
+
64
+ def initialize(app, server)
65
+ @app = app
66
+ @server = server
67
+ @index_path = server.index_path
68
+ end
59
69
 
60
- def call(env)
70
+ def call(env)
71
+ if %w[/ /index.html].include? env['PATH_INFO']
72
+ [200, { 'Content-Type' => 'text/html' }, [html]]
73
+ else
61
74
  @app.call env
62
75
  end
76
+ end
63
77
 
64
- class Index
65
-
66
- def initialize(app, server)
67
- @app = app
68
- @server = server
69
- @index_path = server.index_path
70
- end
71
-
72
- def call(env)
73
- if %w[/ /index.html].include? env['PATH_INFO']
74
- [200, { 'Content-Type' => 'text/html' }, [html]]
75
- else
76
- @app.call env
77
- end
78
- end
79
-
80
- # Returns the html content for the root path. Supports ERB
81
- def html
82
- if @index_path
83
- raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
84
- Tilt.new(@index_path).render(self)
85
- else
86
- raise "Main asset path not configured (set 'main' within Opal::Server.new block)" if @server.main.nil?
87
- source
88
- end
89
- end
90
-
91
- def javascript_include_tag name
92
- sprockets = @server.sprockets
93
- prefix = @server.prefix
94
- debug = @server.debug
95
-
96
- ::Opal::Sprockets.javascript_include_tag(name, sprockets: sprockets, prefix: prefix, debug: debug)
97
- end
98
-
99
- def source
100
- <<-HTML
101
- <!DOCTYPE html>
102
- <html>
103
- <head>
104
- <meta charset="utf-8">
105
- <title>Opal Server</title>
106
- </head>
107
- <body>
108
- #{javascript_include_tag @server.main}
109
- </body>
110
- </html>
111
- HTML
112
- end
78
+ # Returns the html content for the root path. Supports ERB
79
+ def html
80
+ if @index_path
81
+ raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
82
+ Tilt.new(@index_path).render(self)
83
+ else
84
+ raise "Main asset path not configured (set 'main' within Opal::Server.new block)" if @server.main.nil?
85
+ source
113
86
  end
114
87
  end
88
+
89
+ def javascript_include_tag name
90
+ sprockets = @server.sprockets
91
+ prefix = @server.prefix
92
+ debug = @server.debug
93
+
94
+ ::Opal::Sprockets.javascript_include_tag(name, sprockets: sprockets, prefix: prefix, debug: debug)
95
+ end
96
+
97
+ def source
98
+ <<-HTML
99
+ <!DOCTYPE html>
100
+ <html>
101
+ <head>
102
+ <meta charset="utf-8">
103
+ <title>Opal Server</title>
104
+ </head>
105
+ <body>
106
+ #{javascript_include_tag @server.main}
107
+ </body>
108
+ </html>
109
+ HTML
110
+ end
115
111
  end
116
112
  end
@@ -1,12 +1,5 @@
1
1
  module Opal
2
2
  module Sprockets
3
- BASE_VERSION = '0.4.9'
4
- OPAL_VERSION = '1.0.0'
5
- SPROCKETS_VERSION = '3.7'
6
-
7
- # Just keep the first two segments of dependencies' versions
8
- v = -> v { v.split('.')[0..1].compact.join('.') }
9
-
10
- VERSION = "#{BASE_VERSION}.#{v[OPAL_VERSION]}.#{v[SPROCKETS_VERSION]}"
3
+ VERSION = '1.0.0'
11
4
  end
12
5
  end
@@ -1,25 +1,39 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/opal/sprockets/version', __FILE__)
3
-
4
- Gem::Specification.new do |s|
5
- s.name = 'opal-sprockets'
6
- s.version = Opal::Sprockets::VERSION
7
- s.authors = ['Elia Schito', 'Adam Beynon']
8
- s.email = 'elia@schito.me'
9
- s.homepage = 'https://github.com/opal/opal-sprockets#readme'
10
- s.summary = 'Sprockets support for Opal.'
11
- s.description = 'Sprockets support for Opal.'
12
-
13
- s.files = `git ls-files`.split("\n")
14
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
- s.require_paths = ['lib']
16
-
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'
20
-
21
- s.add_development_dependency 'rake'
22
- s.add_development_dependency 'rspec'
23
- s.add_development_dependency 'rack-test'
24
- s.add_development_dependency 'sourcemap'
1
+ require_relative 'lib/opal/sprockets/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'opal-sprockets'
5
+ spec.version = Opal::Sprockets::VERSION
6
+ spec.authors = ['Elia Schito', 'Adam Beynon']
7
+ spec.email = 'elia@schito.me'
8
+
9
+ spec.summary = 'Sprockets support for Opal.'
10
+ spec.homepage = 'https://github.com/opal/opal-sprockets#readme'
11
+ spec.license = 'MIT'
12
+
13
+ spec.metadata['homepage_uri'] = spec.homepage
14
+ spec.metadata['source_code_uri'] = 'https://github.com/opal/opal-sprockets'
15
+ spec.metadata['changelog_uri'] = 'https://github.com/opal/opal-sprockets/blob/master/CHANGELOG.md'
16
+
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5', '< 3.1')
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ files = Dir.chdir(__dir__) { `git ls-files -z`.split("\x0") }
22
+
23
+ spec.files = files.grep_v(%r{^(test|spec|features)/})
24
+ spec.test_files = files.grep(%r{^(test|spec|features)/})
25
+ spec.bindir = "exe"
26
+ spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'sprockets', "~> 4.0"
30
+ spec.add_dependency 'opal', [">= 1.0", "< 1.2"]
31
+ spec.add_dependency 'tilt', '>= 1.4'
32
+
33
+ spec.add_development_dependency 'rake'
34
+ spec.add_development_dependency 'rspec'
35
+ spec.add_development_dependency 'rack-test'
36
+ spec.add_development_dependency 'sourcemap'
37
+ spec.add_development_dependency 'ejs'
38
+ spec.add_development_dependency 'pry'
25
39
  end
@@ -1,5 +1,5 @@
1
1
  #= require no_requires
2
- //= require jst_file
2
+ #= require ./jst_file
3
3
  #= require_tree ./required_tree_test
4
4
  require 'file_with_directives'
5
5
  require <%= "base64".inspect %>
File without changes
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,9 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
+
4
+ RSpec.configure do |config|
5
+ config.before do
6
+ Opal::Config.reset!
7
+ Opal::Sprockets::Processor.reset_cache_key!
8
+ end
9
+ end
@@ -1,38 +1,28 @@
1
1
  require 'spec_helper'
2
2
  require 'opal/sprockets/erb'
3
3
 
4
- describe Opal::ERB::Processor do
4
+ describe Opal::Sprockets::ERB do
5
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,
6
+ let(:data) { %{<% print("") %><a href="<%= url %>"><%= name %></a>} }
7
+ let(:input) { {
8
+ environment: Opal::Sprockets::Environment.new.tap {|e| e.append_path "/Code/app/mylib/opal"},
9
+ data: data,
16
10
  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
-
11
+ load_path: "/Code/app/mylib/opal",
12
+ metadata: {},
13
+ cache: Sprockets::Cache.new
14
+ } }
24
15
  let(:ext) { 'opalerb' }
25
16
 
26
- it "is registered for '.opalerb' files" do
27
- expect(Tilt["test.#{ext}"]).to eq(described_class)
28
- end
29
-
30
17
  it 'renders the template' do
31
- expect(template.render(sprockets_context)).to include('"<a href=\""')
18
+ result = described_class.call(input)
19
+
20
+ expect(result[:data]).to include('"<a href=\""')
32
21
  end
33
22
 
34
23
  it 'implicitly requires "erb"' do
35
- template.render(sprockets_context)
36
- expect(required_assets).to eq(['erb'])
24
+ result = described_class.call(input)
25
+
26
+ expect(result[:required].first).to include("stdlib/erb.rb")
37
27
  end
38
28
  end
@@ -1,79 +1,75 @@
1
1
  require 'spec_helper'
2
2
  require 'opal/sprockets/processor'
3
3
 
4
- describe Opal::Processor do
4
+ describe Opal::Sprockets::Processor do
5
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,
6
+ let(:data) { "foo" }
7
+ let(:input) { {
8
+ environment: Opal::Sprockets::Environment.new.tap {|e| e.append_path "/Code/app/mylib/opal"},
9
+ data: data,
16
10
  filename: pathname.to_s,
17
- root_path: '/Code/app/mylib',
18
- is_a?: true,
19
- ) }
11
+ load_path: "/Code/app/mylib/opal",
12
+ metadata: {},
13
+ cache: Sprockets::Cache.new
14
+ } }
15
+
16
+ before do
17
+ allow(Sprockets::SourceMapUtils).to receive(:format_source_map)
18
+ allow(Sprockets::SourceMapUtils).to receive(:combine_source_maps)
19
+ end
20
20
 
21
21
  %w[.rb .opal].each do |ext|
22
- let(:ext) { ext }
22
+ describe %{with extension "#{ext}"} do
23
+ let(:ext) { ext }
23
24
 
24
- describe %Q{with extension "#{ext}"} do
25
25
  it "is registered for '#{ext}' files" do
26
- expect(Sprockets.engines[ext]).to eq(described_class)
26
+ mime_type = Sprockets.mime_types.find { |_,v| v[:extensions].include? ".rb" }.first
27
+ transformers = Sprockets.transformers[mime_type]
28
+ transformer = transformers["application/javascript"]
29
+ expect(transformer.processors).to include(described_class)
27
30
  end
28
31
 
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
+ it "compiles the code" do
33
+ result = described_class.call(input.merge data: "puts 'Hello, World!'\n")
34
+
35
+ expect(result[:data]).to include('"Hello, World!"')
32
36
  end
33
- end
34
- end
35
37
 
36
- describe '.stubbed_files' do
37
- around do |e|
38
- config.stubbed_files.clear
39
- e.run
40
- config.stubbed_files.clear
41
- end
38
+ describe '.stubbed_files' do
39
+ it 'requires non-stubbed files' do
40
+ result = described_class.call(input.merge(data: 'require "set"'))
42
41
 
43
- let(:stubbed_file) { 'foo' }
44
- let(:template) { described_class.new { |t| "require #{stubbed_file.inspect}" } }
45
- let(:config) { Opal::Config }
42
+ expect(result[:required].first).to include("stdlib/set.rb")
43
+ end
46
44
 
47
- it 'usually require files' do
48
- expect(sprockets_context).to receive(:require_asset).with(stubbed_file)
49
- template.render(sprockets_context)
50
- end
45
+ it 'skips require of stubbed file' do
46
+ Opal::Config.stubbed_files << 'set'
47
+ result = described_class.call(input.merge(data: "require 'set'"))
51
48
 
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
49
+ expect(result[:required]).not_to include("set.rb")
50
+ end
57
51
 
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} }
52
+ it 'marks a stubbed file as loaded' do
53
+ Opal::Config.stubbed_files << 'set'
54
+ result = described_class.call(input.merge(data: "require 'set'"))
63
55
 
64
- code = ::Opal::Sprockets.load_asset('bar')
65
- expect(code).to match(stubbed_file)
66
- end
67
- end
56
+ expect(result[:data]).not_to include(::Opal::Sprockets.load_asset('set'))
57
+ end
58
+ end
68
59
 
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)
60
+ describe '.cache_key' do
61
+ it 'can be reset' do
62
+ old_cache_key = described_class.cache_key
63
+ Opal::Config.arity_check_enabled = !Opal::Config.arity_check_enabled
64
+ stale_cache_key = described_class.cache_key
65
+
66
+ described_class.reset_cache_key!
67
+ reset_cache_key = described_class.cache_key
68
+
69
+ expect(stale_cache_key).to eq(old_cache_key)
70
+ expect(reset_cache_key).not_to eq(old_cache_key)
71
+ end
72
+ end
77
73
  end
78
74
  end
79
75
  end