@@ -6,26 +6,25 @@ import 'dart:cli';
66
77import 'package:sass_api/sass_api.dart' as sass;
88
9- import 'dispatcher.dart' ;
10- import 'embedded_sass.pb.dart' hide SourceSpan;
11- import 'utils.dart' ;
9+ import '../dispatcher.dart' ;
10+ import '../embedded_sass.pb.dart' hide SourceSpan;
11+ import '../utils.dart' ;
12+ import 'base.dart' ;
1213
1314/// An importer that asks the host to resolve imports.
14- class Importer extends sass.Importer {
15- /// The [Dispatcher] to which to send requests.
16- final Dispatcher _dispatcher;
17-
15+ class HostImporter extends ImporterBase {
1816 /// The ID of the compilation in which this importer is used.
1917 final int _compilationId;
2018
2119 /// The host-provided ID of the importer to invoke.
2220 final int _importerId;
2321
24- Importer (this ._dispatcher, this ._compilationId, this ._importerId);
22+ HostImporter (Dispatcher dispatcher, this ._compilationId, this ._importerId)
23+ : super (dispatcher);
2524
2625 Uri ? canonicalize (Uri url) {
2726 return waitFor (() async {
28- var response = await _dispatcher
27+ var response = await dispatcher
2928 .sendCanonicalizeRequest (OutboundMessage_CanonicalizeRequest ()
3029 ..compilationId = _compilationId
3130 ..importerId = _importerId
@@ -34,7 +33,7 @@ class Importer extends sass.Importer {
3433
3534 switch (response.whichResult ()) {
3635 case InboundMessage_CanonicalizeResponse_Result .url:
37- return _parseAbsoluteUrl ("CanonicalizeResponse.url" , response.url);
36+ return parseAbsoluteUrl ("CanonicalizeResponse.url" , response.url);
3837
3938 case InboundMessage_CanonicalizeResponse_Result .error:
4039 throw response.error;
@@ -48,7 +47,7 @@ class Importer extends sass.Importer {
4847 sass.ImporterResult load (Uri url) {
4948 return waitFor (() async {
5049 var response =
51- await _dispatcher .sendImportRequest (OutboundMessage_ImportRequest ()
50+ await dispatcher .sendImportRequest (OutboundMessage_ImportRequest ()
5251 ..compilationId = _compilationId
5352 ..importerId = _importerId
5453 ..url = url.toString ());
@@ -58,41 +57,18 @@ class Importer extends sass.Importer {
5857 return sass.ImporterResult (response.success.contents,
5958 sourceMapUrl: response.success.sourceMapUrl.isEmpty
6059 ? null
61- : _parseAbsoluteUrl ("ImportResponse.success.source_map_url" ,
60+ : parseAbsoluteUrl ("ImportResponse.success.source_map_url" ,
6261 response.success.sourceMapUrl),
6362 syntax: syntaxToSyntax (response.success.syntax));
6463
6564 case InboundMessage_ImportResponse_Result .error:
6665 throw response.error;
6766
6867 case InboundMessage_ImportResponse_Result .notSet:
69- _sendAndThrow (mandatoryError ("ImportResponse.result" ));
68+ sendAndThrow (mandatoryError ("ImportResponse.result" ));
7069 }
7170 }());
7271 }
7372
74- /// Parses [url] as a [Uri] and throws an error if it's invalid or relative
75- /// (including root-relative).
76- ///
77- /// The [field] name is used in the error message if one is thrown.
78- Uri _parseAbsoluteUrl (String field, String url) {
79- Uri parsedUrl;
80- try {
81- parsedUrl = Uri .parse (url);
82- } on FormatException catch (error) {
83- _sendAndThrow (paramsError ("$field is invalid: $error " ));
84- }
85-
86- if (parsedUrl.scheme.isNotEmpty) return parsedUrl;
87- _sendAndThrow (paramsError ('$field must be absolute, was "$parsedUrl "' ));
88- }
89-
90- /// Sends [error] to the remote endpoint, and also throws it so that the Sass
91- /// compilation fails.
92- Never _sendAndThrow (ProtocolError error) {
93- _dispatcher.sendError (error);
94- throw "Protocol error: ${error .message }" ;
95- }
96-
9773 String toString () => "HostImporter" ;
9874}
0 commit comments