opal-sprockets 0.4.8.1.0.3.7 → 1.0.2

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.8'
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.2'
11
4
  end
12
5
  end
@@ -1,87 +1,19 @@
1
1
  require 'opal'
2
- require 'opal/sprockets/processor'
3
- require 'opal/sprockets/erb'
4
- require 'opal/sprockets/server'
2
+ require 'opal/sprockets/version'
5
3
 
6
4
  module Opal
7
5
  module Sprockets
8
- # Bootstraps modules loaded by sprockets on `Opal.modules` marking any
9
- # non-Opal asset as already loaded.
10
- #
11
- # @example
12
- #
13
- # Opal::Sprockets.load_asset('application')
14
- #
15
- # @example Will output the following JavaScript:
16
- #
17
- # Opal.loaded("jquery.self", "yet_another_carousel.self");
18
- # Opal.require("opal", "application");
19
- #
20
- # @param name [String] The logical name of the main asset to be loaded (without extension)
21
- #
22
- # @return [String] JavaScript code
23
- def self.load_asset(*names)
24
- if names.last.is_a?(::Sprockets::Environment)
25
- unless @load_asset_warning_displayed
26
- @load_asset_warning_displayed = true
27
- warn "Passing a sprockets environment to Opal::Sprockets.load_asset no more needed.\n #{caller(1, 3).join("\n ")}"
28
- end
29
- names.pop
30
- end
31
-
32
- names = names.map { |name| name.sub(/(\.(js|rb|opal))*\z/, '') }
33
- stubbed = ::Opal::Config.stubbed_files.to_a
34
-
35
- loaded = 'OpalLoaded || []'
36
- loaded = "#{stubbed.to_json}.concat(#{loaded})" if stubbed.any?
37
-
38
- [
39
- "Opal.loaded(#{loaded});",
40
- *names.map { |name| "Opal.require(#{name.to_json});" }
41
- ].join("\n")
42
- end
43
-
44
- # Mark an asset as already loaded.
45
- # This is useful for requiring JavaScript files which are not managed by Opal's laoding system.
46
- #
47
- # @param [String] name The "logical name" of the asset
48
- # @return [String] JavaScript code
49
- def self.loaded_asset(name)
50
- %{if (typeof(OpalLoaded) === 'undefined') OpalLoaded = []; OpalLoaded.push(#{name.to_json});}
51
- end
52
-
53
- # Generate a `<script>` tag for Opal assets.
54
- #
55
- # @param [String] name The logical name of the asset to be loaded (without extension)
56
- # @param [Hash] options The options about sprockets
57
- # @option options [Sprockets::Environment] :sprockets The sprockets instance
58
- # @option options [String] :prefix The prefix String at which is mounted Sprockets, e.g. '/assets'
59
- # @option options [Boolean] :debug Wether to enable debug mode along with sourcemaps support
60
- #
61
- # @return a string of HTML code containing `<script>` tags.
62
- def self.javascript_include_tag(name, options = {})
63
- sprockets = options.fetch(:sprockets)
64
- prefix = options.fetch(:prefix)
65
- debug = options.fetch(:debug)
66
-
67
- # Avoid double slashes
68
- prefix = prefix.chop if prefix.end_with? '/'
69
-
70
- asset = sprockets[name]
71
- raise "Cannot find asset: #{name}" if asset.nil?
72
- scripts = []
73
-
74
- if debug
75
- asset.to_a.map do |dependency|
76
- scripts << %{<script src="#{prefix}/#{dependency.digest_path}?body=1"></script>}
77
- end
78
- else
79
- scripts << %{<script src="#{prefix}/#{name}.js"></script>}
80
- end
81
-
82
- scripts << %{<script>#{load_asset('opal', name)}</script>}
83
-
84
- scripts.join "\n"
85
- end
6
+ require 'opal/sprockets/assets_helper'
7
+ require 'opal/sprockets/mime_types'
8
+ extend AssetsHelper
9
+ extend MimeTypes
10
+
11
+ require 'opal/sprockets/environment'
12
+ require 'opal/sprockets/erb'
13
+ require 'opal/sprockets/processor'
14
+ require 'opal/sprockets/server'
86
15
  end
16
+
17
+ autoload :Processor, 'opal/processor'
18
+ autoload :Environment, 'opal/environment'
87
19
  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", "< 2.0"]
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