Sass @import using leading underscore

Timidfriendly picture Timidfriendly · Jan 16, 2015 · Viewed 14.3k times · Source

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';

Answer

cimmanon picture cimmanon · Jan 16, 2015

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";

Files with the same name but different extension

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.
)

Files with the same name, but one is prefixed with an underscore

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