Skip to content

Conversation

@ntkme
Copy link
Member

@ntkme ntkme commented Oct 25, 2025

This is the counterpart of NodePackageImporter for loading pkg: URLs from RubyGems.

To gem maintainers: Because gemspec does not support declaring exports like package.json, you will have to put an _index.scss (or similar) at the root of the package if you want the users to be able to @use "pkg:gem_name";.

All the subpaths are looked up from the root of a gem. For example:

# frozen_string_literal: true

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'bootstrap', require: false
  gem 'sass-embedded'
end

puts Sass.compile_string('@use "pkg:bootstrap/assets/stylesheets/bootstrap";',
                         importers: [Sass::GemPackageImporter.new],
                         style: :compressed).css

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.

@ntkme ntkme merged commit 98ea3de into main Oct 25, 2025
94 checks passed
@ntkme ntkme deleted the gem-package-importer branch October 25, 2025 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants