@csstools/sass-import-resolve
v1.0.0
Published
An algorithm for resolving imports in Sass
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-resolveThe 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
idbegins with/cwdis the filesystem root
fileiscwd/idbaseis base path offilediris directory path offile- if
baseends with.sass,.scss, or.css- test whether
fileexists - if
basedoes not start with_- test whether
dir/_baseexists
- test whether
- test whether
- otherwise
- test whether
dir/base.scssexists - test whether
dir/base.sassexists - test whether
dir/base.cssexists - if
basedoes not start with_- test whether
dir/_base.scssexists - test whether
dir/_base.sassexists - test whether
dir/_base.cssexists
- 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
basedoes not end with.css- throw
"File to import not found or unreadable"
- throw
See the Sass Import Resolve Specification for more details.
