

Libsass – C implementation of a Sass compiler - kudu
https://github.com/hcatlin/libsass

======
hcatlin
Wow, didn't expect to see this at the top of HN. Thanks kudo for promoting the
project!

Aaron Leung and I have been working on this for a while. I'm the original
creator of Sass and Aaron is a badass computer linguist. Hoping to announce
some big new features around the end of the year.

Official site is at: [http://libsass.org](http://libsass.org) Or, follow us on
Twitter for more updates: @hcatlin & @akhleung

~~~
mhogomchungu
a bit of optimization in $src/libsass/copy_c_str.cpp

    
    
            char* copy_c_str(const char* orig)
            {
                    size_t len =  strlen(orig) + 1 ;             
                    char* copy = (char*) malloc(sizeof(char) * len );
                    memcpy(copy, orig, len);
                    return copy;
            }

~~~
unwind
Uh, okay. I didn't read the original, sounds somewhat scary.

Anyway, please don't cast the return value of malloc() in C
([http://stackoverflow.com/a/605858/28169](http://stackoverflow.com/a/605858/28169)).

Also, of course len should be const and sizeof (char) should be removed. Also
testing whether malloc() succeeded before relying on the result being valid is
a good idea (but perhaps there's machinery in place to abort on error), as is
deciding what to do if the input string is NULL.

~~~
dchest
_Anyway, please don 't cast the return value of malloc() in C
([http://stackoverflow.com/a/605858/28169](http://stackoverflow.com/a/605858/28169))._

It's C++.

------
danenania
This is great as compilation in ruby really is painfully slow for anything
substantial, but I wonder about the choice of an independent project vs.
forking the ruby compiler and optimizing the bottlenecks with C extensions.
Might that allow you to fix the most serious issues without having to worry
about maintaining feature parity in a completely separate code base?

In any case, nice work. I look forward to trying it out in my next project.

~~~
iamlacroix
One benefit of being a separate project is that it can be used with non-Ruby
wrappers. See node-sass [https://github.com/andrew/node-
sass](https://github.com/andrew/node-sass) as an example.

~~~
WalterSear
And consequently from that project: [https://npmjs.org/package/grunt-
sass](https://npmjs.org/package/grunt-sass)

Since I'd guess most people interested in the node version will want the grunt
one.

~~~
pselbert
I have been all too pleased to move away from the ruby based compiler and just
use a pure grunt workflow. It is vastly faster and lighter weight.

From what I understand there are some limitations to the implementation, but I
haven't encountered any so far.

~~~
imjared
The few issues I've run into are that I can't use Compass and I can't have
Source Maps (yet).

Instead of Compass, I've switched to using Bourbon (bourbon.io) but may
consider just using Auto Prefixer in future dev efforts.

Source maps I can live without since I haven't gotten used to them. I know
it's in the works or at least being thought about:
[https://github.com/hcatlin/libsass/issues/122](https://github.com/hcatlin/libsass/issues/122).

------
pilif
I'm periodically updating a PHP binding to libsass on my companies github
account here:
[https://github.com/sensational/sassphp](https://github.com/sensational/sassphp)

It was my intention of keeping up with releases, but it looks like updating it
periodically to git HEAD of libsass might be what's needed. I'll look into
getting the submodule updated to a later release today.

That said: last summer when I started maintaining this fork of an earlier
binding, I noticed that libsass (even tag RELEASE-1.0) was in a bit an early
state of development: HEAD was flat-out segfaulting and RELEASE-1.0 was
producing miscompilations.

I've reported a few bugs but in the end, we've settled back with ruby sass for
the time being. The speed up is nice, but only if the library is actually
producing valid output without crashing.

Maybe by now this is better and I should give it another go :-)

Also, I'm certainly no C++ programmer and thus this might be common, but their
template based parser with a lot of implementation code in a header file kind
of scares me
([https://github.com/hcatlin/libsass/blob/master/prelexer.hpp](https://github.com/hcatlin/libsass/blob/master/prelexer.hpp))

~~~
akhleung
We did a lot of development during the summer (including a rewrite that was
long overdue), so there were a few segfaults back then. The library should be
pretty stable now though -- we've embedded it into our SDK and build system,
and it's been working fine.

As for the parser -- that started out as an experiment of sorts. I wanted to
do something like parser combinators (but for scanning), and I decided to
templatize the whole thing so that all of the scanning code would be generated
at compile-time. If I were to redo it, I'd probably use expression templates
(which I didn't know about at the time).

------
mfkp
For all the Go fans out there, I built a sass watcher/compiler using Go +
libsass, I'd love to get some feedback.
[https://github.com/mfkp/gassy](https://github.com/mfkp/gassy)

------
leif
I'm not a web guy, but isn't sass a CSS generator, as in something you run
once before deploying anything? What's the advantage of speed for something
like this?

~~~
hcatlin
So, when big companies like Linkedin generate their Sass projects, it can take
hours on a single machine. Tons of programmers all working in Sass.

At Moovweb, some of our customer's projects can take 30 seconds just for the
Sass to compile for fairly straightforward sites.

~~~
gkoberger
Seriously? I believe you, but if it's taking hours then it seems like the
problem isn't as much with SASS but rather the way they write CSS or their
build process. If they only recompiled changed files, there's no way it could
take "hours".

Even if they recompile everything... all CSS needs to be interpreted by the
browser at some point. How much CSS do they have?

That being said, what are the speed comparisons like?

~~~
icambron
Yeah, I'd love to hear more from Hampton on the hours thing. What actually
takes so long? Why does the number of developers matter? I've worked on some
substantial (though not LinkedIn-sized) projects that used SASS and I've never
clear _seconds_ in compilation time.

Though I completely get the portability aspect.

~~~
hcatlin
They use
[https://github.com/linkedin/archetype](https://github.com/linkedin/archetype)
which is an extension on top of Compass. Including all those libraries can
mean a TON of memory and processing is required. Mixins calling mixins calling
mixins. Plus, custom ruby extensions, etc. The needs of organizing hundreds of
developers logically and making sure the code is optimized can be a hauss.

~~~
icambron
Thanks.

------
__david__
I've been maintaining a Perl binding to libsass.

CPAN page here:
[http://search.cpan.org/perldoc?CSS%3A%3ASass](http://search.cpan.org/perldoc?CSS%3A%3ASass)

Github here: [https://github.com/caldwell/CSS-
Sass](https://github.com/caldwell/CSS-Sass)

------
calinet6
Let's get down to brass tacks—is there a ruby gem which can use this and drop
into the Rails asset pipeline to speed it up? If not, what would be the best
way to get that started?

Edit: found this:
[https://github.com/hcatlin/sassruby](https://github.com/hcatlin/sassruby) \--
Looks like it's not quite mature enough, but promising!

~~~
charliesome
> is there a ruby gem which can use this and drop into the Rails asset
> pipeline to speed it up?

Often the source of slowness in the asset pipeline is not the compilation but
the asset pipeline itself. It does some weird stuff.

I'd imagine dropping a libsass based SASS compiler into a Rails app wouldn't
actually translate into much of a performance improvement.

------
pedrocr
_Any contribution to the project are seen as copyright assigned to Hampton
Catlin, a human on the planet earth. Your contribution warrants that you have
the right to assign copyright on your work. The intention here is to ensure
that the project remains totally free (liberal, like)._

That's a curious setup. The license is MIT so you're not getting the "if you
distribute it you have to use the same license" that you get with GPL. So as
long as the contributions use the same MIT license why does the author need
copyright assignment? The MIT license is simple and standard enough that
relicensing doesn't seem like a likely need.

~~~
hcatlin
Good question. I've gotten conflicting legal advice. Some say I need full
signed contributor agreements for everything, no matter the license, and some
say that's not needed at all. I opted for something in-between. Just a notice
that _could_ cover our ass in the future. Several big companies are working on
submitting code, and so licensing there can get tricky... hence, just go
overkill and never worry about it. ;)

~~~
pedrocr
Thanks for the reply. That makes sense. I don't see why saying "anything
that's contributed is MIT licensed" wouldn't cut it but I'm no lawyer... :)

------
lobster_johnson
Unfortunately it only implements the curly-brace SCSS syntax, not the indented
SASS syntax [1] (which is weird considering it's called Libsass, not Libscss),
and there are no plans to support both syntaxes.

The companies I have worked for all use the indented syntax, since it meshes
very well with HAML and CoffeeScript. But it looks like more people use the
brace syntax (roughly 980k SCSS files on Github, vs. 85k SASS files).

[1]
[https://github.com/hcatlin/libsass/issues/16](https://github.com/hcatlin/libsass/issues/16)

~~~
myers
I agree about fitting in well with HAML and CoffeeScript. You might also
consider Slim as a less noisy HTML shortcut language.

This covers why I like Slim over HAML

[http://me.phillipoertel.com/articles/2013/02/03/why-i-
like-t...](http://me.phillipoertel.com/articles/2013/02/03/why-i-like-the-
slim-template-engine)

~~~
lobster_johnson
I don't know. I wish developers could stop reinventing/tweaking stuff that
isn't particularly broken (the differences between HAML and Slim, for example,
are marginal and not really interesting) and start standardizing on something.
Node, meanwhile, seems to have standardized on Jade.

------
chavesn
I know this is completely juvenile of me, but I can't help but see "libs ass".

It's kind of like "xbone". I'm sure it's not intentional, but people will see
it nonetheless.

------
vezzy-fnord
Given that this is a C++ implementation, why the incorrect article and repo
title?

Was it rewritten in C++ later on?

Otherwise, it seems interesting. I'll look into it.

~~~
solox3
> While libsass is primarily implemented in C++, it provides a simple C
> interface that is defined in [sass_inteface.h].

~~~
sheetjs
To be fair, that argument can be applied to most scripting languages:

A C header for a C++ library that integrates V8 and runs JS code can,
according to your logic, count as a C implementation.

~~~
TheHydroImpulse
No, because it can't be used in straight up C, while C++ libraries that expose
their functionality with valid C can.

------
chrismorgan
So, three months ago they started using Travis CI and, as far as I can tell
from looking at the results ([https://travis-
ci.org/hcatlin/libsass/builds](https://travis-ci.org/hcatlin/libsass/builds)),
_not once have the tests actually passed_. (Builds 1 and 2 failed silently and
build 8 didn't test anything. The rest were all detected as failing.)

~~~
akhleung
Ah, that's because Travis is running all of the "to-do" tests as well.
Reorganizing the tests and making Travis happy is ... well, it's on the to-do
list.

------
pjmlp
It looks like C++ to me.

------
lightblade
Do wanted to note that this does not work with Compass currently as Compass
extends SASS with Ruby.

~~~
legutierr
Are you aware of any plans for Compass to remove the Ruby dependency?

~~~
lightblade
I don't know, but I suspect it doesn't looks positive. Compass haven't had any
new releases for a year now.

~~~
BillSaysThis
Not true--there have been a series of alpha and now RC releases of 0.13. Chris
and Nathan and the others are working on both Sass and Compass in parallel.

~~~
lightblade
Thanks!

I just tried the pre-release version of 0.13. I found it was installing
something with native binding? I believe it was one of the dependencies on
Compass. I don't think this works well for us as our build process relies on
Maven with JRuby to power Compass.

------
smrtinsert
I really want a pure js impl.

~~~
phaer
Write one :)

