Skip to content

Instantly share code, notes, and snippets.

@ScottGuymer
Last active August 29, 2015 13:56
Show Gist options
  • Save ScottGuymer/9053654 to your computer and use it in GitHub Desktop.
Save ScottGuymer/9053654 to your computer and use it in GitHub Desktop.
IBundleTransform and file reader to allow on the fly compilation of less in .net
Public Module BundleConfig
''' <summary>
''' Registers the bundles.
''' </summary>
''' <param name="bundles">The bundles.</param>
''' <remarks></remarks>
Public Sub RegisterBundles(ByVal bundles As BundleCollection)
Dim less = New Bundle("~/bundles/bootstrap").Include("~/content/less/bootstrap.less")
less.Transforms.Add(CType(New LessTransform("~/content/less/"), IBundleTransform))
less.Transforms.Add(New CssMinify())
bundles.Add(less)
Next
End Sub
End Module
Imports System.Web.Optimization
Imports dotless.Core.configuration
Imports dotless.Core
Imports dotless.Core.Loggers
Namespace Branding
''' <summary>
''' Class to provide less transformation functionality
''' </summary>
''' <remarks></remarks>
Public Class LessTransform
Implements IBundleTransform
Private ReadOnly _path As String
''' <summary>
''' Initializes a new instance of the <see cref="LessTransform" /> class.
''' </summary>
''' <param name="path">The path.</param>
''' <remarks></remarks>
Sub New(path As String)
_path = path
End Sub
''' <summary>
''' Processes the specified context.
''' </summary>
''' <param name="context">The context.</param>
''' <param name="response">The response.</param>
''' <remarks></remarks>
Public Sub Process(context As BundleContext, response As BundleResponse) Implements IBundleTransform.Process
Dim config As New DotlessConfiguration()
config.MinifyOutput = False
config.ImportAllFilesAsLess = True
config.CacheEnabled = False
config.LessSource = GetType(LessVirtualFileReader)
#If DEBUG Then
config.Logger = GetType(DiagnosticsLogger)
#End If
LessVirtualFileReader.BasePath = _path
response.Content = Less.Parse(response.Content, config)
response.ContentType = "text/css"
End Sub
End Class
End Namespace
Imports dotless.Core.Input
Imports System.IO
Imports System.Web.Hosting
Namespace Branding
''' <summary>
''' This is an implementation of the dotless IFileReader to
''' enable the @import syntax within less to get the correct
''' folder to import from
''' </summary>
''' <remarks></remarks>
Friend NotInheritable Class LessVirtualFileReader
Implements IFileReader
''' <summary>
''' Shared variable to allow the passing in of the less import folder
''' </summary>
''' <remarks></remarks>
Public Shared BasePath As String
Public Function GetBinaryFileContents(fileName As String) As Byte() Implements dotless.Core.Input.IFileReader.GetBinaryFileContents
fileName = GetFullPath(fileName)
Return File.ReadAllBytes(fileName)
End Function
Public Function GetFileContents(fileName As String) As String Implements dotless.Core.Input.IFileReader.GetFileContents
fileName = GetFullPath(fileName)
Return File.ReadAllText(fileName)
End Function
Public Function DoesFileExist(fileName As String) As Boolean Implements dotless.Core.Input.IFileReader.DoesFileExist
fileName = GetFullPath(fileName)
Return File.Exists(fileName)
End Function
Public ReadOnly Property UseCacheDependencies() As Boolean Implements IFileReader.UseCacheDependencies
Get
Return True
End Get
End Property
''' <summary>
''' This shared function uses the BasePath to return the path to the import file requested
''' </summary>
''' <param name="path"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Shared Function GetFullPath(path As String) As String
Return HostingEnvironment.MapPath(BasePath & "/" & path)
End Function
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment