From 454d04bc8bf519ba0477954f12555d04f590fcfa Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Wed, 25 Mar 2026 16:02:05 +0100 Subject: [PATCH] add vite script taghelper --- zero/Frontend/ViteScriptTagHelper.cs | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 zero/Frontend/ViteScriptTagHelper.cs diff --git a/zero/Frontend/ViteScriptTagHelper.cs b/zero/Frontend/ViteScriptTagHelper.cs new file mode 100644 index 00000000..7753b013 --- /dev/null +++ b/zero/Frontend/ViteScriptTagHelper.cs @@ -0,0 +1,44 @@ +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; +using Microsoft.Extensions.Options; + +namespace zero.Frontend; + +[HtmlTargetElement("app-vitescript", Attributes = "src", TagStructure = TagStructure.NormalOrSelfClosing)] +public class ViteScriptTagHelper(IWebHostEnvironment env, IZeroOptions options) : TagHelper +{ + [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().Port; +#endif + + HttpRequest request = ViewContext.HttpContext.Request; + string fullPath = $"{request.Scheme}://{request.Host.Host}:{viteProxyPort}/{Src}"; + + output.TagName = "script"; + output.Attributes.SetAttribute("type", "module"); + output.Attributes.SetAttribute("defer", string.Empty); + output.Attributes.SetAttribute("crossorigin", string.Empty); + output.Attributes.SetAttribute("src", fullPath); + } +} \ No newline at end of file