CoffeeScript, Sass and LESS support for Visual Studio and ASP.NET with the Mindscape Web Workbench
There's some really impressive stuff happening in the .NET Community lately. Folks are reaching outside their standard built-in tools and pulling inspiration from everywhere. It's been said that (some) Microsoft developers don't like to use tools or technologies that aren't built in to Visual Studio. However, myself and others have been pushing the concept of LEGO blocks snapping together. Rather than thinking of Visual Studio as a giant single block, consider it as a small block amongst many others. Feel empowered to choose the technologies that work for you and discarding the ones that don't. I talked about this LEGO analogy in my DevDays keynote in The Netherlands earlier in the year.
Snap in tools like the HTML5 Web Standards Update for Visual Studio, loggers/profilers/debuggers like Glimpse, MiniProfiler and ELMAH, Package Managers like NuGet and OpenWrap, all work together to make your development experience more enjoyable (and some new Visual Studio Styles/Themes and a little fresh wallpaper never hurt either! You can even make VS2010 look like 2008 if it make you happy).
One of the most impressive (free!) tools I've seen lately is the Mindscape Web Workbench. It is a 100% free plugin for Visual Studio 2010 to provide CoffeeScript, Sass and Less editing.
What? CoffeeScript? Sass? Less? What are these silly names and why should I care? Well, remember when I blogged about "fanciful names?" These are some names you'll want to know about. Here's a little about each and how Mindscape makes them fun for Visual Studio Developers.
CoffeeScript
"CoffeeScript is a little language that compiles into JavaScript." I've blogged a few times lately about how JavaScript is becoming not just THE ubiquitous language on the web, but also a valid target for high level languages to compile to. Script# compiles C# to JavaScript, GWT compiles to JavaScript, ClojureScript compiles to JavaScript, etc. However, each of these languages has semantics that require some significant mapping to JavaScript. They aren't JavaScript and don't know about it.
Here's the difference with CoffeeScript. "The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime."
CoffeeScript "uplevels" JavaScript to make it more expressive and more idiomatic. You know when you're explaining something and you can't think of a word? Or perhaps you don't know the right word? CoffeeScript adds those new words to JavaScript and you find yourself going, YES! That's what I meant to say!
For example, from their website, here's some CoffeeScript:
# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
And here's what it "compiles" into:
number = 42;
opposite = true;
if (opposite) {
number = -42;
}
So what, you say. How about:
# Existence:
alert "I knew it!" if elvis?
Turning into...
if (typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!"); }
Now we're talking. See how one is more expressive (and more enjoyable) then the other? This has a multiplying effect and makes your CoffeeScript code more expressive and your resulting JavaScript more robust.
list = [1, 2, 3, 4, 5]
square = (x) -> x * x
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
cubes = (math.cube num for num in list)
becomes
list = [1, 2, 3, 4, 5];
square = function(x) {
return x * x;
};
math = {
root: Math.sqrt,
square: square,
cube: function(x) {
return x * square(x);
}
};
cubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return _results;
})();
Remember how jQuery made you feel empowered? Released from tedious DOM code? Well, CoffeeScript does that for tedious JavaScript code.
Here's what it looks like in Visual Studio. Oh, yes.
All this is enabled by the Free Web Workbench.
Sass
CSS is great and we all have a love hate relationship with it. When it works, it's great. Demos are nice, but the reality of CSS (much like that of JavaScript) is that it never quite reads like the fine poetry you were hoping for.
Two syntaxes are trying to improve on CSS in the same way that CoffeeScript improves on JavaScript. Remember that the reality is we can't change CSS and JavaScript, but we can change the Domain Specific Language that we write them in. You can't change Aseembler, but you can use C. Then layer in #defines, or perhaps just use C++...you get the idea. Up level your abstractions and favor the more expressive language.
Sass could mean Super Awesome Style Sheets. It's a meta-language on top of CSS. A better, redesigned CSS that is still a super set of CSS itself. CSS, cascading though it is, is still very flat.
If you write this SASS, it makes sense, doesn't it? It's intuitive and you might even think it's CSS as it is.
.fakeshadow {
border: {
style: solid;
left: {
width: 4px;
color: #888;
}
right: {
width: 2px;
color: #ccc;
}
}
}
Here's the resulting CSS:
.fakeshadow {
border-style: solid;
border-left-width: 4px;
border-left-color: #888;
border-right-width: 2px;
border-right-color: #ccc; }
This clean nesting works with #IDs of course as well such that this Sass:
#navbar {
width: 80%;
height: 23px;
ul { list-style-type: none; }
li {
float: left;
a { font-weight: bold; }
}
}
expands to this valid CSS:
#navbar {
width: 80%;
height: 23px;
}
#navbar ul {
list-style-type: none;
}
#navbar li {
float: left;
}
#navbar li a {
font-weight: bold;
}
Which would you rather write? I'm sold.
Sass uses an indentation syntax primarily but you can use brackets rather than indentations and semicolons rather than new lines. It's up to you. I like mine to look like nested CSS.
Your Sass files are converted into CSS as you click save in Visual Studio. It's a wonderfully clean integration.
Sass is attractive because you really don't need to learn another syntax. You just use scoping and indenting rules you already know and you get cleaner CSS.
But, there's another alternative...
Less
Less extends CSS and adds more dynamic behaviors, variables, functions and more. Because it's more complex and a more dynamic translation, LESS actually runs on the client side and is supported by a client side JavaScript file called http://lesscss.googlecode.com/files/less-1.1.3.min.js.
Because of this, the Web Workbench doesn't do the same automatic developer-time conversion of LESS files. For me, this makes LESS (ahem) less attractive.
That said, you still these features from the Web Workbench, which is nothing to sneeze at.
- Syntax highlighting
- Intellisense
- Warnings of syntax errors
- Warnings of unknown variables and mixins
- Go to variable or mixin definition
Here's an example LESS file...
@the-border: 1px;
@base-color: #111;
@red: #842210;
#header {
color: @base-color * 3;
border-left: @the-border;
border-right: @the-border * 2;
}
#footer {
color: @base-color + #003300;
border-color: desaturate(@red, 10%);
}
This is then compiled and results this CSS. Note what happened with functions, statements and variables:
#header {
color: #333;
border-left: 1px;
border-right: 2px;
}
#footer {
color: #114411;
border-color: #7d2717;
}
Interesting stuff. I think it's important not only that you, Dear Reader, give this stuff a good hard look, but that you continue to look for ways that the open source community, both .NET and otherwise, are innovating. You maybe able to integrate these tools easily into your existing projects and not only have more fun but be more productive.
Thanks so much to the folks at Mindscape for releasing this free and elegant tool!
Related Links
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
The updates through the Visual Studio Extension Manager is a great way for folks to keep grabbing the latest bits - bug fixes, general improvements and new features.
Thanks,
John-Daniel Trask
What bothered me the most is that you still have to write stuff like a:hover separately, you can't just do something like:
a {
text-decoration: underline;
:hover { text-decoration: none; }
}
because this translates to:
a { text-decoration: underline; }
a :hover { text-decoration: none; }
and that does not work as intended. And this is a trivial example. But what if you have a .big nested in an anchor element in LESS or SCSS? Is that really a .big or a.big? Two very different things. But all in all LESS and SCSS are still way better then writing CSS the old fashioned way.
I hope Mindscape includes the LESS compiler and removes those annoying sqiugglies.
It's easy to install, just install ruby (www.rubinstaller.org) and grab Compass (www.compass-style.org).
I build a little Windows service that will watch a folder and subfolders for modified Sass files and automatically recompile them into css. If this would be useful for you, @lucasjans on Twitter.
but really what are we doing ? why no one tries to change some of these assemblies ( I mean big companies which are holder of browsers ) I mean javascript , css or event html? I feel that like old asp we all trying to cover lack of new technologies we need by creating some add-on frameworks and etc.it seems that such VS 5 we will have lots of components active x which will confuse us in the future for finding which is better really or which is not ... I can't beleive that an idea from about 20 years ago is the best one for today ...
Making sure a Visual Studio Extension works well is like all installable software - we've tested it on many different setups and configurations. We have many users without any issues at all and dog food the software ourselves but inevitably some configurations we haven't seen do cause hiccups. We would love feedback on those situations so we can make this free tool better for everyone :-)
@gligoran We're looking at getting the LESS compiler included as it has been requested a lot. We tighten up on the squigglies with each release. If you have specific situations do not hesitate to send them to us and we'll ensure the parser is doing the right thing. Squigglies are only good if they are showing actual mistakes, otherwise, as you say, they're just annoying!
John-Daniel Trask
a {
text-decoration: underline;
&:hover { text-decoration: none; }
}
Note the ampersand in front of :hover. This will do what you want.
Gidon
@John-Daniel: That would be great to have a built-in LESS compiler.
There are a lot of squigglies in my styles, but an example from the starter ASP.NET MVC 3 application in the Site.css is the font-size: .85em. Note that there's no leading zero. I've checked this with the W3C validator and is valid CSS. Your editor marks it as syntax error, although it SCSS compiles it correctly. Also I would be great to have the Format Document functionality as CSS and other native Visual Studio documents have, but that's of lower priority.
But don't mistake my complaining for criticism. It's great to see proper SCSS and LESS support in Visual Studio :)
-- Goran
.line {
+ .line { /* some css */ }
> .something { /* some css */ }
}
the indented + and > are squiggled.
Vs SASS creating it's own DSL with crazy whitespace/tab issues targeted towards ruby programmers. (of course it now supports regular CSS, but only after Less came out).
The plugin will definitely have value when the Less syntax being supported without errors from within Visual Studio and Dot Less itself integrated.
Anybody else is having the same issue?
I created an extension to execute CoffeeScript and JavaScript programs from Visual Studio 2010.
See <a @href="http://visualstudiogallery.msdn.microsoft.com/0cc86917-7f3a-4646-9a29-de997fe1ba23">CoffeeScriptRunnerVSPackage</a>.
It is part of my project <a @href="https://github.com/fredericaltorres/DynamicJavaScriptRunTimes.NET">DynamicJavaScript.Net</a> on Github, which is about using JavaScript from C# programs.
The JavaScript runtime used is Noesis JavaScript.Net which use a version of Google V8 from 2010, it is really fast.
For LESS what about the haack's T4 template? I've never used it, but i remember coming across it in the past:
http://haacked.com/archive/2009/12/02/t4-template-for-less-css.aspx
Great article as always though :-) coffee script sounds interesting, but i'm already planning on learning knockout.js, backbone.js and maybe node.js! Too much javascript at the moment, exciting times
BOb
However, what LESS does have that SASS doesn't, is the client-side translation via Javascript. It's not something I'd use in production, but it makes it easy to play around with LESS if you're just learning and want to try it out.
I'd have to agree with a number of commenters there's a lot of OLD OUTDATED MISSINFORMATION on blog posts that look at both Less and SASS, in particular citing the client-side compilation I've used Less for over 10 months now and not once have I used the client-side scripts. Everything I deliver is pure CSS.
Please update the blog post to make it clear that Less can be compiled OFF the client exceeding easy, there are a number of options.
SASS is nice too, I've used that on a number of projects also, currently I'm preferring less over SASS, but who knows that might change
Either via the command-line utility lessc which is packaged with the official less download, which I originally wrote a wrapper for that listened for file changes and automatically generated the CSS to be delivered. lesscss.org
OSX has the Less.app that works a treat, this is currently my favourite util on OSX. less.app
Another OSX app is SimpleLess, this looks similar to the Less.app but I've never used it : SimpleLess
As for Windows you can either write your own wrapper, it's really trivial using .net FileSystemWatcher but there are also some other solutions :
WinLess, I've used this when using a windows box and works a treat. Works very similar to Less.app, watches files & folders for changes to .less files and automatically compiles the CSS on changes to these files Win Less.
If you're using Visual Studio, that I imagine people reading this post are WinLess also have a really simple script that allows you to add the compilation of the CSS to the build event of the Visual Studio Solution. So rather than generating the CSS on every save it will compile on when you compile your C#, or whatever.
Another option is dotless, I found this not a great tool and TBH I'm not convinced on getting the server to do the complication via a HTTPHandler IMHO that's going to be asking for trouble, but nevertheless it works DotLessCSS
The worse side of SASS and LESS is that we cannot easily append live updates directly from firebug , back to css files.
In normal cases you have SAVE Button in LIVE preview mode of CSS files inside firebug. And if you do not use webkit stuff you can save back.
I keep webkit stuff, gradients etc in different files.
Comments are closed.