Skip to content

Instantly share code, notes, and snippets.

@drphrozen
Created September 26, 2012 06:54
Show Gist options
  • Save drphrozen/3786482 to your computer and use it in GitHub Desktop.
Save drphrozen/3786482 to your computer and use it in GitHub Desktop.
LessBundle
public class LessBundle : Bundle
{
public LessBundle(string virtualPath) : base(virtualPath, new LessTransform(), new CssMinify()) { }
}
public class LessTransform : IBundleTransform
{
public void Process(BundleContext context, BundleResponse response)
{
var sb = new StringBuilder();
foreach(var file in response.Files) {
var importer = new Importer(new FileReader(new SimplePathResolver(file.Directory.FullName)));
var parser = new Parser(new PlainStylizer(), importer);
var engine = new LessEngine(parser, new DiagnosticsLogger(LogLevel.Debug), false, true);
using (var stream = file.OpenText())
{
var less = stream.ReadToEnd();
var css = engine.TransformToCss(less, null);
sb.AppendLine(css);
}
}
response.Content = sb.ToString();
response.ContentType = "text/css";
}
}
class MappedImporter : Importer
{
private readonly string _currentDirectory;
public MappedImporter(string currentDirectory)
{
_currentDirectory = currentDirectory;
}
protected override string CurrentDirectory { get { return _currentDirectory; } }
}
class SimplePathResolver : IPathResolver
{
private readonly string _root;
public SimplePathResolver(string root)
{
_root = root;
}
public string GetFullPath(string path)
{
var output = Path.Combine(_root, path);
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment