@csstools/sass-import-resolve
v1.0.0
Published
An algorithm for resolving imports in Sass
Downloads
562,509
Readme
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
});
/* sharedCache {
"/absolute/path/to/sass-file": Promise {
file,
contents (when `readFile` is true)
}
} */
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
iscwd/id
base
is base path offile
dir
is directory path offile
- if
base
ends with.sass
,.scss
, or.css
- test whether
file
exists - if
base
does not start with_
- test whether
dir/_base
exists
- test whether
- test whether
- 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
- test whether
- test whether
- 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"
- throw
- otherwise, if
base
does not end with.css
- throw
"File to import not found or unreadable"
- throw
See the Sass Import Resolve Specification for more details.