compile function

String compile (
  1. String path,
  2. {bool color = false,
  3. Logger logger,
  4. Iterable<Importer> importers,
  5. Iterable<String> loadPaths,
  6. SyncPackageResolver packageResolver,
  7. Iterable<Callable> functions,
  8. OutputStyle style,
  9. void sourceMap(
    1. SingleMapping map
  10. bool charset = true}

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. 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.

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 FilesystemImporters to importers.

  • Each load path specified in the SASS_PATH environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.

  • 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.

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.

The style parameter controls the style of the resulting CSS.

If sourceMap is passed, it's passed a SingleMapping that indicates which sections of the source file(s) correspond to which in the resulting CSS. It's called immediately before this method returns, and only if compilation succeeds. Note that SingleMapping.targetUrl will always be null. Users using the SourceMap API should be sure to add the source_maps package to their pubspec.

If charset is true, this will include a @charset declaration or a UTF-8 byte-order mark if the stylesheet contains any non-ASCII characters. Otherwise, it will never include a @charset declaration or a byte-order mark.

This parameter is meant to be used as an out parameter, so that users who want access to the source map can get it. For example:

SingleMapping sourceMap;
var css = compile(sassPath, sourceMap: (map) => sourceMap = map);

Throws a SassException if conversion fails.


String compile(String path,
    {bool color = false,
    Logger logger,
    Iterable<Importer> importers,
    Iterable<String> loadPaths,
    SyncPackageResolver packageResolver,
    Iterable<Callable> functions,
    OutputStyle style,
    void sourceMap(SingleMapping map),
    bool charset = true}) {
  logger ??= Logger.stderr(color: color);
  var result = c.compile(path,
      logger: logger,
      importCache: ImportCache(importers,
          logger: logger,
          loadPaths: loadPaths,
          packageResolver: packageResolver),
      functions: functions,
      style: style,
      sourceMap: sourceMap != null,
      charset: charset);
  if (sourceMap != null) sourceMap(result.sourceMap);
  return result.css;