The @import
rule is the legacy way of splitting styles across multiple files
in Sass. The @use
rule should generally be used instead, but @import
is
still supported for backwards-compatibility.
ImportRule ::= '@import' (ImportArgumentNoMedia ',')* ImportArgument ImportArgumentNoMedia ::= ImportUrl ImportModifierNoMedia* ImportArgument ::= ImportUrl ImportModifier ImportModifierNoMedia ::= InterpolatedIdentifier* (ImportFunction | ImportSupports) ImportModifier ::= ImportModifierNoMedia* InterpolatedIdentifier* ImportMedia? ImportMedia ::= MediaFeatureInParens (',' MediaQueryList)* | InterpolatedIdentifier (',' MediaQueryList)* ImportSupports ::= 'supports(' SupportsDeclaration ')' ImportFunction ::= InterpolatedIdentifier¹ '(' InterpolatedDeclarationValue? ')' ImportUrl ::= QuotedString | InterpolatedUrl
1: This identifier may not be "supports"
or "and"
. No whitespace is allowed
between it and the following (
.
This somewhat involved grammar was chosen over the simpler
ImportRule ::= '@import" (ImportArgument ',')* ImportArgument ImportArgument ::= ImportUrl ImportModifier* ImportArgument ::= ImportUrl ImportModifier* ImportModifier ::= ImportFunction | ImportSupports | MediaQueryList
because this simpler version produces a number of problematic ambiguities. For example:
@import "..." a b(c)
could be parsed as either:
MediaQuery "a", ImportFunction "b(c)"
MediaQuery "a b", MediaQuery "(c)"
@import "..." a and(b)
could be parsed as either:
MediaQuery "a", ImportFunction "and(b)"
MediaQuery "a and(b)"
To resolve these, this grammar explicitly indicates that a
MediaQueryList
and its associated commas may only appear at the end of anImportRule
, and delineates the exact circumstances in which anInterpolatedIdentifier
is or is not part of aMediaQueryList
.Note that this parses
@import "..." layer (max-width: 600px)
differently than the CSS standard: in CSS,layer
is a CSS layering keyword but Sass parses it as part of a media query in this instance. This doesn't pose a problem in practice because Sass's semantics never depend on how import modifiers are parsed.
To execute an @import
rule rule
:
-
For each of
rule
'sImportArgumentNoMedia
s andImportArgument
sargument
:-
If any of the following are true,
argument
is considered "plain CSS":argument
's URL string begins withhttp://
orhttps://
.argument
's URL string ends with.css
.argument
's URL is anInterpolatedUrl
.argument
has at least oneImportModifierNoMedia
.argument
has a non-emptyImportModifier
.
Note that this means that imports that explicitly end with
.css
are treated as plain CSS@import
rules, rather than importing stylesheets as CSS. -
If
argument
is "plain CSS":-
Evaluate each of the following within
argument
'sImportModifierNoMedia
s orImportModifier
s, and concatenate the results into a single string with" "
between each one:-
For an
InterpolatedIdentifier
outside anImportMedia
, concatenate the result of evaluating it. -
For an
ImportFunction
, concatenate:- The result of evaluating its
InterpolatedIdentifier
"("
- The result of evaluating its
InterpolatedDeclarationValue
(or""
if it doesn't have one) ")"
- The result of evaluating its
-
For an
ImportSupports
, concatenate:"supports("
- The result of evaluating its
SupportsDeclaration
as a CSS string - `")"
-
For an
ImportMedia
, concatenate the result of evaluating it as aMediaQueryList
as a CSS string.ImportMedia
is a subset of the valid syntax ofMediaQueryList
, so this will always work.
-
-
Add an
@import
with the evaluated modifiers to the current module's CSS AST.
-
-
Otherwise, let
file
be the result of loading the file withargument
's URL string. If this returns null, throw an error. -
If
file
's canonical URL is the same as that of any other current source file, throw an error. -
Let
imported
be the result of executingfile
with the empty configuration and the current import context, except that ifrule
is nested within at-rules and/or style rules, that context is preserved when executingfile
.Note that this execution can mutate
import
. -
Let
css
be the result of resolvingimported
's extensions, except that ifrule
is nested within at-rules and/or style rules, that context is added to CSS that comes from modules loaded byimported
.This creates an entirely separate CSS tree with an entirely separate
@extend
context than normal@use
s of these modules. This means their CSS may be duplicated, and they may be extended differently. -
Add
css
to the current module's CSS. -
Add
imported
's extensions to the current module. -
If the
@import
rule is nested within at-rules and/or style rules, add each member inimported
to the local scope. -
Otherwise, add each member in
imported
to the current import context and the current module.Members defined directly in
imported
will have already been added toimport
in the course of its execution. This only adds members thatimported
forwards.Members from
imported
override members of the same name and type that have already been added toimport
andmodule
.
-