opal-sprockets 0.2.1 → 0.3.0
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/README.md +97 -2
- data/lib/opal/sprockets/erb.rb +2 -3
- data/lib/opal/sprockets/processor.rb +32 -11
- data/lib/opal/sprockets/server.rb +11 -5
- data/lib/opal/sprockets/version.rb +1 -1
- data/opal-sprockets.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8b04180f54f9eacd9d9747b07f672fd7d0a3eec
|
4
|
+
data.tar.gz: d99d41d4c89bfee3986ad9c1a3a31116ae72c5f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2a6c9e34bb10d7e70b68b9e4564821cbad9fec5add260261e5a26405b18b8c4d6e8e80008a3b7861db5b9edc6acbc6290bc443ec03b71a3136169d7b30bd796
|
7
|
+
data.tar.gz: a65a54c75521c62ac96e210dcdc1bed5d68c8e3e45c3bc62a38261364180113f3ca58059972b2ca0a64e18daba9b2a8517eac6ca89e97e77784f219616f9a764
|
data/README.md
CHANGED
@@ -1,6 +1,101 @@
|
|
1
|
-
#
|
1
|
+
# Opal Sprockets
|
2
2
|
|
3
|
-
|
3
|
+
_Adds sprockets support for [Opal](http://opalrb.org)._
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add to your `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "opal-sprockets"
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Sprockets uses a set of load paths to resolve dependencies. This gem extends
|
16
|
+
sprockets to provide opal load paths to sprockets. `opal-sprockets` provides
|
17
|
+
a template processor for all files with `.rb` or `.opal` extensions.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
#= require opal
|
21
|
+
|
22
|
+
puts "opal running in sprockets!"
|
23
|
+
```
|
24
|
+
|
25
|
+
### Improved require support
|
26
|
+
|
27
|
+
By default, sprockets will examine your code for processor directive comments
|
28
|
+
to handle requires, e.g. `#= require opal`. Opal takes this one step futher
|
29
|
+
by extending the opal processor to automatically detect and register any
|
30
|
+
`require` call made inside your ruby code:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require "opal"
|
34
|
+
require "opal-jquery"
|
35
|
+
|
36
|
+
puts "opal-jquery is now available!"
|
37
|
+
```
|
38
|
+
|
39
|
+
Opal cannot require files at runtime, so this trick allows ruby code to use
|
40
|
+
the nicer ruby syntax for requiring dependencies.
|
41
|
+
|
42
|
+
## Example
|
43
|
+
|
44
|
+
Sprockets uses a load path for code files, so make a simple `app/` directory
|
45
|
+
with some code inside `app/application.rb`:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# app/application.rb
|
49
|
+
|
50
|
+
require "opal"
|
51
|
+
|
52
|
+
puts "hello, world"
|
53
|
+
```
|
54
|
+
|
55
|
+
The opal corelib and runtime can be included in your app simply by adding
|
56
|
+
`require "opal"`. We also need an html file to test the application with,
|
57
|
+
so add `index.html`:
|
58
|
+
|
59
|
+
```html
|
60
|
+
<!DOCTYPE html>
|
61
|
+
<html lang="en">
|
62
|
+
<head>
|
63
|
+
<meta charset="utf-8">
|
64
|
+
<script src="/assets/application.js"></script>
|
65
|
+
</head>
|
66
|
+
<body>
|
67
|
+
</body>
|
68
|
+
</html>
|
69
|
+
```
|
70
|
+
|
71
|
+
### Running Application
|
72
|
+
|
73
|
+
`opal-sprockets` comes with a simple `Server` class that can be used to easily
|
74
|
+
configure applications inside `config.ru`:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# config.ru
|
78
|
+
|
79
|
+
require 'bundler'
|
80
|
+
Bundler.require
|
81
|
+
|
82
|
+
run Opal::Server.new { |s|
|
83
|
+
s.append_path 'app'
|
84
|
+
|
85
|
+
s.main = 'application'
|
86
|
+
}
|
87
|
+
```
|
88
|
+
|
89
|
+
This just adds the `app/` directory to the load path, and tells sprockets that
|
90
|
+
`application.rb` will be the main file to load.
|
91
|
+
|
92
|
+
Now just run the rack app:
|
93
|
+
|
94
|
+
```
|
95
|
+
$ bundle exec rackup
|
96
|
+
```
|
97
|
+
|
98
|
+
And then visit `http://127.0.0.1:9292` in any browser.
|
4
99
|
|
5
100
|
## License
|
6
101
|
|
data/lib/opal/sprockets/erb.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'opal'
|
2
|
-
require 'opal/
|
2
|
+
require 'opal/compiler'
|
3
3
|
require 'sprockets'
|
4
4
|
|
5
5
|
module Opal
|
@@ -16,11 +16,10 @@ module Opal
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def prepare
|
19
|
-
# ...
|
20
19
|
end
|
21
20
|
|
22
21
|
def evaluate(scope, locals, &block)
|
23
|
-
Opal::ERB.
|
22
|
+
Opal::ERB.compile data, scope.logical_path.sub(/^templates\//, '')
|
24
23
|
end
|
25
24
|
end
|
26
25
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'set'
|
1
2
|
require 'sprockets'
|
2
3
|
|
3
4
|
$OPAL_SOURCE_MAPS = {}
|
@@ -33,7 +34,6 @@ module Opal
|
|
33
34
|
|
34
35
|
class << self
|
35
36
|
attr_accessor :method_missing_enabled
|
36
|
-
attr_accessor :optimized_operators_enabled
|
37
37
|
attr_accessor :arity_check_enabled
|
38
38
|
attr_accessor :const_missing_enabled
|
39
39
|
attr_accessor :dynamic_require_severity
|
@@ -42,13 +42,20 @@ module Opal
|
|
42
42
|
end
|
43
43
|
|
44
44
|
self.method_missing_enabled = true
|
45
|
-
self.optimized_operators_enabled = true
|
46
45
|
self.arity_check_enabled = false
|
47
46
|
self.const_missing_enabled = true
|
48
47
|
self.dynamic_require_severity = :error # :error, :warning or :ignore
|
49
48
|
self.source_map_enabled = true
|
50
49
|
self.irb_enabled = false
|
51
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
|
+
|
52
59
|
def initialize_engine
|
53
60
|
require_template_library 'opal'
|
54
61
|
end
|
@@ -59,32 +66,46 @@ module Opal
|
|
59
66
|
def evaluate(context, locals, &block)
|
60
67
|
options = {
|
61
68
|
:method_missing => self.class.method_missing_enabled,
|
62
|
-
:optimized_operators => self.class.optimized_operators_enabled,
|
63
69
|
:arity_check => self.class.arity_check_enabled,
|
64
70
|
:const_missing => self.class.const_missing_enabled,
|
65
71
|
:dynamic_require_severity => self.class.dynamic_require_severity,
|
66
|
-
:source_map_enabled => self.class.source_map_enabled,
|
67
72
|
:irb => self.class.irb_enabled,
|
68
73
|
:file => context.logical_path,
|
69
|
-
:source_file => context.pathname.to_s
|
70
74
|
}
|
71
75
|
|
72
|
-
|
73
|
-
result =
|
76
|
+
compiler = Opal::Compiler.new
|
77
|
+
result = compiler.compile data, options
|
74
78
|
|
75
|
-
|
79
|
+
compiler.requires.each do |r|
|
80
|
+
next if stubbed_file? r
|
76
81
|
path = find_opal_require context.environment, r
|
77
82
|
context.require_asset path
|
78
83
|
end
|
79
84
|
|
80
|
-
if
|
81
|
-
$OPAL_SOURCE_MAPS[context.pathname] =
|
82
|
-
"#{result}\n//@ sourceMappingURL
|
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"
|
83
88
|
else
|
84
89
|
result
|
85
90
|
end
|
86
91
|
end
|
87
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
|
+
|
88
109
|
def find_opal_require(environment, r)
|
89
110
|
path = environment.paths.find do |p|
|
90
111
|
File.exist?(File.join(p, "#{r}.rb"))
|
@@ -27,11 +27,17 @@ module Opal
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def call(env)
|
30
|
-
|
31
|
-
asset = sprockets[path]
|
32
|
-
return [404, {}, []] if asset.nil?
|
30
|
+
path_info = env['PATH_INFO']
|
33
31
|
|
34
|
-
|
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
|
35
41
|
end
|
36
42
|
end
|
37
43
|
|
@@ -74,7 +80,7 @@ module Opal
|
|
74
80
|
@app = Rack::Builder.app do
|
75
81
|
use Rack::ShowExceptions
|
76
82
|
map('/assets') { run sprockets }
|
77
|
-
map(server.source_maps.prefix) { run server.source_maps } if
|
83
|
+
map(server.source_maps.prefix) { run server.source_maps } if server.source_map_enabled
|
78
84
|
use Index, server
|
79
85
|
run Rack::Directory.new(server.public_dir)
|
80
86
|
end
|
data/opal-sprockets.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opal-sprockets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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
|
+
date: 2013-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.5.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.5.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|