From f54963b5c19e15c7bfe670809496558a208f78f2 Mon Sep 17 00:00:00 2001 From: Tyler Horth Date: Fri, 30 Sep 2016 14:30:35 -0400 Subject: [PATCH] Fix relative urls & source maps fixes #284 --- lib/sprockets/asset.rb | 7 +++++++ lib/sprockets/path_utils.rb | 15 +++++++++++++-- lib/sprockets/source_map_comment_processor.rb | 4 +++- lib/sprockets/source_map_utils.rb | 3 +++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/sprockets/asset.rb b/lib/sprockets/asset.rb index 52d0072dc..ae8f29318 100644 --- a/lib/sprockets/asset.rb +++ b/lib/sprockets/asset.rb @@ -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 + # Public: Returns String MIME type of asset. Returns nil if type is unknown. attr_reader :content_type diff --git a/lib/sprockets/path_utils.rb b/lib/sprockets/path_utils.rb index 467917de8..6e7555b43 100644 --- a/lib/sprockets/path_utils.rb +++ b/lib/sprockets/path_utils.rb @@ -6,6 +6,7 @@ module Sprockets # when code actually wants to reference ::FileUtils. module PathUtils extend self + require 'pathname' # Public: Like `File.stat`. # @@ -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) @@ -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 diff --git a/lib/sprockets/source_map_comment_processor.rb b/lib/sprockets/source_map_comment_processor.rb index 6cc3337f7..c48622f16 100644 --- a/lib/sprockets/source_map_comment_processor.rb +++ b/lib/sprockets/source_map_comment_processor.rb @@ -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 diff --git a/lib/sprockets/source_map_utils.rb b/lib/sprockets/source_map_utils.rb index 71a509f69..381eedf55 100644 --- a/lib/sprockets/source_map_utils.rb +++ b/lib/sprockets/source_map_utils.rb @@ -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)