css-import-resolve v1.0.2
CSS Import Resolve
CSS Import Resolve is an algorithm for resolving imports in CSS.
See the CSS Import Resolve specification for more details.
npm install css-import-resolveimport { resolveAsync } from 'css-import-resolve'
// where `id` is location stylesheet you are resolving, and
const id = 'path/to/css'
// where `cwd` is the current working directory
const cwd = process.cwd()
resolveAsync(id, cwd).then(file => { /* file is an absolute path */ })Resolve Algorithm
When @import is called, the following high-level algorithm is used to resolve
the location of the CSS file, found within url(id) from cwd:
- if
idbegins with/cwdis the filesystem root
- resolve as a file using
cwd/idasfile - otherwise, resolve as a directory using
cwd/idasdir - otherwise, if
iddoes not begin with/,./, or../- resolve as a module using
cwdandid
- resolve as a module using
- otherwise, throw
"CSS Module not found"
Resolve as a File
- resolve
fileas the file - otherwise, resolve
file.cssas the file
Resolve as a Directory
- resolve the JSON contents of
dir/package.jsonaspkg- if
pkghas anexports.css.importfield- resolve
dir/pkg.exports.css.importas the file
- resolve
- if
pkghas anexports.css.defaultfield- resolve
dir/pkg.exports.css.defaultas the file
- resolve
- if
pkghas anexports.cssfield- resolve
dir/pkg.exports.cssas the file
- resolve
- if
pkghas astylefield- resolve
dir/pkg.styleas the file
- resolve
- otherwise, resolve
dir/index.cssas the file
- if
Resolve as a Module
- for each
dirin the node modules directory usingcwd- resolve as a file using
dir/idasfile - otherwise, resolve as a directory using
dir/idasdir
- resolve as a file using
Node Modules Directory
segmentsiscwdsplit by/countis the length ofsegmentsdirsis an empty list- while
countis greater than0- if
segments[count]is notnode_modules- push a new item to
dirsas the/-joinedsegments[0 - count]andnode_modules
- push a new item to
countiscountminus1
- if
- return
dirs
resolveSync
import { resolveSync } from 'css-import-resolve'
const fileOrNull = resolveSync('path/to/css', process.cwd())Options
import { resolveSync } from 'css-import-resolve'
const id = 'path/to/css'
const cwd = process.cwd()
// resolve files as css modules
const opts = {
extensions: ['module.css'],
indexes: ['index.module.css'],
packageProps: ['exports.icss.import', 'exports.icss.default', 'exports.icss']
}
resolveSync(id, cwd, opts)baseUrl
The baseUrl parameter sets the base directory used by non-relative paths during resolution.
extensions
The extensions parameter sets the list of file extensions checked during resolution. Its default value is ["css"].
indexes
The index parameter sets the list of index files in a directory that are checked during resolution. Its default value is ["index.css"].
packageProps
The packageProps parameter sets the list of fields in a package.json that are checked during resolution. Its default value is ["exports.css.import", "exports.css.default", "exports.css", "style"].
fileResolverAsyncCache
The fileResolverAsyncCache parameter sets the object used to cache asynchronous results during resolution, allowing duplicate checks to resolve without revisiting a file system.
fileResolverSyncCache
The fileResolverSyncCache parameter sets the object used to cache asynchronous results during resolution, allowing duplicate checks to resolve without revisiting a file system.