Remove extra importer parameters from internal compile functions

These can be passed as an ImportCache now, so they make the API more
confusing for relatively little benefit.

This also changes the const AsyncImportCache.none field to a non-const
AsyncImportCache.none() constructor. The const field didn't make sense
for a couple reasons: first, it wouldn't use a user's custom logger;
and second, it couldn't cache any relative imports, which were still
possible when the initial file was loaded from disk.
This commit is contained in:
Natalie Weizenbaum 2018-12-11 17:53:46 -08:00
parent 4c2d3af3b0
commit a09a62538e
7 changed files with 65 additions and 79 deletions

View File

@ -9,9 +9,11 @@ import 'dart:async';
import 'package:source_maps/source_maps.dart';
import 'src/async_import_cache.dart';
import 'src/callable.dart';
import 'src/compile.dart' as c;
import 'src/exception.dart';
import 'src/import_cache.dart';
import 'src/importer.dart';
import 'src/logger.dart';
import 'src/sync_package_resolver.dart';
@ -86,11 +88,13 @@ String compile(String path,
Iterable<Callable> functions,
OutputStyle style,
void sourceMap(SingleMapping map)}) {
logger ??= Logger.stderr(color: color);
var result = c.compile(path,
logger: logger ?? Logger.stderr(color: color),
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
logger: logger,
importCache: ImportCache(importers,
logger: logger,
loadPaths: loadPaths,
packageResolver: packageResolver),
functions: functions,
style: style,
sourceMap: sourceMap != null);
@ -167,12 +171,14 @@ String compileString(String source,
url,
void sourceMap(SingleMapping map),
@Deprecated("Use syntax instead.") bool indented = false}) {
logger ??= Logger.stderr(color: color);
var result = c.compileString(source,
syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss),
logger: logger ?? Logger.stderr(color: color),
importers: importers,
packageResolver: packageResolver,
loadPaths: loadPaths,
logger: logger,
importCache: ImportCache(importers,
logger: logger,
packageResolver: packageResolver,
loadPaths: loadPaths),
functions: functions,
style: style,
importer: importer,
@ -196,11 +202,13 @@ Future<String> compileAsync(String path,
Iterable<AsyncCallable> functions,
OutputStyle style,
void sourceMap(SingleMapping map)}) async {
logger ??= Logger.stderr(color: color);
var result = await c.compileAsync(path,
logger: logger ?? Logger.stderr(color: color),
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
logger: logger,
importCache: AsyncImportCache(importers,
logger: logger,
loadPaths: loadPaths,
packageResolver: packageResolver),
functions: functions,
style: style,
sourceMap: sourceMap != null);
@ -226,12 +234,14 @@ Future<String> compileStringAsync(String source,
url,
void sourceMap(SingleMapping map),
@Deprecated("Use syntax instead.") bool indented = false}) async {
logger ??= Logger.stderr(color: color);
var result = await c.compileStringAsync(source,
syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss),
logger: logger ?? Logger.stderr(color: color),
importers: importers,
packageResolver: packageResolver,
loadPaths: loadPaths,
logger: logger,
importCache: AsyncImportCache(importers,
logger: logger,
packageResolver: packageResolver,
loadPaths: loadPaths),
functions: functions,
style: style,
importer: importer,

View File

@ -23,20 +23,12 @@ import 'visitor/serialize.dart';
/// Like [compileAsync] in `lib/sass.dart`, but provides more options to support
/// the node-sass compatible API and the executable.
///
/// No more than one of the following arguments or sets of arguments may be
/// provided:
///
/// * `importCache`;
/// * `nodeImporter`;
/// * `importers`, `packageResolver`, and/or `loadPaths`.
/// At most one of `importCache` and `nodeImporter` may be provided at once.
Future<CompileResult> compileAsync(String path,
{Syntax syntax,
Logger logger,
AsyncImportCache importCache,
NodeImporter nodeImporter,
Iterable<AsyncImporter> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver,
Iterable<AsyncCallable> functions,
OutputStyle style,
bool useSpaces = true,
@ -48,8 +40,7 @@ Future<CompileResult> compileAsync(String path,
Stylesheet stylesheet;
if (nodeImporter == null &&
(syntax == null || syntax == Syntax.forPath(path))) {
importCache ??= AsyncImportCache(importers,
loadPaths: loadPaths, packageResolver: packageResolver, logger: logger);
importCache ??= AsyncImportCache.none(logger: logger);
stylesheet = await importCache.importCanonical(
FilesystemImporter('.'), p.toUri(p.canonicalize(path)), p.toUri(path));
} else {
@ -75,12 +66,7 @@ Future<CompileResult> compileAsync(String path,
/// Like [compileStringAsync] in `lib/sass.dart`, but provides more options to
/// support the node-sass compatible API.
///
/// No more than one of the following arguments or sets of arguments may be
/// provided:
///
/// * `importCache`;
/// * `nodeImporter`;
/// * `importers`, `packageResolver`, and/or `loadPaths`.
/// At most one of `importCache` and `nodeImporter` may be provided at once.
Future<CompileResult> compileStringAsync(String source,
{Syntax syntax,
Logger logger,
@ -100,11 +86,6 @@ Future<CompileResult> compileStringAsync(String source,
var stylesheet =
Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger);
if (nodeImporter == null) {
importCache ??= AsyncImportCache(importers,
loadPaths: loadPaths, packageResolver: packageResolver, logger: logger);
}
return _compileStylesheet(
stylesheet,
logger,

View File

@ -17,9 +17,6 @@ import 'utils.dart'; // ignore: unused_import
/// An in-memory cache of parsed stylesheets that have been imported by Sass.
class AsyncImportCache {
/// A cache that contains no importers.
static const none = AsyncImportCache._none();
/// The importers to use when loading new Sass files.
final List<AsyncImporter> _importers;
@ -63,6 +60,13 @@ class AsyncImportCache {
_canonicalizeCache = {},
_importCache = {};
/// Creates an import cache without any globally-avaiable importers.
AsyncImportCache.none({Logger logger})
: _importers = const [],
_logger = logger ?? const Logger.stderr(),
_canonicalizeCache = {},
_importCache = {};
/// Converts the user's [importers], [loadPaths], and [packageResolver]
/// options into a single list of importers.
static List<AsyncImporter> _toImporters(Iterable<AsyncImporter> importers,

View File

@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_compile.dart.
// See tool/synchronize.dart for details.
//
// Checksum: ece42e2134684c290c8d921521c863857664d323
// Checksum: e6de63e581c0f4da96756b9e2904bd81a090dc5b
//
// ignore_for_file: unused_import
@ -31,20 +31,12 @@ import 'visitor/serialize.dart';
/// Like [compile] in `lib/sass.dart`, but provides more options to support
/// the node-sass compatible API and the executable.
///
/// No more than one of the following arguments or sets of arguments may be
/// provided:
///
/// * `importCache`;
/// * `nodeImporter`;
/// * `importers`, `packageResolver`, and/or `loadPaths`.
/// At most one of `importCache` and `nodeImporter` may be provided at once.
CompileResult compile(String path,
{Syntax syntax,
Logger logger,
ImportCache importCache,
NodeImporter nodeImporter,
Iterable<Importer> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver,
Iterable<Callable> functions,
OutputStyle style,
bool useSpaces = true,
@ -56,8 +48,7 @@ CompileResult compile(String path,
Stylesheet stylesheet;
if (nodeImporter == null &&
(syntax == null || syntax == Syntax.forPath(path))) {
importCache ??= ImportCache(importers,
loadPaths: loadPaths, packageResolver: packageResolver, logger: logger);
importCache ??= ImportCache.none(logger: logger);
stylesheet = importCache.importCanonical(
FilesystemImporter('.'), p.toUri(p.canonicalize(path)), p.toUri(path));
} else {
@ -83,12 +74,7 @@ CompileResult compile(String path,
/// Like [compileString] in `lib/sass.dart`, but provides more options to
/// support the node-sass compatible API.
///
/// No more than one of the following arguments or sets of arguments may be
/// provided:
///
/// * `importCache`;
/// * `nodeImporter`;
/// * `importers`, `packageResolver`, and/or `loadPaths`.
/// At most one of `importCache` and `nodeImporter` may be provided at once.
CompileResult compileString(String source,
{Syntax syntax,
Logger logger,
@ -108,11 +94,6 @@ CompileResult compileString(String source,
var stylesheet =
Stylesheet.parse(source, syntax ?? Syntax.scss, url: url, logger: logger);
if (nodeImporter == null) {
importCache ??= ImportCache(importers,
loadPaths: loadPaths, packageResolver: packageResolver, logger: logger);
}
return _compileStylesheet(
stylesheet,
logger,

View File

@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_import_cache.dart.
// See tool/synchronize.dart for details.
//
// Checksum: 54045771e1267d445a733a83d9fb7e40439d886e
// Checksum: 4989811a7b432e181ccc42004e91f4fe54a786ca
//
// ignore_for_file: unused_import
@ -22,9 +22,6 @@ import 'utils.dart'; // ignore: unused_import
/// An in-memory cache of parsed stylesheets that have been imported by Sass.
class ImportCache {
/// A cache that contains no importers.
static const none = ImportCache._none();
/// The importers to use when loading new Sass files.
final List<Importer> _importers;
@ -68,6 +65,13 @@ class ImportCache {
_canonicalizeCache = {},
_importCache = {};
/// Creates an import cache without any globally-avaiable importers.
ImportCache.none({Logger logger})
: _importers = const [],
_logger = logger ?? const Logger.stderr(),
_canonicalizeCache = {},
_importCache = {};
/// Converts the user's [importers], [loadPaths], and [packageResolver]
/// options into a single list of importers.
static List<Importer> _toImporters(Iterable<Importer> importers,

View File

@ -204,7 +204,9 @@ class _EvaluateVisitor
Map<String, Value> variables,
Logger logger,
bool sourceMap})
: _importCache = importCache ?? AsyncImportCache.none,
: _importCache = nodeImporter == null
? importCache ?? AsyncImportCache.none(logger: logger)
: null,
_importer = importer ?? Importer.noOp,
_nodeImporter = nodeImporter,
_logger = logger ?? const Logger.stderr(),
@ -1815,10 +1817,11 @@ class _EvaluateVisitor
/// Creates a new stack frame with location information from [member] and
/// [span].
Frame _stackFrame(String member, FileSpan span) => frameForSpan(span, member,
url: span.sourceUrl == null
? null
: _importCache.humanize(span.sourceUrl));
Frame _stackFrame(String member, FileSpan span) {
var url = span.sourceUrl;
if (url != null && _importCache != null) url = _importCache.humanize(url);
return frameForSpan(span, member, url: url);
}
/// Returns a stack trace at the current point.
///

View File

@ -5,7 +5,7 @@
// DO NOT EDIT. This file was generated from async_evaluate.dart.
// See tool/synchronize.dart for details.
//
// Checksum: 07e2cda457e3bca4efd3b3f4742744c91c2970d8
// Checksum: 2065f7d701646283e536c5300b0f20f38c4acb46
//
// ignore_for_file: unused_import
@ -211,7 +211,9 @@ class _EvaluateVisitor
Map<String, Value> variables,
Logger logger,
bool sourceMap})
: _importCache = importCache ?? ImportCache.none,
: _importCache = nodeImporter == null
? importCache ?? ImportCache.none(logger: logger)
: null,
_importer = importer ?? Importer.noOp,
_nodeImporter = nodeImporter,
_logger = logger ?? const Logger.stderr(),
@ -1790,10 +1792,11 @@ class _EvaluateVisitor
/// Creates a new stack frame with location information from [member] and
/// [span].
Frame _stackFrame(String member, FileSpan span) => frameForSpan(span, member,
url: span.sourceUrl == null
? null
: _importCache.humanize(span.sourceUrl));
Frame _stackFrame(String member, FileSpan span) {
var url = span.sourceUrl;
if (url != null && _importCache != null) url = _importCache.humanize(url);
return frameForSpan(span, member, url: url);
}
/// Returns a stack trace at the current point.
///