Skip to content

Instantly share code, notes, and snippets.

@cjsewell
Created February 6, 2020 01:42
Show Gist options
  • Save cjsewell/155311c09aa098195a085d0114a41868 to your computer and use it in GitHub Desktop.
Save cjsewell/155311c09aa098195a085d0114a41868 to your computer and use it in GitHub Desktop.
Laravel mix utility module
Imports System.IO
Imports System.Web.Script.Serialization
Namespace Laravel
Public Module Mix
Private ReadOnly ManifestDirectory = "~/client"
Private ReadOnly Property ManifestFile As String
Get
Return HttpContext.Current.Server.MapPath($"{ManifestDirectory}/mix-manifest.json")
End Get
End Property
Private ReadOnly Property HotFile As String
Get
Return HttpContext.Current.Server.MapPath($"{ManifestDirectory}/hot")
End Get
End Property
Private ReadOnly Property IsHot As Boolean
Get
Return File.Exists(HotFile)
End Get
End Property
Private ReadOnly Property HotPrefix As String
Get
Return If(IsHot, File.ReadAllText(HotFile).Trim.TrimEnd("/"c), "")
End Get
End Property
Private ReadOnly Property Manifest As Dictionary(Of String, String)
Get
If HttpContext.Current.Items("MixManifest") Is Nothing Then
If File.Exists(ManifestFile) Then
Dim manifestJson As String = File.ReadAllText(ManifestFile)
Try
Dim jss As New JavaScriptSerializer()
HttpContext.Current.Items("MixManifest") = jss.Deserialize (Of Dictionary(Of String, String))(manifestJson)
Catch ex As Exception
End Try
End If
End If
Return CType(HttpContext.Current.Items("MixManifest"), Dictionary(Of String, String))
End Get
End Property
Public Function Mix(requirement As String) As String
If (Not requirement.StartsWith("http") AndAlso Manifest.ContainsKey(requirement)) Then
Dim path = Manifest(requirement).TrimStart("/"c)
Return If(IsHot, $"{HotPrefix}/{path}", VirtualPathUtility.ToAbsolute($"{ManifestDirectory}/{path}"))
End If
Return requirement
End Function
End Module
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment