Skip to content

Commit

Permalink
Create file urls to relative path helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Sep 22, 2023
1 parent 7740424 commit 6996bab
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/sassc/embedded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def render
source_mapping_url = if source_map_embed?
"data:application/json;base64,#{[@source_map].pack('m0')}"
else
URL.unescape(URL.parse(source_map_file_url).route_from(url).to_s)
URL.file_urls_to_relative_path(source_map_file_url, url)
end
css += "\n/*# sourceMappingURL=#{source_mapping_url} */"
end
Expand All @@ -61,7 +61,7 @@ def render
line += 1 unless line.nil?
url = e.span&.url
path = if url&.start_with?(Protocol::FILE)
URL.unescape(URL.parse(url).route_from(URL.path_to_file_url("#{Dir.pwd}/")).to_s)
URL.file_urls_to_relative_path(url, URL.path_to_file_url("#{Dir.pwd}/"))
end
raise SyntaxError.new(e.full_message, filename: path, line: line)
end
Expand All @@ -79,10 +79,10 @@ def source_map

url = URL.parse(source_map_file_url || file_url)
data = JSON.parse(@source_map)
data['file'] = URL.unescape(URL.parse(output_url).route_from(url).to_s) if output_url
data['file'] = URL.file_urls_to_relative_path(output_url, url) if output_url
data['sources'].map! do |source|
if source.start_with?(Protocol::FILE)
URL.unescape(URL.parse(source).route_from(url).to_s)
URL.file_urls_to_relative_path(source, url)
else
source
end
Expand Down Expand Up @@ -309,7 +309,7 @@ def canonicalize(url, context)
@parent_urls.push(canonical_url)
canonical_url
elsif url.start_with?(Protocol::FILE)
path = URL.unescape(URL.parse(url).route_from(@parent_urls.last).to_s)
path = URL.file_urls_to_relative_path(url, @parent_urls.last)
parent_path = URL.file_url_to_path(@parent_urls.last)

imports = @importer.imports(path, parent_path)
Expand Down Expand Up @@ -354,7 +354,7 @@ def load_paths
end

def resolve_file_url(url, parent_url, from_import)
path = URL.unescape(URL.parse(url).route_from(parent_url).to_s)
path = URL.file_urls_to_relative_path(url, parent_url)
parent_path = URL.file_url_to_path(parent_url)
[File.dirname(parent_path)].concat(load_paths).each do |load_path|
resolved = FileImporter.resolve_path(File.absolute_path(path, load_path), from_import)
Expand Down Expand Up @@ -568,6 +568,10 @@ def unescape(str)
PARSER.unescape(str)
end

def file_urls_to_relative_path(url, from_url)
URL.unescape(URL.parse(url).route_from(from_url).to_s)
end

def file_url_to_path(url)
return if url.nil?

Expand Down

0 comments on commit 6996bab

Please sign in to comment.