I have already written about the advantages of a CSS preprocessor like LESS in this post. Today I want to show you how you can easily implement support for such a preprocessor in your ASP.NET Core project. I will use Gulp to create the ability to write LESS files and convert them to CSS files so that they can be integrated into the ASP.NET Core application. Finally, I will automate the whole thing so that the LESS file is converted without manual interaction on every build.
To convert from LESS to CSS I use Gulp. Gulp is a workflow automation tool for JavaScript. This allows me to implement the whole thing per npm into my ASP.NET core project. I don’t have any node packages in use yet, so I need a new package.json file first. I don’t create it with Visual Studio because I didn’t get it to work correctly. You are welcome to try it, but if it doesn’t work either, try the following:
Start the setup wizard using npm init, and fill in the configuration as needed. Then I need the following dependencies, which will be added to the dev-dependencies section. After adding them. the area will look something like this:
|
|
Now I need to setup gulp using a new file called gulpfile.js:
|
|
And that’s about it, actually. If your LESS files are in a directory other than “Styles”, you have to change the configuration accordingly. You can now start the conversion with the following command:
|
|
Now I would like to automate this, because I do not want to manually execute the conversion everytime I changed something. To achieve that, I edit the .csproj file of my project by hand and add the following command:
|
|
That’s it. That’s it. The conversion process is now fully automated and you can define and use your LESS files without worrying about the conversion.