Sass Import Resolve 

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
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
});
The Resolve Algorithm
When @import
is called, the following high-level algorithm is used to resolve
the location of a file within url(id)
from cwd
:
- if
id
begins with /
cwd
is the filesystem root
file
is cwd/id
base
is base path of file
dir
is directory path of file
- if
base
ends with .sass
, .scss
, or .css
- test whether
file
exists - if
base
does not start with _
- test whether
dir/_base
exists
- otherwise
- test whether
dir/base.scss
exists - test whether
dir/base.sass
exists - test whether
dir/base.css
exists - if
base
does not start with _
- test whether
dir/_base.scss
exists - test whether
dir/_base.sass
exists - test whether
dir/_base.css
exists
- if the length of existing files is
1
- return the existing file
- otherwise, if the length of existing files is greater than
1
- throw
"It's not clear which file to import"
- otherwise, if
base
does not end with .css
- throw
"File to import not found or unreadable"
See the Sass Import Resolve Specification for more details.