Skip to content

Commit

Permalink
Set a FileSystemImporter if custom importer is not given for legacy A…
Browse files Browse the repository at this point in the history
…PI (#128)
  • Loading branch information
ntkme committed Apr 12, 2022
1 parent 464d9d5 commit 9750dc8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/src/compile.ts
Expand Up @@ -2,6 +2,7 @@
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import * as p from 'path';
import {Observable} from 'rxjs';
import * as supportsColor from 'supports-color';

Expand All @@ -17,6 +18,7 @@ import {MessageTransformer} from './message-transformer';
import {PacketTransformer} from './packet-transformer';
import {SyncEmbeddedCompiler} from './sync-compiler';
import {deprotofySourceSpan} from './deprotofy-span';
import {legacyImporterProtocol} from './legacy/importer';

export function compile(
path: string,
Expand Down Expand Up @@ -87,10 +89,20 @@ function newCompileStringRequest(
input.setSource(source);
input.setSyntax(utils.protofySyntax(options?.syntax ?? 'scss'));

if (options?.url) input.setUrl(options.url.toString());
const url = options?.url?.toString();
if (url && url !== legacyImporterProtocol) {
input.setUrl(url);
}

if (options && 'importer' in options && options.importer) {
input.setImporter(importers.register(options.importer));
} else if (url === legacyImporterProtocol) {
const importer = new proto.InboundMessage.CompileRequest.Importer();
importer.setPath(p.resolve('.'));
input.setImporter(importer);
} else {
// When importer is not set on the host, the compiler will set a
// FileSystemImporter if `url` is set to a file: url or a NoOpImporter.
}

const request = newCompileRequest(importers, options);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/legacy/index.ts
Expand Up @@ -172,7 +172,9 @@ function convertStringOptions<sync extends 'sync' | 'async'>(

return {
...modernOptions,
url: options.file ? pathToFileURL(options.file) : undefined,
url: options.file
? pathToFileURL(options.file)
: new URL(legacyImporterProtocol),
importer: modernOptions.importers ? modernOptions.importers[0] : undefined,
syntax: options.indentedSyntax ? 'indented' : 'scss',
};
Expand Down

0 comments on commit 9750dc8

Please sign in to comment.