Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3736881

Browse files
committedMay 12, 2014
[snapshot] WIP [skip ci]
1 parent c172789 commit 3736881

File tree

8 files changed

+80
-25
lines changed

8 files changed

+80
-25
lines changed
 

‎lib/opal/builder.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def inspect
109109
end
110110

111111
def source_map
112-
''
112+
@source_map ||= assets.map(&:source_map).compact.map(&:map).reduce(:+)
113113
end
114114

115115
def == other
@@ -126,7 +126,7 @@ def initialize(*)
126126
@requires = []
127127
end
128128

129-
attr_reader :path, :source, :requires
129+
attr_reader :path, :source, :requires, :source_map
130130
end
131131

132132
class RubyAsset < Asset
@@ -141,6 +141,13 @@ def source
141141
compiled.result
142142
end
143143

144+
def source_map
145+
# compiled.source_map
146+
::Opal::SourceMap.new(compiled.fragments, compiled.file).tap do |map|
147+
map.file = path
148+
end
149+
end
150+
144151
def requires
145152
compiled.requires
146153
end

‎lib/opal/nodes/top.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def compile
3737

3838
def opening
3939
if compiler.requirable?
40-
line "Opal.modules[#{compiler.file.inspect}] = function($opal) {"
40+
line "this.Opal.modules[#{compiler.file.inspect}] = function($opal) {"
4141
else
4242
line "(function($opal) {"
4343
end
@@ -47,7 +47,7 @@ def closing
4747
if compiler.requirable?
4848
line "};\n"
4949
else
50-
line "})(Opal);\n"
50+
line "})(this.Opal);\n"
5151
end
5252
end
5353

‎lib/opal/source_map.rb

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# require 'opal'
2-
require 'source_map'
2+
require 'sourcemap'
33

44
module Opal
55
class SourceMap
@@ -12,18 +12,29 @@ def initialize(fragments, file)
1212
end
1313

1414
def map
15-
@map ||= ::SourceMap.new.tap do |map|
15+
# @mappings = SourceMap::Map.new([
16+
# SourceMap::Mapping.new('a.js', SourceMap::Offset.new(0, 0), SourceMap::Offset.new(0, 0)),
17+
# SourceMap::Mapping.new('b.js', SourceMap::Offset.new(1, 0), SourceMap::Offset.new(20, 0)),
18+
# SourceMap::Mapping.new('c.js', SourceMap::Offset.new(2, 0), SourceMap::Offset.new(30, 0))
19+
# ])
20+
21+
@map ||= begin
1622
line, column = 1, 0
1723

24+
mappings = []
1825
@fragments.each do |fragment|
1926
if source_line = fragment.line
20-
map.add_mapping(
21-
:generated_line => line,
22-
:generated_col => column,
23-
:source_line => source_line,
24-
:source_col => fragment.column,
25-
:source => file
27+
mappings << ::SourceMap::Mapping.new(file,
28+
::SourceMap::Offset.new(line, column),
29+
::SourceMap::Offset.new(source_line, fragment.column)
2630
)
31+
# map.add_mapping(
32+
# :generated_line => line,
33+
# :generated_col => column,
34+
# :source_line => source_line,
35+
# :source_col => fragment.column,
36+
# :source => file
37+
# )
2738
end
2839

2940
new_lines = fragment.code.count "\n"
@@ -35,9 +46,19 @@ def map
3546
column += fragment.code.size
3647
end
3748
end
49+
50+
::SourceMap::Map.new(mappings)
3851
end
3952
end
4053

54+
def + other
55+
to_source_map + other.to_source_map
56+
end
57+
58+
def to_source_map
59+
map
60+
end
61+
4162
def as_json
4263
map.as_json
4364
end

‎lib/opal/sprockets/processor.rb

+35-10
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,14 @@ def evaluate(context, locals, &block)
103103
result = builder.build_str(data, path, :prerequired => prerequired)
104104

105105
if self.class.source_map_enabled
106-
$OPAL_SOURCE_MAPS[context.pathname] = result.source_map.to_s
107-
"#{result.to_s}\n//# sourceMappingURL=#{context.logical_path}.map\n"
106+
logical_path = context.logical_path.to_s
107+
$OPAL_SOURCE_MAPS[logical_path] = result.source_map.as_json.to_json
108+
"#{result.to_s}\n//# sourceMappingURL=./#{context.logical_path}.map\n"
108109
else
109110
result.to_s
110111
end
111112
end
112113

113-
def source_file_url(context)
114-
"#{prefix}/#{context.logical_path.to_s}"
115-
end
116-
117-
def prefix
118-
"/__opal_source_maps__"
119-
end
120-
121114
def stubbed_file?(name)
122115
stubbed_files.include? name
123116
end
@@ -134,10 +127,42 @@ def find_opal_require(environment, r)
134127
path ? File.join(path, "#{r}.rb") : r
135128
end
136129
end
130+
131+
module MapsMiddleware
132+
def call(env)
133+
if env['PATH_INFO'].end_with?('.map')
134+
path = unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))
135+
path = path.sub(/\.(map|js)/, '')
136+
137+
if fingerprint = path_fingerprint(path)
138+
path = path.sub("-#{fingerprint}", '')
139+
end
140+
141+
asset = find_asset(path)
142+
logical_path = asset.logical_path.to_s.gsub(/\.(map|js)$/, '')
143+
body = $OPAL_SOURCE_MAPS[logical_path] || %Q{
144+
{
145+
"keys": #{$OPAL_SOURCE_MAPS.keys.inspect},
146+
"path": #{logical_path}
147+
}
148+
}
149+
[ 200, { "Content-Type" => "application/json", "Content-Length" => Rack::Utils.bytesize(body).to_s }, [ body ] ]
150+
else
151+
super
152+
end
153+
end
154+
end
155+
137156
end
138157

139158
Tilt.register 'rb', Opal::Processor
140159
Sprockets.register_engine '.rb', Opal::Processor
141160

142161
Tilt.register 'opal', Opal::Processor
143162
Sprockets.register_engine '.opal', Opal::Processor
163+
164+
165+
require 'sprockets/server'
166+
Sprockets::Server.class_eval do
167+
prepend Opal::MapsMiddleware
168+
end

‎lib/opal/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Opal
2-
VERSION = '0.7.0.dev'
2+
VERSION = '0.7.0.dev3'
33
end

‎opal.gemspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
1818
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
1919
s.require_paths = ['lib']
2020

21-
s.add_dependency 'source_map'
21+
# s.add_dependency 'source_map'
22+
s.add_dependency 'sourcemap'
2223
s.add_dependency 'sprockets'
2324

2425
s.add_development_dependency 'mspec', '1.5.20'

‎opal/corelib/runtime.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(function(undefined) {
22
// The Opal object that is exposed globally
33
var Opal = this.Opal = {};
4+
if (typeof(global) === 'object') global.Opal = Opal;
45

56
// The actual class for BasicObject
67
var RubyBasicObject;

‎spec/cli/compiler_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
describe 'requirable' do
1111
it 'executes the file' do
1212
expect_compiled("").to include('(function($opal) {')
13-
expect_compiled("").to end_with("})(Opal);\n")
13+
expect_compiled("").to end_with("})(this.Opal);\n")
1414
end
1515

1616
it 'puts the compiled into "Opal.modules"' do

0 commit comments

Comments
 (0)
Please sign in to comment.