Skip to content

Commit

Permalink
Fix relative urls & source maps
Browse files Browse the repository at this point in the history
fixes #284
  • Loading branch information
Tyler Horth committed Sep 30, 2016
1 parent 55b8c83 commit f54963b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/sprockets/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def digest_path
logical_path.sub(/\.(\w+)$/) { |ext| "-#{etag}#{ext}" }
end

# Public: Return load path + logical path with digest spliced in.
#
# Returns String.
def full_digest_path
File.join(@load_path, digest_path)
end

This comment has been minimized.

Copy link
@jeremy

jeremy Jan 24, 2017

Member

This appears to be unused. Is it related to this change?

# Public: Returns String MIME type of asset. Returns nil if type is unknown.
attr_reader :content_type

Expand Down
15 changes: 13 additions & 2 deletions lib/sprockets/path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Sprockets
# when code actually wants to reference ::FileUtils.
module PathUtils
extend self
require 'pathname'

# Public: Like `File.stat`.
#
Expand Down Expand Up @@ -73,8 +74,6 @@ def entries(path)
#
# Returns true if path is absolute, otherwise false.
if File::ALT_SEPARATOR
require 'pathname'

# On Windows, ALT_SEPARATOR is \
# Delegate to Pathname since the logic gets complex.
def absolute_path?(path)
Expand Down Expand Up @@ -102,6 +101,18 @@ def relative_path?(path)
path =~ /^\.\.?($|#{SEPARATOR_PATTERN})/ ? true : false
end

# Public: Get relative path from `start` to `dest`.
#
# start - String start path (file or dir)
# dest - String destination path
#
# Returns relative String path from `start` to `dest`
def relative_path_from(start, dest)
start, dest = Pathname.new(start), Pathname.new(dest)
start = start.dirname unless start.directory?
dest.relative_path_from(start).to_s
end

# Internal: Get relative path for root path and subpath.
#
# path - String path
Expand Down
4 changes: 3 additions & 1 deletion lib/sprockets/source_map_comment_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def self.call(input)
uri, _ = env.resolve!(input[:filename], accept: map_type)
map = env.load(uri)

path = PathUtils.relative_path_from(input[:filename], map.full_digest_path)

asset.metadata.merge(
data: asset.source + (comment % map.digest_path),
data: asset.source + (comment % path),
links: asset.links + [asset.uri, map.uri]
)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/sprockets/source_map_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def encode_json_source_map(mappings, sources: nil, names: nil, filename: nil)
case mappings
when String
when Array
mappings.each do |m|
m[:source] = relative_path_from(filename, m[:source])
end if filename
sources ||= mappings.map { |m| m[:source] }.uniq.compact
names ||= mappings.map { |m| m[:name] }.uniq.compact
mappings = encode_vlq_mappings(mappings, sources: sources, names: names)
Expand Down

0 comments on commit f54963b

Please sign in to comment.