2016-10-15 03:24:50 +02:00
|
|
|
// 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.
|
|
|
|
|
2017-11-11 00:47:25 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2018-01-06 02:53:51 +01:00
|
|
|
import 'src/callable.dart';
|
2017-07-08 02:03:31 +02:00
|
|
|
import 'src/compile.dart' as c;
|
2017-02-04 21:20:34 +01:00
|
|
|
import 'src/exception.dart';
|
2017-10-06 03:45:26 +02:00
|
|
|
import 'src/importer.dart';
|
2018-03-12 04:38:29 +01:00
|
|
|
import 'src/logger.dart';
|
2017-02-03 10:08:06 +01:00
|
|
|
import 'src/sync_package_resolver.dart';
|
2018-01-15 01:05:19 +01:00
|
|
|
import 'src/visitor/serialize.dart';
|
2016-10-15 03:24:50 +02:00
|
|
|
|
2018-01-06 02:53:51 +01:00
|
|
|
export 'src/callable.dart' show Callable, AsyncCallable;
|
2017-10-06 03:45:26 +02:00
|
|
|
export 'src/importer.dart';
|
2018-03-12 04:38:29 +01:00
|
|
|
export 'src/logger.dart';
|
2018-01-13 10:30:42 +01:00
|
|
|
export 'src/value.dart' show ListSeparator;
|
|
|
|
export 'src/value/external/value.dart';
|
2018-01-15 01:05:19 +01:00
|
|
|
export 'src/visitor/serialize.dart' show OutputStyle;
|
2017-10-06 03:45:26 +02:00
|
|
|
|
2017-07-08 02:03:31 +02:00
|
|
|
/// Loads the Sass file at [path], compiles it to CSS, and returns the result.
|
2016-10-15 03:24:50 +02:00
|
|
|
///
|
2018-03-12 04:38:29 +01:00
|
|
|
/// If [color] is `true`, this will use terminal colors in warnings. It's
|
|
|
|
/// ignored if [logger] is passed.
|
|
|
|
///
|
|
|
|
/// If [logger] is passed, it's used to emit all messages that are generated by
|
|
|
|
/// Sass code. Users may pass custom subclasses of [Logger].
|
2016-10-20 23:35:45 +02:00
|
|
|
///
|
2017-10-06 03:45:26 +02:00
|
|
|
/// 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].
|
2017-02-03 10:08:06 +01:00
|
|
|
///
|
|
|
|
/// [SyncPackageResolver]: https://www.dartdocs.org/documentation/package_resolver/latest/package_resolver/SyncPackageResolver-class.html
|
|
|
|
///
|
2018-01-06 02:53:51 +01:00
|
|
|
/// Dart functions that can be called from Sass may be passed using [functions].
|
|
|
|
/// Each [Callable] defines a top-level function that will be invoked when the
|
|
|
|
/// given name is called from Sass.
|
|
|
|
///
|
2018-01-15 01:05:19 +01:00
|
|
|
/// The [style] parameter controls the style of the resulting CSS.
|
|
|
|
///
|
2017-07-07 09:57:10 +02:00
|
|
|
/// Throws a [SassException] if conversion fails.
|
2017-07-08 02:03:31 +02:00
|
|
|
String compile(String path,
|
2017-10-06 03:45:26 +02:00
|
|
|
{bool color: false,
|
2018-03-12 04:38:29 +01:00
|
|
|
Logger logger,
|
2017-10-06 03:45:26 +02:00
|
|
|
Iterable<Importer> importers,
|
|
|
|
Iterable<String> loadPaths,
|
2018-01-06 02:53:51 +01:00
|
|
|
SyncPackageResolver packageResolver,
|
2018-01-15 01:05:19 +01:00
|
|
|
Iterable<Callable> functions,
|
|
|
|
OutputStyle style}) {
|
2017-10-06 03:45:26 +02:00
|
|
|
var result = c.compile(path,
|
2018-03-12 04:38:29 +01:00
|
|
|
logger: logger ?? new Logger.stderr(color: color),
|
2017-10-06 03:45:26 +02:00
|
|
|
importers: importers,
|
|
|
|
loadPaths: loadPaths,
|
2018-01-06 02:53:51 +01:00
|
|
|
packageResolver: packageResolver,
|
2018-01-15 01:05:19 +01:00
|
|
|
functions: functions,
|
|
|
|
style: style);
|
2017-10-06 03:45:26 +02:00
|
|
|
return result.css;
|
|
|
|
}
|
2017-07-07 09:57:10 +02:00
|
|
|
|
2017-07-08 02:03:31 +02:00
|
|
|
/// Compiles [source] to CSS and returns the result.
|
2017-07-07 09:57:10 +02:00
|
|
|
///
|
|
|
|
/// If [indented] is `true`, this parses [source] using indented syntax;
|
2018-03-12 04:38:29 +01:00
|
|
|
/// otherwise (and by default) it uses SCSS.
|
|
|
|
///
|
|
|
|
/// If [color] is `true`, this will use terminal colors in warnings. It's
|
|
|
|
/// ignored if [logger] is passed.
|
|
|
|
///
|
|
|
|
/// If [logger] is passed, it's used to emit all messages that are generated by
|
|
|
|
/// Sass code. Users may pass custom subclasses of [Logger].
|
2017-07-07 09:57:10 +02:00
|
|
|
///
|
2017-10-06 03:45:26 +02:00
|
|
|
/// 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].
|
2017-07-07 09:57:10 +02:00
|
|
|
///
|
|
|
|
/// [SyncPackageResolver]: https://www.dartdocs.org/documentation/package_resolver/latest/package_resolver/SyncPackageResolver-class.html
|
|
|
|
///
|
2018-01-06 02:53:51 +01:00
|
|
|
/// Dart functions that can be called from Sass may be passed using [functions].
|
|
|
|
/// Each [Callable] defines a top-level function that will be invoked when the
|
|
|
|
/// given name is called from Sass.
|
|
|
|
///
|
2018-01-15 01:05:19 +01:00
|
|
|
/// The [style] parameter controls the style of the resulting CSS.
|
|
|
|
///
|
2017-10-06 03:45:26 +02:00
|
|
|
/// 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`.
|
2017-07-07 09:57:10 +02:00
|
|
|
///
|
|
|
|
/// Throws a [SassException] if conversion fails.
|
2017-07-08 02:03:31 +02:00
|
|
|
String compileString(String source,
|
2017-07-09 23:52:14 +02:00
|
|
|
{bool indented: false,
|
|
|
|
bool color: false,
|
2018-03-12 04:38:29 +01:00
|
|
|
Logger logger,
|
2017-10-06 03:45:26 +02:00
|
|
|
Iterable<Importer> importers,
|
2017-07-09 23:52:14 +02:00
|
|
|
SyncPackageResolver packageResolver,
|
2018-01-06 02:53:51 +01:00
|
|
|
Iterable<String> loadPaths,
|
|
|
|
Iterable<Callable> functions,
|
2018-01-15 01:05:19 +01:00
|
|
|
OutputStyle style,
|
2017-10-06 03:45:26 +02:00
|
|
|
Importer importer,
|
2017-07-09 23:52:14 +02:00
|
|
|
url}) {
|
|
|
|
var result = c.compileString(source,
|
|
|
|
indented: indented,
|
2018-03-12 04:38:29 +01:00
|
|
|
logger: logger ?? new Logger.stderr(color: color),
|
2017-10-06 03:45:26 +02:00
|
|
|
importers: importers,
|
2017-07-09 23:52:14 +02:00
|
|
|
packageResolver: packageResolver,
|
2018-01-06 02:53:51 +01:00
|
|
|
loadPaths: loadPaths,
|
|
|
|
functions: functions,
|
2018-01-15 01:05:19 +01:00
|
|
|
style: style,
|
2017-10-06 03:45:26 +02:00
|
|
|
importer: importer,
|
2017-07-09 23:52:14 +02:00
|
|
|
url: url);
|
|
|
|
return result.css;
|
|
|
|
}
|
2017-07-08 02:03:31 +02:00
|
|
|
|
2017-11-11 00:47:25 +01:00
|
|
|
/// 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,
|
2018-03-12 04:38:29 +01:00
|
|
|
Logger logger,
|
2017-11-11 00:47:25 +01:00
|
|
|
Iterable<AsyncImporter> importers,
|
2018-01-06 02:53:51 +01:00
|
|
|
SyncPackageResolver packageResolver,
|
2017-11-11 00:47:25 +01:00
|
|
|
Iterable<String> loadPaths,
|
2018-01-15 01:05:19 +01:00
|
|
|
Iterable<AsyncCallable> functions,
|
|
|
|
OutputStyle style}) async {
|
2017-11-11 00:47:25 +01:00
|
|
|
var result = await c.compileAsync(path,
|
2018-03-12 04:38:29 +01:00
|
|
|
logger: logger ?? new Logger.stderr(color: color),
|
2017-11-11 00:47:25 +01:00
|
|
|
importers: importers,
|
|
|
|
loadPaths: loadPaths,
|
2018-01-06 02:53:51 +01:00
|
|
|
packageResolver: packageResolver,
|
2018-01-15 01:05:19 +01:00
|
|
|
functions: functions,
|
|
|
|
style: style);
|
2017-11-11 00:47:25 +01:00
|
|
|
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,
|
2018-03-12 04:38:29 +01:00
|
|
|
Logger logger,
|
2017-11-11 00:47:25 +01:00
|
|
|
Iterable<AsyncImporter> importers,
|
|
|
|
SyncPackageResolver packageResolver,
|
2018-01-06 02:53:51 +01:00
|
|
|
Iterable<String> loadPaths,
|
|
|
|
Iterable<AsyncCallable> functions,
|
2018-01-15 01:05:19 +01:00
|
|
|
OutputStyle style,
|
2017-11-11 00:47:25 +01:00
|
|
|
AsyncImporter importer,
|
|
|
|
url}) async {
|
|
|
|
var result = await c.compileStringAsync(source,
|
|
|
|
indented: indented,
|
2018-03-12 04:38:29 +01:00
|
|
|
logger: logger ?? new Logger.stderr(color: color),
|
2017-11-11 00:47:25 +01:00
|
|
|
importers: importers,
|
|
|
|
packageResolver: packageResolver,
|
2018-01-06 02:53:51 +01:00
|
|
|
loadPaths: loadPaths,
|
|
|
|
functions: functions,
|
2018-01-15 01:05:19 +01:00
|
|
|
style: style,
|
2017-11-11 00:47:25 +01:00
|
|
|
importer: importer,
|
|
|
|
url: url);
|
|
|
|
return result.css;
|
|
|
|
}
|
|
|
|
|
2017-07-08 02:03:31 +02:00
|
|
|
/// Use [compile] instead.
|
|
|
|
@Deprecated('Will be removed in 1.0.0')
|
|
|
|
String render(String path,
|
|
|
|
{bool color: false, SyncPackageResolver packageResolver}) =>
|
2017-07-09 23:52:14 +02:00
|
|
|
compile(path, color: color, packageResolver: packageResolver);
|