1. Установить библиотеку dotless
2. Создаем класс LessTransform.cs, например в папке App_Start
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System.IO; using System.Web.Hosting; using System.Web.Optimization; using dotless.Core.configuration; using dotless.Core.Input; namespace ILK.Web.App_Start { public class LessTransform : IBundleTransform { public void Process(BundleContext context, BundleResponse response) { DotlessConfiguration config = new DotlessConfiguration(); config.MinifyOutput = false; config.ImportAllFilesAsLess = true; config.CacheEnabled = false; config.LessSource = typeof(VirtualFileReader); response.Content = dotless.Core.Less.Parse(response.Content, config); response.ContentType = "text/css"; } } internal sealed class VirtualFileReader : IFileReader { public byte[] GetBinaryFileContents(string fileName) { fileName = GetFullPath(fileName); return File.ReadAllBytes(fileName); } public string GetFileContents(string fileName) { fileName = GetFullPath(fileName); return File.ReadAllText(fileName); } public bool DoesFileExist(string fileName) { fileName = GetFullPath(fileName); return File.Exists(fileName); } private static string GetFullPath(string path) { return HostingEnvironment.MapPath("~" + path); } public bool UseCacheDependencies { get { return false; } } } } |
3. В классе BundleConfig пишем следующий код
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { // Styles var styleBundleList = new List<Bundle> { new StyleBundle("~/css/site-css").Include( "~/content/css/reset.css", "~/content/css/select.less", "~/content/css/site.less" ) }; foreach (var bundle in styleBundleList) { bundle.Transforms.Add(new LessTransform()); bundle.Transforms.Add(new CssMinify()); bundles.Add(bundle); } } } |
4. Для минификации добавьте в BundleConfig:
|
1 2 3 |
#if RELEASE BundleTable.EnableOptimizations = true; #endif |