initial version.
feat: automatically run vite dev server when in Debugger mode. feat: supports hotreload on both js frontend and .net backend.
This commit is contained in:
1 parent
f925e00540
commit
5c81ed18b2
25 files changed
+743
No files matched your search
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace WebApplication1;
|
||||
|
||||
#if DEBUG
|
||||
public class ViteHelper(IOptions<JsonOptions> options)
|
||||
{
|
||||
#else
|
||||
public class ViteHelper(IWebHostEnvironment env, IOptions<JsonOptions> options)
|
||||
{
|
||||
readonly string rootPath = env.WebRootPath ?? Path.Combine(AppContext.BaseDirectory, "wwwroot");
|
||||
#endif
|
||||
readonly JsonSerializerOptions jsonOptions = options.Value.JsonSerializerOptions;
|
||||
|
||||
public Task<string> RenderAsync(string pathBase, string templatePath, string entryScript, Dictionary<string, string> stringTokens = null)
|
||||
{
|
||||
return RenderInternalAsync(pathBase, templatePath, entryScript, null, stringTokens);
|
||||
}
|
||||
|
||||
public Task<string> RenderAsync<T>(string pathBase, string templatePath, string entryScript, T serverData = default, Dictionary<string, string> stringTokens = null)
|
||||
{
|
||||
return RenderInternalAsync(pathBase, templatePath, entryScript, serverData, stringTokens);
|
||||
}
|
||||
|
||||
async Task<string> RenderInternalAsync(string pathBase, string templatePath, string entryScript, object serverData = null, Dictionary<string, string> stringTokens = null)
|
||||
{
|
||||
var path = Path.Combine(AppContext.BaseDirectory, templatePath);
|
||||
var html = await File.ReadAllTextAsync(path);
|
||||
|
||||
if (stringTokens is not null)
|
||||
{
|
||||
foreach (var token in stringTokens)
|
||||
{
|
||||
html = html.Replace($"<!-- @{token.Key} -->", token.Value);
|
||||
}
|
||||
}
|
||||
|
||||
html = html.Replace("<!-- @ServerData -->", serverData is not null ?
|
||||
$"<script>window.__SERVER_DATA__={JsonSerializer.Serialize(serverData, jsonOptions)};</script>" :
|
||||
"");
|
||||
|
||||
#if DEBUG
|
||||
var viteAssets = $"""
|
||||
<script type="module" src="http://localhost:5173/@vite/client"></script>
|
||||
<script type="module" src="http://localhost:5173/{entryScript}"></script>
|
||||
""";
|
||||
#else
|
||||
var manifestPath = Path.Combine(rootPath, "dist", ".vite", "manifest.json");
|
||||
var manifestJson = await File.ReadAllTextAsync(manifestPath);
|
||||
using var manifest = JsonDocument.Parse(manifestJson);
|
||||
|
||||
var entry = manifest.RootElement.GetProperty(entryScript);
|
||||
var jsFile = entry.GetProperty("file").GetString();
|
||||
|
||||
var viteAssets = $"<base href=\"{pathBase}/\"/>" +
|
||||
$"<script type=\"module\" src=\"dist/{jsFile}\"></script>";
|
||||
|
||||
if (entry.TryGetProperty("css", out var cssArray))
|
||||
{
|
||||
foreach (var css in cssArray.EnumerateArray())
|
||||
{
|
||||
viteAssets += $"\n<link rel=\"stylesheet\" href=\"dist/{css.GetString()}\"/>";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return html.Replace("<!-- @ViteAssets -->", viteAssets);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user