Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/sass/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative 'embedded_protocol'
require_relative 'exception'
require_relative 'fork_tracker'
require_relative 'gem_package_importer'
require_relative 'logger/silent'
require_relative 'logger/source_location'
require_relative 'logger/source_span'
Expand Down
18 changes: 18 additions & 0 deletions lib/sass/gem_package_importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Sass
# The built-in RubyGems package importer. This loads pkg: URLs from gems.
class GemPackageImporter
# @!visibility private
def find_file_url(url, _canonicalize_context)
return unless url.start_with?('pkg:')

library, _, path = url[4..].partition('/')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we would have to decode the percent-encoding in the URL first, but in here we're lucky:

  • The gem name part will only have "unreserved characters" in a URL and dart will unescape them as part of normalization if they are escaped. If we get a percent encoding in name it must be an invalid gem name, thus we don't have to worry.
  • The subpath part we can just pass the escaped version back as part of the file URL as is.

gem_dir = Gem::Dependency.new(library).to_spec.gem_dir

"file://#{'/' unless gem_dir.start_with?('/')}#{gem_dir}/#{path}"
rescue Gem::MissingSpecError
nil
end
end
end
61 changes: 61 additions & 0 deletions spec/sass/gem_package_importer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Sass::GemPackageImporter do
around do |example|
require 'bundler/inline'

sandbox do |dir|
dir.write({
'_test.scss' => '/* test */',
'_index.scss' => '@use "test";',
'test.gemspec' => <<~GEMSPEC
Gem::Specification.new do |spec|
spec.name = 'test'
spec.summary = 'test'
spec.authors = ['test']
spec.version = '0.0.1'
spec.files = Dir['**/*']
end
GEMSPEC
})

gemfile do
gem 'test', path: dir.path, require: false
end

example.call
end
end

describe 'resolves pkg: url' do
it 'without subpath' do
expect(Sass.compile_string('@use "pkg:test";',
importers: [described_class.new],
logger: Sass::Logger.silent).css)
.to eq('/* test */')
end

it 'with subpath' do
expect(Sass.compile_string('@use "pkg:test/test";',
importers: [described_class.new],
logger: Sass::Logger.silent).css)
.to eq('/* test */')
end

it 'with uppercase scheme' do
expect(Sass.compile_string('@use "PKG:test";',
importers: [described_class.new],
logger: Sass::Logger.silent).css)
.to eq('/* test */')
end

it 'with precent-encoding' do
expect(Sass.compile_string('@use "pkg:%74%65%73%74";',
importers: [described_class.new],
logger: Sass::Logger.silent).css)
.to eq('/* test */')
end
end
end