From 9750dc826ad64c437d3ea7a96a9b994c7f7e0c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Mon, 11 Apr 2022 17:09:22 -0700 Subject: [PATCH] Set a FileSystemImporter if custom importer is not given for legacy API (#128) --- lib/src/compile.ts | 14 +++++++++++++- lib/src/legacy/index.ts | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/src/compile.ts b/lib/src/compile.ts index 32ee0dc7..6a7f480f 100644 --- a/lib/src/compile.ts +++ b/lib/src/compile.ts @@ -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'; @@ -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, @@ -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); diff --git a/lib/src/legacy/index.ts b/lib/src/legacy/index.ts index 78705f0d..6c987985 100644 --- a/lib/src/legacy/index.ts +++ b/lib/src/legacy/index.ts @@ -172,7 +172,9 @@ function convertStringOptions( 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', };