Automatic Sass Imports with Sass Globbing
Sass is awesome, and I use it 99% of the time now. That 1% is when I’m working on a legacy project that I likely didn’t start. I have one hang up on it though.
It’s hard to rank them, but perhaps my favorite aspect of Sass is the concept of partials. For those that aren’t familiar a partial is a standalone Sass file that has a filename starting with
_ and that underscore in the filename prevents Sass from creating an actual build file from it and instead allowing you to utilize it in a ‘parent’ Sass file. Using partials you can physically separate your module styles, which really sends home the modular message.
So back to my issue: I create a lot of partials, which means each time I add a new module, I have to open the ‘parent’ Sass file and make sure I
@import that module. It gets tedious, especially with new projects.
The standard solution for an issue like this is what’s referred to as globbing, where software will take everything in a directory and use it. It sounds like a perfect match for my personal issue, but Sass doesn’t facilitate globbing. I get why: globbing means a mechanical way of including files, usually based on their file name, ordered alphabetically. That means that the order in which a globbing routine processes files will likely change over time. As we all know with CSS, there are many ways to be more or less specific, and changing the order of an
@import in Sass could greatly change what renders on the page.
So I get the decision, but I as a developer would love to be able to take the responsibility of setting some sort of configuration flag to allow globbing and be victim to the modularity of my modules. Ryan offers his own take on a solution, and while that rocks, if I were to implement it I’d lose the standardized nature of Sass so collaboration would require a new dependency. Ryan has done everything in his power to fall in line with the Sass workflow, and that’s awesome, but every time I’ve utilized a 3rd party
gem it always raises eyebrows with sysadmins I work with because it’s one more thing they need to add to their build process. They don’t like that.