The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

114 lines
3.8 KiB

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Teknik.Utilities.TagHelpers
{
[HtmlTargetElement("bundle")]
public class BundleTagHelper : TagHelper
{
private const string VirtualFolder = "./wwwroot/";
private const string ConfigPath = "bundleconfig.json";
private readonly IWebHostEnvironment _env;
private readonly IMemoryCache _cache;
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public bool AppendVersion { get; set; } = false;
public string Src { get; set; }
public BundleTagHelper(IWebHostEnvironment env, IMemoryCache cache)
{
_env = env;
_cache = cache;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
// Clear the initial wrap tag
output.TagName = string.Empty;
// Clean up the src
Src = Src.TrimStart(new [] { '~', '/' });
List<string> files = new List<string>();
switch (_env.EnvironmentName)
{
case "Test":
case "Production":
files.Add(Src);
break;
case "Development":
var configFile = Path.Combine(_env.ContentRootPath, ConfigPath);
var bundle = GetBundle(configFile, Src);
if (bundle == null)
return;
// Clean up the bundle to remove the virtual folder that aspnetcore provides.
files.AddRange(bundle.InputFiles.Select(file =>
{
if (file.StartsWith(VirtualFolder))
return file.Substring(VirtualFolder.Length);
return file;
}).ToList());
break;
}
foreach (var file in files)
{
// Get the file version for this file
string fullPath = "/" + file;
if (AppendVersion)
{
var _fileVersionProvider = ViewContext.HttpContext.RequestServices.GetRequiredService<IFileVersionProvider>();
fullPath = _fileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.Path, fullPath);
}
if (file.EndsWith(".js"))
{
output.Content.AppendHtml($"<script src='{fullPath}' type='text/javascript'></script>");
}
else if (file.EndsWith(".css"))
{
output.Content.AppendHtml($"<link rel='stylesheet' href='{fullPath}'></link>");
}
}
}
public static Bundle GetBundle(string configFile, string bundlePath)
{
var file = new FileInfo(configFile);
if (!file.Exists)
return null;
var bundles = JsonConvert.DeserializeObject<IEnumerable<Bundle>>(File.ReadAllText(configFile));
return (from b in bundles
where b.OutputFileName.EndsWith(bundlePath, StringComparison.InvariantCultureIgnoreCase)
select b).FirstOrDefault();
}
public class Bundle
{
[JsonProperty("outputFileName")]
public string OutputFileName { get; set; }
[JsonProperty("inputFiles")]
public List<string> InputFiles { get; set; } = new List<string>();
}
}
}