An algorithm for resolving imports in Sass
CC0-1.0 License
Sass Import Resolve resolves the path and contents of Sass imports, following the Sass Import Resolve Specification.
npm install @csstools/sass-import-resolve
The resolve
method takes any path and returns its absolute path, as
resolved by the Sass Import Resolve Specification.
import resolve from '@csstools/sass-import-resolve';
const { file } = resolve('path/to/sass-file');
The resolve
method may also return the contents of the resolved file.
const { file, contents } = resolve('path/to/sass-file', {
readFile: true
});
The resolve
method may also resolve the path from a specific directory.
const { file } = resolve('sass-file', {
cwd: 'path/to'
});
The resolve
method may also share its resolved cache.
const sharedCache = {};
const { file } = resolve('path/to/sass-file', {
cache: sharedCache
});
/* sharedCache {
"/absolute/path/to/sass-file": Promise {
file,
contents (when `readFile` is true)
}
} */
When @import
is called, the following high-level algorithm is used to resolve
the location of a file within url(id)
from cwd
:
id
begins with /
cwd
is the filesystem rootfile
is cwd/id
base
is base path of file
dir
is directory path of file
base
ends with .sass
, .scss
, or .css
file
existsbase
does not start with _
dir/_base
existsdir/base.scss
existsdir/base.sass
existsdir/base.css
existsbase
does not start with _
dir/_base.scss
existsdir/_base.sass
existsdir/_base.css
exists1
1
"It's not clear which file to import"
base
does not end with .css
"File to import not found or unreadable"
See the Sass Import Resolve Specification for more details.