Skip to content

Commit

Permalink
Breaking change on importer function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Sep 21, 2023
1 parent 4744bd1 commit efbc7a8
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 26 deletions.
20 changes: 20 additions & 0 deletions lib/sass/canonicalize_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Sass
# Contextual information passed to `canonicalize` and `find_file_url`.
# Not all importers will need this information to resolve loads, but some may find it useful.
#
# @see https://sass-lang.com/documentation/js-api/interfaces/canonicalizecontext/
class CanonicalizeContext
# @return [String, nil]
attr_reader :containing_url

# @return [Boolean]
attr_reader :from_import

def initialize(containing_url, from_import)
@containing_url = containing_url
@from_import = from_import
end
end
end
1 change: 1 addition & 0 deletions lib/sass/embedded.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../../ext/sass/cli'
require_relative 'canonicalize_context'
require_relative 'compile_error'
require_relative 'compile_result'
require_relative 'embedded/connection'
Expand Down
20 changes: 16 additions & 4 deletions lib/sass/embedded/host/importer_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(importers, load_paths, alert_color:)
end

def register(importer)
importer = Structifier.to_struct(importer, :canonicalize, :load, :find_file_url)
importer = Structifier.to_struct(importer, :canonicalize, :load, :non_canonical_scheme, :find_file_url)

is_importer = importer.respond_to?(:canonicalize) && importer.respond_to?(:load)
is_file_importer = importer.respond_to?(:find_file_url)
Expand All @@ -39,7 +39,17 @@ def register(importer)
@importers_by_id[id] = importer
if is_importer
EmbeddedProtocol::InboundMessage::CompileRequest::Importer.new(
importer_id: id
importer_id: id,
non_canonical_scheme: if importer.respond_to?(:non_canonical_scheme)
non_canonical_scheme = importer.non_canonical_scheme
if non_canonical_scheme.is_a?(String)
[non_canonical_scheme]
else
non_canonical_scheme || []
end
else
[]
end
)
else
EmbeddedProtocol::InboundMessage::CompileRequest::Importer.new(
Expand All @@ -50,7 +60,8 @@ def register(importer)

def canonicalize(canonicalize_request)
importer = @importers_by_id[canonicalize_request.importer_id]
url = importer.canonicalize(canonicalize_request.url, from_import: canonicalize_request.from_import)&.to_s
url = importer.canonicalize(canonicalize_request.url,
Protofier.from_proto_canonicalize_context(canonicalize_request))&.to_s

EmbeddedProtocol::InboundMessage::CanonicalizeResponse.new(
id: canonicalize_request.id,
Expand Down Expand Up @@ -84,7 +95,8 @@ def import(import_request)

def file_import(file_import_request)
importer = @importers_by_id[file_import_request.importer_id]
file_url = importer.find_file_url(file_import_request.url, from_import: file_import_request.from_import)&.to_s
file_url = importer.find_file_url(file_import_request.url,
Protofier.from_proto_canonicalize_context(file_import_request))&.to_s

EmbeddedProtocol::InboundMessage::FileImportResponse.new(
id: file_import_request.id,
Expand Down
7 changes: 7 additions & 0 deletions lib/sass/embedded/protofier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class Embedded
module Protofier
module_function

def from_proto_canonicalize_context(canonicalize_request)
CanonicalizeContext.new(
canonicalize_request.containing_url == '' ? nil : canonicalize_request.containing_url,
canonicalize_request.from_import
)
end

def from_proto_compile_response(compile_response)
oneof = compile_response.result
result = compile_response.public_send(oneof)
Expand Down
Loading

0 comments on commit efbc7a8

Please sign in to comment.