Dart Sass
Write a custom importer by implementing either the DartSass.Importer
or
DartSass.FilesystemImporter
protocol and passing an instance wrapped in
a DartSass.ImportResolver
to a DartSass.Compiler
.
Topics
protocol Importer
protocol Importer
Methods required to implement a stylesheet importer.
Importers resolve @import
, @use
, and @forward
rules in
stylesheets. The methods are called back by the compiler during compilation. You
don’t need this to include the contents of a filesystem directory: see instead
ImportResolver.loadPath(_:)
.
You could use this to present network resources or dynamically constructed content.
An import has two steps: canonicalization and loading.
Importer.canonicalize(...)
is always called first with whatever URL
text the user has written in their rule. The routine interprets that and returns a canonical
URL that is absolute with a scheme.
The compiler most likely implements a cache based on canonical URLs. If the compiler
does not have a stylesheet cached for the canonical URL then it calls
Importer.load(...)
with that URL to get at the content.
File extension rules
Sass itself handles imports from the filesystem using various filename conventions. Users of your importer mostly likely expect the same behavior if the URLs you are importing resemble filenames with extensions and directories.
From the Sass embedded protocol documentation:
The importer should look for stylesheets by adding the prefix _
to the
URL’s basename, and by adding the extensions .sass
and .scss
if the
URL doesn’t already have one of those extensions. For example, given the URL
“foo/bar/baz” the importer should look for:
foo/bar/baz.sass
foo/bar/baz.scss
foo/bar/_baz.sass
foo/bar/_baz.scss
Given the URL “foo/bar/baz.scss” the importer should look for:
foo/bar/baz.scss
foo/bar/_baz.scss
If the importer finds a stylesheet at more than one of these URLs then it must throw an error indicating that the import is ambiguous.
If none of the possible paths are valid then the importer should perform the
same resolution on the URL followed by /index
. In the example above, it
should additionally look for:
foo/bar/baz/_index.sass
foo/bar/baz/index.sass
foo/bar/baz/_index.scss
foo/bar/baz/index.scss
If more than one of these implicit index resources exist then the importer must throw an error indicating that the import is ambiguous.
Declaration
public protocol Importer: Sendable
struct ImporterResults
struct ImporterResults
The results of loading a stylesheet through an importer.
Declaration
public struct ImporterResults: Sendable
class ImporterContext
class ImporterContext
Context for resolving an import / use / forward rule.
Declaration
public final class ImporterContext: Sendable
protocol FilesystemImporter
protocol FilesystemImporter
Methods required to implement a filesystem-redirecting stylesheet importer.
Use this to map imports to a filesystem location, letting the Sass compiler deal with index directories, file extensions, and actually loading the stylesheet.
Declaration
public protocol FilesystemImporter: Sendable
enum ImportResolver
enum ImportResolver
How the Sass compiler should resolve @import
, @use
, and @forward
rules.
Declaration
public enum ImportResolver: Sendable