This repository has been archived by the owner on May 15, 2023. It is now read-only.
/
file.dart
65 lines (50 loc) · 2.1 KB
/
file.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
// Copyright 2021 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.
// ignore: deprecated_member_use
import 'dart:cli';
import 'package:sass_api/sass_api.dart' as sass;
import '../dispatcher.dart';
import '../embedded_sass.pb.dart' hide SourceSpan;
import 'base.dart';
/// A filesystem importer to use for most implementation details of
/// [FileImporter].
///
/// This allows us to avoid duplicating logic between the two importers.
final _filesystemImporter = sass.FilesystemImporter('.');
/// An importer that asks the host to resolve imports in a simplified,
/// file-system-centric way.
class FileImporter extends ImporterBase {
/// 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;
FileImporter(Dispatcher dispatcher, this._compilationId, this._importerId)
: super(dispatcher);
Uri? canonicalize(Uri url) {
if (url.scheme == 'file') return _filesystemImporter.canonicalize(url);
// ignore: deprecated_member_use
return waitFor(() async {
var response = await dispatcher
.sendFileImportRequest(OutboundMessage_FileImportRequest()
..compilationId = _compilationId
..importerId = _importerId
..url = url.toString()
..fromImport = fromImport);
switch (response.whichResult()) {
case InboundMessage_FileImportResponse_Result.fileUrl:
var url = parseAbsoluteUrl("The file importer", response.fileUrl);
if (url.scheme != 'file') {
throw 'The file importer must return a file: URL, was "$url"';
}
return _filesystemImporter.canonicalize(url);
case InboundMessage_FileImportResponse_Result.error:
throw response.error;
case InboundMessage_FileImportResponse_Result.notSet:
return null;
}
}());
}
sass.ImporterResult? load(Uri url) => _filesystemImporter.load(url);
String toString() => "FileImporter";
}