File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1111require_relative 'embedded_protocol'
1212require_relative 'exception'
1313require_relative 'fork_tracker'
14+ require_relative 'gem_package_importer'
1415require_relative 'logger/silent'
1516require_relative 'logger/source_location'
1617require_relative 'logger/source_span'
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments