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
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
-
-
otherwise, throw
"CSS Module not found"
2.1. Resolve as a File
-
resolve
fileas the file -
otherwise, resolve
file.cssas the file
2.2. 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
-
-
if
pkghas anexports.css.defaultfield-
resolve
dir/pkg.exports.css.defaultas the file
-
-
if
pkghas anexports.cssfield-
resolve
dir/pkg.exports.cssas the file
-
-
if
pkghas astylefield-
resolve
dir/pkg.styleas the file
-
-
otherwise, resolve
dir/index.cssas the file
-
2.3. 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
-
2.4. Node Modules Directory
-
segmentsiscwdsplit by/ -
countis the length ofsegments -
dirsis 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
-
-
countiscountminus1
-
-
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.
