An algorithm for resolving imports in Sass
npm install @csstools/sass-import-resolve[![NPM Version][npm-img]][npm-url]
[![Linux Build Status][cli-img]][cli-url]
[![Windows Build Status][win-img]][win-url]
[![Gitter Chat][git-img]][git-url]
[Sass Import Resolve] resolves the path and contents of Sass imports, following
the [Sass Import Resolve Specification].
``sh`
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].
`js
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.
`js`
const { file, contents } = resolve('path/to/sass-file', {
readFile: true
});
The resolve method may also resolve the path from a specific directory.
`js`
const { file } = resolve('sass-file', {
cwd: 'path/to'
});
The resolve method may also share its resolved cache.
`js
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 resolveurl(id)
the location of a file within from cwd:
1. if id begins with /cwd
1. is the filesystem rootfile
2. is cwd/idbase
3. is base path of filedir
4. is directory path of filebase
5. if ends with .sass, .scss, or .cssfile
1. test whether existsbase
2. if does not start with _dir/_base
1. test whether existsdir/base.scss
6. otherwise
1. test whether existsdir/base.sass
2. test whether existsdir/base.css
3. test whether existsbase
4. if does not start with _dir/_base.scss
1. test whether existsdir/_base.sass
2. test whether existsdir/_base.css
3. test whether exists1
6. if the length of existing files is 1
1. return the existing file
7. otherwise, if the length of existing files is greater than "It's not clear which file to import"
1. throw base
8. otherwise, if does not end with .css"File to import not found or unreadable"`
1. throw
See the [Sass Import Resolve Specification] for more details.
[Sass Import Resolve]: https://github.com/jonathantneal/sass-import-resolve
[Sass Import Resolve Specification]: https://jonathantneal.github.io/sass-import-resolve
[cli-url]: https://travis-ci.org/jonathantneal/sass-import-resolve
[cli-img]: https://img.shields.io/travis/jonathantneal/sass-import-resolve.svg
[git-url]: https://gitter.im/postcss/postcss
[git-img]: https://img.shields.io/badge/chat-gitter-blue.svg
[npm-url]: https://www.npmjs.com/package/@csstools/sass-import-resolve
[npm-img]: https://img.shields.io/npm/v/@csstools/sass-import-resolve.svg
[win-url]: https://ci.appveyor.com/project/jonathantneal/sass-import-resolve
[win-img]: https://img.shields.io/appveyor/ci/jonathantneal/sass-import-resolve.svg