This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
importer.dart
98 lines (79 loc) · 3.2 KB
/
importer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'dart:cli';
import 'package:sass/sass.dart' as sass;
import 'dispatcher.dart';
import 'embedded_sass.pb.dart' hide SourceSpan;
import 'utils.dart';
/// An importer that asks the host to resolve imports.
class Importer extends sass.Importer {
/// The [Dispatcher] to which to send requests.
final Dispatcher _dispatcher;
/// The ID of the compilation in which this importer is used.
final int _compilationId;
/// The host-provided ID of the importer to invoke.
final int _importerId;
Importer(this._dispatcher, this._compilationId, this._importerId);
Uri? canonicalize(Uri url) {
return waitFor(() async {
var response = await _dispatcher
.sendCanonicalizeRequest(OutboundMessage_CanonicalizeRequest()
..compilationId = _compilationId
..importerId = _importerId
..url = url.toString()
..fromImport = fromImport);
switch (response.whichResult()) {
case InboundMessage_CanonicalizeResponse_Result.url:
return _parseAbsoluteUrl("CanonicalizeResponse.url", response.url);
case InboundMessage_CanonicalizeResponse_Result.error:
throw response.error;
case InboundMessage_CanonicalizeResponse_Result.notSet:
return null;
}
}());
}
sass.ImporterResult load(Uri url) {
return waitFor(() async {
var response =
await _dispatcher.sendImportRequest(OutboundMessage_ImportRequest()
..compilationId = _compilationId
..importerId = _importerId
..url = url.toString());
switch (response.whichResult()) {
case InboundMessage_ImportResponse_Result.success:
return sass.ImporterResult(response.success.contents,
sourceMapUrl: response.success.sourceMapUrl.isEmpty
? null
: _parseAbsoluteUrl("ImportResponse.success.source_map_url",
response.success.sourceMapUrl),
syntax: syntaxToSyntax(response.success.syntax));
case InboundMessage_ImportResponse_Result.error:
throw response.error;
case InboundMessage_ImportResponse_Result.notSet:
_sendAndThrow(mandatoryError("ImportResponse.result"));
}
}());
}
/// Parses [url] as a [Uri] and throws an error if it's invalid or relative
/// (including root-relative).
///
/// The [field] name is used in the error message if one is thrown.
Uri _parseAbsoluteUrl(String field, String url) {
Uri parsedUrl;
try {
parsedUrl = Uri.parse(url);
} on FormatException catch (error) {
_sendAndThrow(paramsError("$field is invalid: $error"));
}
if (parsedUrl.scheme.isNotEmpty) return parsedUrl;
_sendAndThrow(paramsError('$field must be absolute, was "$parsedUrl"'));
}
/// Sends [error] to the remote endpoint, and also throws it so that the Sass
/// compilation fails.
Never _sendAndThrow(ProtocolError error) {
_dispatcher.sendError(error);
throw "Protocol error: ${error.message}";
}
String toString() => "HostImporter";
}