67

How can one get a full sass (i.e. scss) precompiler environment up and running in Visual Studio 2015? This is a sibling question to this one concerning less.

Community
  • 1
  • 1
Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69

5 Answers5

107

This can also be achieved without gulp or grunt by simply clicking your way in Visual Studio.

Install Web Compiler

  1. In Visual Studio, press Tools -> Extensions and updates.
  2. Search for Web Compiler (created by Mads Kristensen) and install.
  3. Restart of Visual Studio will be needed.

Compile files

  1. Right-click the .scss-file in Solution Explorer to setup compilation.
  2. Press Web Compiler -> Compile File (Shift+Alt+Q).

A file called compilerconfig.json is created in the root of the project where you can modify the behavior of the compiler. Right-clicking the compilerconfig.json file let’s you easily run all the configured compilers.

Any time a .scssfile is modified within Visual Studio, the compiler runs automatically to produces the compiled output file.

Compile on Build

  1. Right-click the compilerconfig.json file
  2. Press Web Compiler -> Enable compile on build
  3. Clicking the menu item will prompt you with information that a NuGet package will be installed into the packages folder without adding any files to the project itself. The NuGet package contains an MSBuild task that will run the exact same compilers on the compilerconfig.json file in the root of the project.
Community
  • 1
  • 1
willedanielsson
  • 1,333
  • 3
  • 11
  • 14
  • 1
    I guess the NuGet package needs to be added to the project for this to be integrated into an automated build chain, right? – nyaray Mar 14 '16 at 10:24
  • I'm not getting the 'Enable compile on build' option – Chris Nevill Jun 28 '16 at 16:30
  • Hmm guess it's because I'm using a new ASP.NET Core template – Chris Nevill Jun 28 '16 at 16:36
  • @ChrisNevill I had the same thing. Restarting VS made it show up, but it's still grayed out/inactive. Did you find a solution? I'm on community version of VS2015 and an older .NET Framework. – John Pasquet Jun 28 '16 at 18:57
  • 1
    Mads Kristensen confirmed that it only worked with MsBuild which I believe the ASP.NET Core template doesn't use. So I'm now looking for other solutions. – Chris Nevill Jun 29 '16 at 07:57
  • Nice one, didn't know about the Compile on Build feauture – AlexanderD Oct 27 '16 at 15:05
  • Be careful for the *Compile on Build*, I had to first generate the CSS file (by right clicking on the SCSS file > Web Compiler > Compile file) so an entry is added in the file _compilerconfig.json_. Only afterwards, the auto-compilation worked fine for that file. – Jämes Apr 28 '17 at 12:30
31

