Skip to content

Commit 15e1dba

Browse files
committed
Add Sass::GemPackageImporter
1 parent e781394 commit 15e1dba

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

lib/sass/compiler.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative 'embedded_protocol'
1212
require_relative 'exception'
1313
require_relative 'fork_tracker'
14+
require_relative 'gem_package_importer'
1415
require_relative 'logger/silent'
1516
require_relative 'logger/source_location'
1617
require_relative 'logger/source_span'

lib/sass/gem_package_importer.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module Sass
4+
# The built-in RubyGems package importer. This loads pkg: URLs from gems.
5+
class GemPackageImporter
6+
def initialize
7+
require 'rubygems'
8+
end
9+
10+
def find_file_url(url, _canonicalize_context)
11+
return unless url.start_with?('pkg:')
12+
13+
library, _, path = url.delete_prefix('pkg:').partition('/')
14+
return nil if library.empty? || path.empty?
15+
16+
gem_dir = Gem::Dependency.new(library).to_spec.gem_dir
17+
gem_dir = "/#{gem_dir}" unless gem_dir.start_with?('/')
18+
19+
"file://#{gem_dir}/#{path}"
20+
rescue Gem::MissingSpecError
21+
nil
22+
end
23+
end
24+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Sass::GemPackageImporter do
6+
around do |example|
7+
require 'bundler/inline'
8+
9+
sandbox do |dir|
10+
dir.write({
11+
'_test.scss' => '/* test */',
12+
'test.gemspec' => <<~GEMSPEC
13+
Gem::Specification.new do |spec|
14+
spec.name = 'test'
15+
spec.summary = 'test'
16+
spec.authors = ['test']
17+
spec.version = '0.0.1'
18+
spec.files = Dir['**/*']
19+
end
20+
GEMSPEC
21+
})
22+
23+
gemfile do
24+
gem 'test', path: dir.path, require: false
25+
end
26+
27+
example.call
28+
end
29+
end
30+
31+
it 'resolves pkg: url' do
32+
expect(Sass.compile_string('@use "pkg:test/test";',
33+
importers: [described_class.new],
34+
logger: Sass::Logger.silent).css)
35+
.to eq('/* test */')
36+
end
37+
38+
it 'resolves pkg: url with precent-encoding' do
39+
expect(Sass.compile_string('@use "pkg:%74%65%73%74/%74%65%73%74";',
40+
importers: [described_class.new],
41+
logger: Sass::Logger.silent).css)
42+
.to eq('/* test */')
43+
end
44+
end

0 commit comments

Comments
 (0)