compileString function

String compileString (String source, { Syntax syntax, bool color: false, Logger logger, Iterable<Importer> importers, SyncPackageResolver packageResolver, Iterable<String> loadPaths, Iterable<Callable> functions, OutputStyle style, Importer importer, dynamic url, void sourceMap(SingleMapping map), @Deprecated("Use syntax instead.") bool indented: false })

Compiles source to CSS and returns the result.

This parses the stylesheet as syntax, which defaults to Syntax.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.

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

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.

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.

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.

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.

Implementation

String compileString(String source,
    {Syntax syntax,
    bool color: false,
    Logger logger,
    Iterable<Importer> importers,
    SyncPackageResolver packageResolver,
    Iterable<String> loadPaths,
    Iterable<Callable> functions,
    OutputStyle style,
    Importer importer,
    url,
    void sourceMap(SingleMapping map),
    @Deprecated("Use syntax instead.") bool indented: false}) {
  var result = c.compileString(source,
      syntax: syntax ?? (indented ? Syntax.sass : Syntax.scss),
      logger: logger ?? new Logger.stderr(color: color),
      importers: importers,
      packageResolver: packageResolver,
      loadPaths: loadPaths,
      functions: functions,
      style: style,
      importer: importer,
      url: url,
      sourceMap: sourceMap != null);
  if (sourceMap != null) sourceMap(result.sourceMap);
  return result.css;
}