MVC4 Less Bundle @import Directory

前端 未结 10 814
南方客
南方客 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:46

    Here's the simplest version of the code to handle this I could come up with:

    public class LessTransform : IBundleTransform
    {
        public void Process(BundleContext context, BundleResponse bundle)
        {
            var pathResolver = new ImportedFilePathResolver(context.HttpContext.Server);
            var lessParser = new Parser();
            var lessEngine = new LessEngine(lessParser);
            (lessParser.Importer as Importer).FileReader = new FileReader(pathResolver);
    
            var content = new StringBuilder(bundle.Content.Length);
            foreach (var bundleFile in bundle.Files)
            {
                pathResolver.SetCurrentDirectory(bundleFile.IncludedVirtualPath);
                content.Append(lessEngine.TransformToCss((new StreamReader(bundleFile.VirtualFile.Open())).ReadToEnd(), bundleFile.IncludedVirtualPath));
                content.AppendLine();
            }
    
            bundle.ContentType = "text/css";
            bundle.Content = content.ToString();
        }
    }
    
    public class ImportedFilePathResolver : IPathResolver
    {
        private HttpServerUtilityBase server { get; set; }
        private string currentDirectory { get; set; }
    
        public ImportedFilePathResolver(HttpServerUtilityBase server)
        {
            this.server = server;
        }
    
        public void SetCurrentDirectory(string fileLocation)
        {
            currentDirectory = Path.GetDirectoryName(fileLocation);
        }
    
        public string GetFullPath(string filePath)
        {
            var baseDirectory = server.MapPath(currentDirectory);
            return Path.GetFullPath(Path.Combine(baseDirectory, filePath));
        }
    }
    
    0 讨论(0)
  • 2020-12-12 17:48

    A work around that I found that was really helpful was to set the directory before running Less.Parse inside of the LessMinify.Process(). Here is how I did it:

    public class LessTransform : IBundleTransform
        {
            private string _path;
    
            public LessTransform(string path)
            {
                _path = path;
            }
    
            public void Process(BundleContext context, BundleResponse response)
            {
                Directory.SetCurrentDirectory(_path);
    
                response.Content = Less.Parse(response.Content);
                response.ContentType = "text/css";
            }
        }
    

    Then passing in the path when creating the less transform object like so:

    lessBundle.Transforms.Add(
        new LessTransform(HttpRuntime.AppDomainAppPath + "/Content/Less")
    );
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-12 17:49

    I've written a quick blog post about Using LESS CSS With MVC4 Web Optimization.

    It basically boils down to using the BundleTransformer.Less Nuget Package and changing up your BundleConfig.cs.

    Tested with bootstrap.

    EDIT: Should mention the reason I say this, is I also ran into the @import directory structure issue, and this library handles it correctly.

    0 讨论(0)
  • 2020-12-12 17:53

    The issue is that the DynamicFolderBundle reads all the contents of the files and passes the combined contents to the LessMinify.

    As such any @imports have no reference to the location the file came from.

    To resolve this I had to place all the "less" files into one location.

    Then you have to understand the ordering of the files become important. As such I started to rename the file with a number (eg: "0 CONSTANTS.less", "1 MIXIN.less" which means that they are loaded at the top of the combined output before they go into the LessMinify.

    if you debug your LessMinify and view the response.Content you will see the combined less output!

    Hope this helps

    0 讨论(0)
提交回复
热议问题