MVC4 Less Bundle @import Directory

前端 未结 10 824
南方客
南方客 2020-12-12 17:02

I\'m trying to use MVC4 bundling to group some of my less files, but it looks like the import path I\'m using is off. My directory structure is:

static/
             


        
10条回答
  •  再見小時候
    2020-12-12 17:42

    Here's what I did:

    Added Twitter Bootstrap Nuget module.

    Added this to my _Layout.cshtml file:

    
    

    Note that I renamed my "less" folder to twitterbootstrap just to demonstrate that I could

    Moved all the less files into a subfolder called "imports" except bootstrap.less and (for responsive design) responsive.less.

    ~/Content/twitterbootstrap/imports
    

    Added a configuration in the web.config:

    
    

    Created two classes (slight modification of the class above):

    using System.Configuration;
    using System.IO;
    using System.Web.Optimization;
    using dotless.Core;
    using dotless.Core.configuration;
    using dotless.Core.Input;
    
    namespace TwitterBootstrapLessMinify
    {
        public class TwitterBootstrapLessMinify : CssMinify
        {
            public static string BundlePath { get; private set; }
    
            public override void Process(BundleContext context, BundleResponse response)
            {
                setBasePath(context);
    
                var config = new DotlessConfiguration(dotless.Core.configuration.DotlessConfiguration.GetDefault());
                config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader);
    
                response.Content = Less.Parse(response.Content, config);
                base.Process(context, response);
            }
    
            private void setBasePath(BundleContext context)
            {
                var importsFolder = ConfigurationManager.AppSettings["TwitterBootstrapLessImportsFolder"] ?? "imports";
                var path = context.BundleVirtualPath;
    
                path = path.Remove(path.LastIndexOf("/") + 1);
    
                BundlePath = context.HttpContext.Server.MapPath(path + importsFolder + "/");
            }
        }
    
        public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader
        {
            public IPathResolver PathResolver { get; set; }
            private string basePath;
    
            public TwitterBootstrapLessMinifyBundleFileReader() : this(new RelativePathResolver())
            {
            }
    
            public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver)
            {
                PathResolver = pathResolver;
                basePath = TwitterBootstrapLessMinify.BundlePath;
            }
    
            public bool DoesFileExist(string fileName)
            {
                fileName = PathResolver.GetFullPath(basePath + fileName);
    
                return File.Exists(fileName);
            }
    
            public string GetFileContents(string fileName)
            {
                fileName = PathResolver.GetFullPath(basePath + fileName);
    
                return File.ReadAllText(fileName);
            }
        }
    }
    

    My implementation of the IFileReader looks at the static member BundlePath of the TwitterBootstrapLessMinify class. This allows us to inject a base path for the imports to use. I would have liked to take a different approach (by providing an instance of my class, but I couldn't).

    Finally, I added the following lines to the Global.asax:

    BundleTable.Bundles.EnableDefaultBundles();
    
    var lessFB = new DynamicFolderBundle("less", new TwitterBootstrapLessMinify(), "*.less", false);
    BundleTable.Bundles.Add(lessFB);
    

    This effectively solves the problem of the imports not knowing where to import from.

提交回复
热议问题