Converting from LESS to Sass
It would seem that a lot of people coming here are looking for a how more than a why. If this is you, then jump to the regex below.
Starting with LESS
With the extra syntax to learn, and the additional setup on any machine I wanted to dev from, I never really gave Sass much of a look. That is until I started back at work. The first project I got put on starting back at MICROS already had Sass integrated into it, so I had the opportunity to give it a proper trial.
Trying out Sass
My first impressions were as expected, for the most part they're the same. Yes they use different symbols for variables but nest in the same way. On the plus side, I like the greater availability of third party mixin libraries (we chose bourbon) instead of having to sit down and write my own. But on the down side It felt a little unusual adding in the extra
The main thing I like about Sass though is the stability. Others had mentioned it in blog posts I'd seen across the web, but I hadn't truly appreciated it until working with LESS on a big project. Sometimes LESS just throws seemingly random errors, often the feedback for which is completely unhelpful. It also tries to carry on through errors in a way which would seem helpful, but can lead to horrible amounts of debugging further down the line. I had one case where LESS was compiling despite a bracket mismatch in one of the files causing a huge amount of altered styles. Sass on the other hand has precise and strict error handling. If there is an error in a file, it will tell you where, and will stop compiling, simple as that.
Making the switch
Since making the decision to use Sass over LESS as my pre-processor of choice, I decided to convert the styling on my portfolio. Aside from renaming the file extensions, it was a relatively easy syntax change to make. I managed to do most of the changes with a couple of regex 'find and replace' statements in SublimeText, and for anything that didn't catch running Sass's watch command would constantly tell me what and where the next error was.
NB: SublimeText uses \1 \2 and so on to reference the bracket-matches, other editors may use $1 $2 or something different.
Replacing variables syntax
The only different between variables in LESS and variables in Sass is the character they use, so with the exception of the reserved words of the CSS at-rules (
@keyframes etc) and the LESS reserved word
@include, you can just swap the
@ character for the
Replacing includes with extends
On the assumption that when you
@include one class inside another, you do it without brackets, where as including a mixin you use the brackets, you can safely replace
@extend to map this to Sass. Although the functionality is slightly different, the resultant styling should be the same.
Fixin' the mixin
This is the most difficult to straight-swap, and when I did it originally I ended up just doing it by hand as I only had a few. That said, we can make some assumptions of pattern to help us along the way.
We can assume that all remaining instances of
@include will be to include Sass mixins, and so can remove the
# from the front
By assuming that LESS mixins are either a class or an ID, but still end with a bracket, then they can now be identified with this pattern.
Replacing the declaration with Sass's
@mixin directive. This works well for simple cases, but there are some more complex scenarios - like nested mixins - that may still require manual tweaking.
Now all the automated parts are done, it's down to some manual tweaking for any errors. You can run Sass's watch command (
sass --watch input.scss:output.css) so that each time a file is saves, Sass will attempt to compile, and alert you of the first error it finds. The errors are far more helpful than the equivalent in LESS, and the resolutions usually fairly simple to see. When Sass compiles into CSS, you are error free.
A couple of last gripes
I'm not entirely sure I like the way
@extend works. This may just be my aversion to anything that tries to take control of my code, but sometimes the way it renders the code isn't what you expect to happen, and things like backup colours can get stripped out. I also think all of the extra syntax is not completely necessary, and that makes it less accessible, it's part of the reason I didn't choose Sass in the first place.
Several months on (updated: 05/05/2014)
One of the main things that has annoyed me about Sass more recently is how slow it can get on large projects. Sometimes taking 4-6 seconds just to show a one-line change on a large project. There is an interesting looking project where Sass is being rewritten in C, but as you'd expect, it's a bit behind when it comes to new features, and doesn't compile the CSS exactly the same as the ruby version.