1. Introduction
The purpose of this document is to specify an algorithm for resolving the location of external style sheets included by the @import rule in CSS tooling. It is written for developers and implementors looking for formal guidance, and should match existing implementions.
1.1. The @import rule
The @import rule allows users to import style rules from other style sheets. If an @import rule refers to a valid stylesheet, user agents must treat the contents of the stylesheet as if they were written in place of the @import rule, with two exceptions:
- If a feature *explicitly* defines that it only applies to a particular stylesheet, and not any imported ones (such as the @namespace rule), then it doesn’t apply to the imported stylesheet.
- If a feature relies on the relative position of two or more constructs in a stylesheet (such as the requirement that an @else rule follow another conditional group rule, or the requirement that @charset must not have any other content preceding it), it only applies between constructs in the same stylesheet.
Any @import rules must precede all other at-rules and style rules in a style sheet (besides @charset, which must be the first thing in the style sheet if it exists), or else the @import rule is invalid.
2. 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
id
begins with/
-
cwd
is the filesystem root
-
-
resolve as a file using
cwd/id
asfile
-
otherwise, resolve as a directory using
cwd/id
asdir
-
otherwise, if
id
does not begin with/
,./
, or../
-
resolve as a module using
cwd
andid
-
-
otherwise, throw
"CSS Module not found"
2.1. Resolve as a File
-
resolve
file
as the file -
otherwise, resolve
file.css
as the file
2.2. Resolve as a Directory
-
resolve the JSON contents of
dir/package.json
aspkg
-
if
pkg
has anexports.css.import
field-
resolve
dir/pkg.exports.css.import
as the file
-
-
if
pkg
has anexports.css.default
field-
resolve
dir/pkg.exports.css.default
as the file
-
-
if
pkg
has anexports.css
field-
resolve
dir/pkg.exports.css
as the file
-
-
if
pkg
has astyle
field-
resolve
dir/pkg.style
as the file
-
-
otherwise, resolve
dir/index.css
as the file
-
2.3. Resolve as a Module
-
for each
dir
in the node modules directory usingcwd
:-
resolve as a file using
dir/id
asfile
-
otherwise, resolve as a directory using
dir/id
asdir
-
2.4. Node Modules Directory
-
segments
iscwd
split by/
-
count
is the length ofsegments
-
dirs
is an empty list -
while
count
is greater than0
:-
if
segments[count]
is notnode_modules
-
push a new item to
dirs
as the/
-joinedsegments[0 - count]
andnode_modules
-
-
count
iscount
minus1
-
-
return
dirs
3. Implementations
Parcelify is a tool to add css to your npm modules consumed with browserify. It includes the Resolve Algorithm internally.
PostCSS Import is a
PostCSS plugin to transform @import rules by inlining content. It includes
the Resolve Algorithm internally, but diverges from the load_as_directory algorithm by using the style
field and
then the main
field if possible.
rework-npm is a tool to import CSS styles from NPM modules using rework. It includes the Resolve Algorithm internally, but diverges by supporting glob notation.