If using Gulp + Visual Studio 2015

  1. First install gulp-sass this is the package.json file
{
      "name": "ASP.NET",
      "version": "0.0.0",
      "devDependencies": {
        "gulp": "3.8.11",
        "gulp-concat": "2.5.2",
        "gulp-cssmin": "0.1.7",
        "gulp-uglify": "1.2.0",
        "rimraf": "2.2.8",
        "gulp-sass": "2.2.0"
      }
  1. On the gulpfile.js
   var sass = require("gulp-sass");
   var paths = {
     webroot: "./wwwroot/"
   }
   paths.scss = paths.webroot + "css/**/*.scss";
   gulp.task('sass', function() {
     gulp.src(paths.scss)
       .pipe(sass())
       .pipe(gulp.dest(paths.webroot + "css"));
   });
   gulp.task('watch-sass', function() {
     gulp.watch(paths.scss, ['sass']);
   })
  1. Now open the "Task Runner Explorer" and double click on the task: "watch-sass" Task Runner Explorer

Now every time you save the scss the css will compile and change sass file.

Sanchitos
  • 8,423
  • 6
  • 52
  • 52
  • 1
    also, right click watch-sass from the Task Runner Explorer pane and select Bindings > Project Open. Now every time you open this project and change and save *.scss files it will already be watching and transpiling. Also, probably obvious, but put your *.scss files in your wwwroot/css folder, and it will auto hide the generated *.css file behind the sass files. – Evan Morrison Apr 28 '16 at 23:48
  • I need to revisit this question, things have changed a lot in just a year, your way using gulp (which I have "switched" to from grunt - in quotes because I'm hardly any expert in it) is the way I am most interested in, it is a native way of doing things. So when I get back into this I may need to mark this as the answer. Thanks to everyone elses answers here as well though, those are good contributions on how to skin this cat. But I like the native way. – Nicholas Petersen Jun 20 '16 at 17:18
  • More info can be found here: http://developer.telerik.com/featured/css-preprocessing-with-visual-studio/ – dance2die Sep 19 '16 at 14:48
9

Web Compiler Extension for VS2015

Gulp is required. npm install -g gulp

Hope this helps... a few less hoops.

Edit:

I encountered an issue with the web compiler converting strings to Unicode characters when it should not have been doing so. I switched to this implementation http://blog.oxfordcc.co.uk/compiling-sass-gulp-visual-studio/ in VS2015 and it's compiling the CSS correctly. Added just in case someone else encounters the same issue.

Jim Harkins
  • 383
  • 4
  • 15
6

If you don't want to mess around with the intricacies of setting up the compilation manually, there are a number of amazingly simple extensions available that can handle this for you:

WebCompiler (FREE)

It's been mentioned already but Mads Kristensen (the author of web essentials) has created a standalone compilation tool called Web Compiler. All you have to do is install it, then right click on any of the SASS files you want to compile and select Web Compiler > Compile File. From that point on it is watched and anytime it is saved, the file will be compiled.

Download Web Compiler


CompileSASS (FREE)

Similar to Web Compiler this is a standalone extension that was created to work in both VS2013 and VS2015 because compilation was removed from the popular Web Essentials extension. It's lightweight and does the job very well with great error reporting. Read the author's blog about the extension here.

Download Compile SASS


Web Workbench (FREE/PAID)

Mindscape's Web Workbench was my favourite extension for many years when compiling SASS but I have since moved away in favour of the free alternatives. Still, the Pro version is a powerful tool with many ways to customise the outputted files but it is also quite expensive ($39) considering there are free tools out there.

Download Web WorkBench

Community
  • 1
  • 1
Chris Spittles
  • 15,023
  • 10
  • 61
  • 85
4

Most of the steps in answer to this question are identical to the steps that were given by 'Maverick' on this post that concerned how to do the same thing for less. However, someone did not allow me to alter that question to simply include sass (which maybe was best, I don't know). So the following answer relies on the steps specified by Maverick in that post above, with these differences:

(Pre-step for Empty Projects) If you started with an empty project, first add Grunt and Bower:

Right click solution -> Add -> 'Grunt and Bower to Project' (then wait for a minute for it to all install)

package.json:

"devDependencies": {
    "grunt": "^0.4.5",
    "grunt-bower-task": "^0.4.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-contrib-sass": "^0.9.2"
}

(FYI: grunt-contrib-sass link)

Then:

Dependencies -> right-click NPM -> Restore Packages.

gruntfile.js

1) Add or make sure these three lines are registered near the bottom (as NPM tasks):

grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-sass");

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). }

// ... after bower in grunt.initConfig ...
"default": {
    "files": [
        {
            "expand": true,
            "src": [ "someSourceFolder/**/*.scss" ],
            "dest": "wwwroot/coolbeans", // or "<%= src %>" for output to the same (source) folder
            "ext": ".css"
        }
    ]
},
"watch": {
    "sass": {
        "files": [ "someSourceFolder/**/*.scss" ],
        "tasks": [ "sass" ],
        "options": {
            "livereload": true
        }
    }
}

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.

Community
  • 1
  • 1
Nicholas Petersen
  • 9,104
  • 7
  • 59
  • 69