Files
mixtape/Finch/Frontend/ViteScriptTagHelper.cs
T

44 lines
1.4 KiB
C#
Raw Normal View History

2026-03-25 16:02:05 +01:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Hosting;
2026-04-07 14:23:29 +02:00
namespace Finch.Frontend;
2026-03-25 16:02:05 +01:00
[HtmlTargetElement("app-vitescript", Attributes = "src", TagStructure = TagStructure.NormalOrSelfClosing)]
2026-04-07 14:23:29 +02:00
public class ViteScriptTagHelper(IWebHostEnvironment env, IFinchOptions options) : TagHelper
2026-03-25 16:02:05 +01:00
{
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; } = null!;
[HtmlAttributeName("src")]
public string Src { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (!env.IsDevelopment())
{
output.SuppressOutput();
return;
}
int? viteProxyPort = 5123;
#if DEBUG
viteProxyPort = options.For<ViteProxy.ViteProxyOptions>().Port;
#endif
HttpRequest request = ViewContext.HttpContext.Request;
string fullPath = $"{request.Scheme}://{request.Host.Host}:{viteProxyPort}/{Src}";
output.TagName = "script";
2026-04-07 15:08:24 +02:00
output.TagMode = TagMode.StartTagAndEndTag;
2026-03-25 16:02:05 +01:00
output.Attributes.SetAttribute("type", "module");
output.Attributes.SetAttribute("defer", string.Empty);
output.Attributes.SetAttribute("crossorigin", string.Empty);
output.Attributes.SetAttribute("src", fullPath);
}
}