Ok, so I've created a new ASP.Net 5
/MVC 6
project in Visual Studio 2015 Preview. In keeping with our current method of doing things, for styling I want to use .less
files. Creating the files is straightforward, but Web Essentials no longer compiles them.
So my question is this: what precisely do I need to do to get my .css
files generated when I save the .less
files?
Based on my adventures getting Typescript to work nicely, I will have to use Grunt
to accomplish this task, but I am brand-new to Grunt and so I'm not sure how one would do it?
Please help!
With VS 2015 Web Essential is split into multiple extensions you can download the Web Compiler extension from here and it also has details on how to use it.
It is certainly not elegant as it used to be, but if you are using existing project and want to use a compiler for LESS then this may do the basic job.
So here's how to do it (compile on build and non-elegant compile on save):
Step 1
Open up your
package.json
file (it's in the root of your project) and add these lines:Obviously you can change the version numbers (you'll get helpful intellisense), these are just the current versions.
Step 2
Right-click on the
NPM
folder (underDependencies
) and clickRestore Packages
. This will installless
andgrunt-contrib-less
.Step 3
Once those packages are restored, go to your
gruntfile.js
file (again, in the root of the project). Here, you'll need to add the following section togrunt.initConfig
You'll also need to add this line near the end of
gruntfile.js
:Step 4
Then just go to
View->Other Windows->Task Runner Explorer
in the menu hit the refresh icon/button, then right-click onless
underTasks
and go toBindings
and tickAfter Build
.Hooray, now less files will compile and we (I) learned about grunt, which seems really powerful.
Step 5: Compiling on save
I still haven't got this working to my satisfaction, but here's what I've got so far:
As above, add another NPM package
grunt-contrib-watch
(add to package.json, then restore packages).Then add a watch section in gruntfile.js, like this (obviously this can work for other types of files as well):
So you'll now have something like this in your gruntfile.js:
One can then simply set this task to run on Project Open (right-click on
watch
underTasks
in theTask Runner Explorer
(it's underView->Other Windows
in the top menu) and you're done. I would expect you'd have to close and re-open the project/solution to get this to kick in, otherwise you can manually run the task.(Note: there is now a new question asked here directly concerning sass. I tried to alter the question and tags in this question to include sass, but someone didn't allow it.)
I would like to add the answer to the same question for sass (.scss). The answer is so related I think these may best be combined as two answers in this same post (if you disagree, please let me know; else, we might add "or sass" in the post title?). As such, see Maverick's answer for some fuller details, here's the nutshell for sass:
(Pre-step for Empty Projects) If you started with an empty project, first add Grunt and Bower:
package.json:
(FYI: grunt-contrib-sass link)
Then:
gruntfile.js
1) Add or make sure these three lines are registered near the bottom (as NPM tasks):
2) Again in gruntfile.js, add init configurations, something like the following.
{ Caveat: I am no expert on such configurations. I found the sass configuration on an excellent blog post some time ago that I can't locate at this time in order to give credit. The key was I wanted to find all files in the project within a certain folder (plus descendants). The following does that (notice
"someSourceFolder/**/*.scss"
, and see important related note here). }Now follow the instructions for Task Runner Explorer as given in the other answer. Make sure to close and reopen project. It seems you have to run (double click) 'watch' (under 'Tasks') every time the project is started to get the watch watching, but then it works on subsequent saves.