How to use ASP.Net MVC 4 to Bundle LESS files in Release mode?

后端 未结 5 1334
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 13:46

I\'m trying to have LESS files in my web project, and have the MVC 4 bundling functionality call into the dotLess library to turn the LESS into CSS, then minify the result a

5条回答
  •  不知归路
    2020-12-04 14:05

    As a complement to the accepted answer, I created a LessBundle class, which is the Less eqivalent of the StyleBundle class.

    LessBundle.cs code is:

    using System.Web.Optimization;
    
    namespace MyProject
    {
        public class LessBundle : Bundle
        {
            public LessBundle(string virtualPath) : base(virtualPath, new IBundleTransform[] {new LessTransform(), new CssMinify()})
            {
    
            }
    
            public LessBundle(string virtualPath, string cdnPath)
                : base(virtualPath, cdnPath, new IBundleTransform[] { new LessTransform(), new CssMinify() })
            {
    
            }
        }
    }
    

    Usage is similar to the StyleBundle class, specifying a LESS file instead of a CSS file.

    Add the following to your BundleConfig.RegisterBundles(BundleCollection) method:

    bundles.Add(new LessBundle("~/Content/less").Include(
                     "~/Content/MyStyles.less"));
    

    Update

    This method works fine with optimization switched off, but I ran into some minor problems (with CSS resource paths) when optimization was switched on. After an hour researching the issue I discovered that I have reinvented the wheel...

    If you do want the LessBundle functionality I describe above, check out System.Web.Optimization.Less.

    The NuGet package can be found here.

提交回复
热议问题