Is it possible to import a whole directory in sass using @import?
Solution 1:
If you are using Sass in a Rails project, the sass-rails gem, https://github.com/rails/sass-rails, features glob importing.
@import "foo/*" // import all the files in the foo folder
@import "bar/**/*" // import all the files in the bar tree
To answer the concern in another answer "If you import a directory, how can you determine import order? There's no way that doesn't introduce some new level of complexity."
Some would argue that organizing your files into directories can REDUCE complexity.
My organization's project is a rather complex app. There are 119 Sass files in 17 directories. These correspond roughly to our views and are mainly used for adjustments, with the heavy lifting being handled by our custom framework. To me, a few lines of imported directories is a tad less complex than 119 lines of imported filenames.
To address load order, we place files that need to load first – mixins, variables, etc. — in an early-loading directory. Otherwise, load order is and should be irrelevant... if we are doing things properly.
Solution 2:
This feature will never be part of Sass. One major reason is import order. In CSS, the files imported last can override the styles stated before. If you import a directory, how can you determine import order? There's no way that doesn't introduce some new level of complexity. By keeping a list of imports (as you did in your example), you're being explicit with import order. This is essential if you want to be able to confidently override styles that are defined in another file or write mixins in one file and use them in another.
For a more thorough discussion, view this closed feature request here:
Solution 3:
Check out the sass-globbing project.
Solution 4:
I create a file named __partials__.scss
in the same directory of partials
|- __partials__.scss
|- /partials
|- __header__.scss
|- __viewport__.scss
|- ....
In __partials__.scss
, I wrote these:
@import "partials/header";
@import "partials/viewport";
@import "partials/footer";
@import "partials/forms";
....
So, when I want import the whole partials
, just write @import "partials"
. If I want import any of them, I can also write @import "partials/header"
.