dart-sass/lib/sass.dart
Natalie Weizenbaum 74400dc4a4 Add asynchronous versions of several evaluation-related libraries
This allows us to support asynchronous importers and, eventually,
functions without breaking synchronous support. The copies were made
manually, but the eventual plan is to auto-generate the synchronous
versions by stripping all asynchrony from the async versions.

See #9
2017-12-01 14:29:11 -08:00

139 lines
4.7 KiB
Dart

// Copyright 2016 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.
import 'dart:async';
import 'src/compile.dart' as c;
import 'src/exception.dart';
import 'src/importer.dart';
import 'src/sync_package_resolver.dart';
export 'src/importer.dart';
/// Loads the Sass file at [path], compiles it to CSS, and returns the result.
///
/// If [color] is `true`, this will use terminal colors in warnings.
///
/// Imports are resolved by trying, in order:
///
/// * Loading a file relative to [path].
///
/// * Each importer in [importers].
///
/// * Each load path in [loadPaths]. Note that this is a shorthand for adding
/// [FilesystemImporter]s to [importers].
///
/// * `package:` resolution using [packageResolver], which is a
/// [SyncPackageResolver][] from the `package_resolver` package. Note that
/// this is a shorthand for adding a [PackageImporter] to [importers].
///
/// [SyncPackageResolver]: https://www.dartdocs.org/documentation/package_resolver/latest/package_resolver/SyncPackageResolver-class.html
///
/// Throws a [SassException] if conversion fails.
String compile(String path,
{bool color: false,
Iterable<Importer> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver}) {
var result = c.compile(path,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver);
return result.css;
}
/// Compiles [source] to CSS and returns the result.
///
/// If [indented] is `true`, this parses [source] using indented syntax;
/// otherwise (and by default) it uses SCSS. If [color] is `true`, this will use
/// terminal colors in warnings.
///
/// Imports are resolved by trying, in order:
///
/// * The given [importer], with the imported URL resolved relative to [url].
///
/// * Each importer in [importers].
///
/// * Each load path in [loadPaths]. Note that this is a shorthand for adding
/// [FilesystemImporter]s to [importers].
///
/// * `package:` resolution using [packageResolver], which is a
/// [SyncPackageResolver][] from the `package_resolver` package. Note that
/// this is a shorthand for adding a [PackageImporter] to [importers].
///
/// [SyncPackageResolver]: https://www.dartdocs.org/documentation/package_resolver/latest/package_resolver/SyncPackageResolver-class.html
///
/// The [url] indicates the location from which [source] was loaded. It may be a
/// [String] or a [Uri]. If [importer] is passed, [url] must be passed as well
/// and `importer.load(url)` should return `source`.
///
/// Throws a [SassException] if conversion fails.
String compileString(String source,
{bool indented: false,
bool color: false,
Iterable<Importer> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver,
Importer importer,
url}) {
var result = c.compileString(source,
indented: indented,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
importer: importer,
url: url);
return result.css;
}
/// Like [compile], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compile] should be preferred if possible.
Future<String> compileAsync(String path,
{bool color: false,
Iterable<AsyncImporter> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver}) async {
var result = await c.compileAsync(path,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver);
return result.css;
}
/// Like [compileString], except it runs asynchronously.
///
/// Running asynchronously allows this to take [AsyncImporter]s rather than
/// synchronous [Importer]s. However, running asynchronously is also somewhat
/// slower, so [compileString] should be preferred if possible.
Future<String> compileStringAsync(String source,
{bool indented: false,
bool color: false,
Iterable<AsyncImporter> importers,
Iterable<String> loadPaths,
SyncPackageResolver packageResolver,
AsyncImporter importer,
url}) async {
var result = await c.compileStringAsync(source,
indented: indented,
color: color,
importers: importers,
loadPaths: loadPaths,
packageResolver: packageResolver,
importer: importer,
url: url);
return result.css;
}
/// Use [compile] instead.
@Deprecated('Will be removed in 1.0.0')
String render(String path,
{bool color: false, SyncPackageResolver packageResolver}) =>
compile(path, color: color, packageResolver: packageResolver);