I understand that it is best practise to import SASS/SCSS partials without using the leading underscore; e.g.
@import 'normalize-scss/normalize'; // this imports ./normalize-scss/_normalize.scss
My question for nerdy completeness is, are there any consequences if the file is imported using the underscore?
@import 'normalize-scss/_normalize';
The _ (underscore) is a partial for scss. That means the stylesheet its going to be imported (@import) to a main stylesheet i.e. styles. scss. The advantage on using partials is that you can use many files to organize your code and everything will be compiled on a single file.
As of October 2021 @import will be deprecated, along with global built-in functions, and one year after that (no later than October 2022) @import support will be dropped. Make sure you go check out the newly refreshed Sass documentation for the latest updates.
The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.
Naming Conventions. General rules for naming conventions apply to variables, functions, and mixins. When naming anything in Sass it's recommended to use all lowercase letters with dashes for separation. Sass code syntax is actually based on the CSS guidelines ruleset.
No. If your file is _foo.scss, all of these imports have identical results as long as you don't have any ambiguous filenames (barring any side effects that might exist):
@import "foo"; @import "foo.scss"; @import "_foo"; @import "_foo.scss";
The only time an extension is necessary is if you have both _foo.scss and _foo.sass in the same search path. You'll get the following error if you don't specify which one:
error sass/test.scss (Line 7: It's not clear which file to import for '@import "test/bar"'. Candidates: test/_bar.sass test/_bar.scss Please delete or rename all but one of these files. )
If you have both foo.scss and _foo.scss, then foo.scss will take precedence. If you want _foo.scss instead, you'll need to add the underscore to your import statement.
@import "test/_foo";
Sass will nag you with a warning every time you save no matter what your import statement looks like:
WARNING: In /path/to/test: There are multiple files that match the name "bar.scss": _bar.scss bar.scss
If you add an underscore to the start of the file name, Sass won’t compile it. So, if you don’t want colors.scss
to compile to colors.css
, name the file _colors.scss
instead. Files named this way are called partials in Sass terminology.
More about import feature in Sass you can find here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With