Skip to content

Commit

Permalink
Start cleaning up sourcemap server
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 29, 2013
1 parent a1e149d commit 58edaff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 14 additions & 2 deletions lib/opal/sprockets/processor.rb
Expand Up @@ -88,13 +88,25 @@ def evaluate(context, locals, &block)
end

if options[:source_map_enabled]
$OPAL_SOURCE_MAPS[context.pathname] = Opal::SourceMap.new(compiler.fragments, "file://#{context.pathname.to_s}").to_s
"#{result}\n//@ sourceMappingURL=/__opal_source_maps__/#{context.logical_path}.js.map\n"
$OPAL_SOURCE_MAPS[context.pathname] = Opal::SourceMap.new(compiler.fragments, source_file_url(context)).to_s
"#{result}\n//@ sourceMappingURL=#{source_map_url(context)}\n"
else
result
end
end

def source_map_url(context)
"#{prefix}/#{context.logical_path}.js.map"
end

def source_file_url(context)
"#{prefix}/#{context.logical_path.to_s}"
end

def prefix
"/__opal_source_maps__"
end

def stubbed_file?(name)
self.class.stubbed_files.include? name
end
Expand Down
16 changes: 11 additions & 5 deletions lib/opal/sprockets/server.rb
Expand Up @@ -27,11 +27,17 @@ def inspect
end

def call(env)
path = env['PATH_INFO'].gsub(/^\/|\.js\.map$/, '')
asset = sprockets[path]
return [404, {}, []] if asset.nil?
path_info = env['PATH_INFO']

return [200, {"Content-Type" => "text/json"}, [$OPAL_SOURCE_MAPS[asset.pathname].to_s]]
if path_info =~ /\.js\.map$/
path = env['PATH_INFO'].gsub(/^\/|\.js\.map$/, '')
asset = sprockets[path]
return [404, {}, []] if asset.nil?

return [200, {"Content-Type" => "text/json"}, [$OPAL_SOURCE_MAPS[asset.pathname].to_s]]
else
return [200, {"Content-Type" => "text/text"}, [File.read(sprockets.resolve(path_info))]]
end
end
end

Expand Down Expand Up @@ -74,7 +80,7 @@ def create_app
@app = Rack::Builder.app do
use Rack::ShowExceptions
map('/assets') { run sprockets }
map(server.source_maps.prefix) { run server.source_maps } if @source_map_enabled
map(server.source_maps.prefix) { run server.source_maps } if server.source_map_enabled
use Index, server
run Rack::Directory.new(server.public_dir)
end
Expand Down

0 comments on commit 58edaff

Please sign in to comment.