Skip to content

Commit

Permalink
Handle CompileRequest.StringInput.importer (#22)
Browse files Browse the repository at this point in the history
Partially addresses sass/embedded_protocol#37
  • Loading branch information
nex3 authored Dec 23, 2020
1 parent fa03766 commit 2f4fb06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
45 changes: 27 additions & 18 deletions bin/dart_sass_embedded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,9 @@ void main(List<String> args) {
? (source_maps.SingleMapping map) => sourceMap = map
: null;

var importers = request.importers.map((importer) {
switch (importer.whichImporter()) {
case InboundMessage_CompileRequest_Importer_Importer.path:
return sass.FilesystemImporter(importer.path);

case InboundMessage_CompileRequest_Importer_Importer.importerId:
return Importer(dispatcher, request.id, importer.importerId);

case InboundMessage_CompileRequest_Importer_Importer.fileImporterId:
throw "CompileRequest.Importer.fileImporterId is not yet supported";

case InboundMessage_CompileRequest_Importer_Importer.notSet:
throw mandatoryError("Importer.importer");
}

// dart-lang/sdk#38790
throw "Unknown Importer.importer $importer.";
});
var importers = request.importers.map((importer) =>
_decodeImporter(dispatcher, request, importer) ??
(throw mandatoryError("Importer.importer")));

var globalFunctions = request.globalFunctions.map((signature) =>
hostCallable(dispatcher, functions, request.id, signature));
Expand All @@ -76,6 +61,7 @@ void main(List<String> args) {
result = sass.compileString(input.source,
logger: logger,
importers: importers,
importer: _decodeImporter(dispatcher, request, input.importer),
functions: globalFunctions,
syntax: syntaxToSyntax(input.syntax),
style: style,
Expand Down Expand Up @@ -119,3 +105,26 @@ void main(List<String> args) {
}
});
}

/// Converts [importer] into an [Importer].
sass.Importer _decodeImporter(
Dispatcher dispatcher,
InboundMessage_CompileRequest request,
InboundMessage_CompileRequest_Importer importer) {
switch (importer.whichImporter()) {
case InboundMessage_CompileRequest_Importer_Importer.path:
return sass.FilesystemImporter(importer.path);

case InboundMessage_CompileRequest_Importer_Importer.importerId:
return Importer(dispatcher, request.id, importer.importerId);

case InboundMessage_CompileRequest_Importer_Importer.fileImporterId:
throw "CompileRequest.Importer.fileImporterId is not yet supported";

case InboundMessage_CompileRequest_Importer_Importer.notSet:
return null;
}

// dart-lang/sdk#38790
throw "Unknown Importer.importer $importer.";
}
16 changes: 16 additions & 0 deletions test/importer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ void main() {
});
});

test("handles an importer for a string compile request", () async {
process.inbound.add(compileString("@import 'other'",
importer: InboundMessage_CompileRequest_Importer()..importerId = 1));
await _canonicalize(process);

var request = getImportRequest(await process.outbound.next);
process.inbound.add(InboundMessage()
..importResponse = (InboundMessage_ImportResponse()
..id = request.id
..success = (InboundMessage_ImportResponse_ImportSuccess()
..contents = "a {b: 1px + 2px}")));

await expectLater(process.outbound, emits(isSuccess("a { b: 3px; }")));
await process.kill();
});

group("load paths", () {
test("are used to load imports", () async {
await d.dir("dir", [d.file("other.scss", "a {b: c}")]).create();
Expand Down
2 changes: 2 additions & 0 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ InboundMessage compileString(String css,
String url,
bool sourceMap,
Iterable<InboundMessage_CompileRequest_Importer> importers,
InboundMessage_CompileRequest_Importer importer,
Iterable<String> functions}) {
var input = InboundMessage_CompileRequest_StringInput()..source = css;
if (syntax != null) input.syntax = syntax;
if (url != null) input.url = url;
if (importer != null) input.importer = importer;

var request = InboundMessage_CompileRequest()..string = input;
if (id != null) request.id = id;
Expand Down

0 comments on commit 2f4fb06

Please sign in to comment.