opal-sprockets 0.4.8.1.0.3.7 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +26 -0
- data/CHANGELOG.md +34 -2
- data/bin/console +14 -0
- data/bin/rake +6 -0
- data/bin/rspec +29 -0
- data/bin/setup +8 -0
- data/lib/opal/environment.rb +4 -0
- data/lib/opal/processor.rb +4 -0
- data/lib/opal/sprockets/assets_helper.rb +78 -0
- data/lib/opal/sprockets/environment.rb +12 -14
- data/lib/opal/sprockets/erb.rb +12 -16
- data/lib/opal/sprockets/mime_types.rb +19 -0
- data/lib/opal/sprockets/processor.rb +112 -115
- data/lib/opal/sprockets/server.rb +94 -98
- data/lib/opal/sprockets/version.rb +1 -8
- data/lib/opal/sprockets.rb +13 -81
- data/opal-sprockets.gemspec +38 -24
- data/spec/fixtures/complex_sprockets.js.rb.erb +1 -1
- data/spec/fixtures/{jst_file.js.jst → jst_file.jst.ejs} +0 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/sprockets/erb_spec.rb +15 -25
- data/spec/sprockets/processor_spec.rb +53 -57
- data/spec/sprockets/server_spec.rb +95 -25
- data/spec/sprockets_spec.rb +2 -2
- data/spec/tilt/opal_spec.rb +1 -1
- metadata +69 -27
- data/.travis.yml +0 -21
- data/lib/opal/sprockets/path_reader.rb +0 -36
- data/spec/shared/path_finder_shared.rb +0 -19
- data/spec/shared/path_reader_shared.rb +0 -31
- data/spec/sprockets/path_reader_spec.rb +0 -41
@@ -2,115 +2,111 @@ require 'erb'
|
|
2
2
|
require 'rack'
|
3
3
|
require 'sprockets'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def public_dir=(dir)
|
24
|
+
@public_root = dir
|
25
|
+
@public_urls = ["/"]
|
26
|
+
end
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
def source_map=(enabled)
|
29
|
+
Opal::Config.source_map_enabled = enabled
|
30
|
+
end
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
def source_map_enabled
|
33
|
+
Opal::Config.source_map_enabled
|
34
|
+
end
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
def append_path path
|
37
|
+
@sprockets.append_path path
|
38
|
+
end
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
def use_gem gem_name
|
41
|
+
@sprockets.use_gem gem_name
|
42
|
+
end
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
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
|
data/lib/opal/sprockets.rb
CHANGED
@@ -1,87 +1,19 @@
|
|
1
1
|
require 'opal'
|
2
|
-
require 'opal/sprockets/
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
data/opal-sprockets.gemspec
CHANGED
@@ -1,25 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
data/spec/sprockets/erb_spec.rb
CHANGED
@@ -1,38 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'opal/sprockets/erb'
|
3
3
|
|
4
|
-
describe Opal::ERB
|
4
|
+
describe Opal::Sprockets::ERB do
|
5
5
|
let(:pathname) { Pathname("/Code/app/mylib/opal/foo.#{ext}") }
|
6
|
-
let(:
|
7
|
-
|
8
|
-
:
|
9
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
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
|
-
|
36
|
-
|
24
|
+
result = described_class.call(input)
|
25
|
+
|
26
|
+
expect(result[:required].first).to include("stdlib/erb.rb")
|
37
27
|
end
|
38
28
|
end
|