diff --git a/Moonlight/App/Services/Sessions/BundleService.cs b/Moonlight/App/Services/Sessions/BundleService.cs index 70a70ece..2b0cfbfc 100644 --- a/Moonlight/App/Services/Sessions/BundleService.cs +++ b/Moonlight/App/Services/Sessions/BundleService.cs @@ -4,98 +4,126 @@ namespace Moonlight.App.Services.Sessions; public class BundleService { - private readonly ConfigService ConfigService; - public BundleService(ConfigService configService) { - ConfigService = configService; + var url = configService + .GetSection("Moonlight") + .GetValue("AppUrl"); + + #region JS + + JsFiles = new(); + + JsFiles.AddRange(new[] + { + url + "/_framework/blazor.server.js", + url + "/assets/plugins/global/plugins.bundle.js", + url + "/_content/XtermBlazor/XtermBlazor.min.js", + url + "/_content/BlazorTable/BlazorTable.min.js", + url + "/_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js", + url + "/_content/Blazor.ContextMenu/blazorContextMenu.min.js", + "https://www.google.com/recaptcha/api.js", + "https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.5.0/lib/xterm-addon-fit.min.js", + "https://cdn.jsdelivr.net/npm/xterm-addon-search@0.8.2/lib/xterm-addon-search.min.js", + "https://cdn.jsdelivr.net/npm/xterm-addon-web-links@0.5.0/lib/xterm-addon-web-links.min.js", + url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js", + "require.config({ paths: { 'vs': '/_content/BlazorMonaco/lib/monaco-editor/min/vs' } });", + url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js", + url + "/_content/BlazorMonaco/jsInterop.js", + url + "/assets/js/scripts.bundle.js", + url + "/assets/js/moonlight.js", + "moonlight.loading.registerXterm();", + url + "/_content/Blazor-ApexCharts/js/apex-charts.min.js", + url + "/_content/Blazor-ApexCharts/js/blazor-apex-charts.js" + }); + + #endregion + + #region CSS + + CssFiles = new(); + + CssFiles.AddRange(new[] + { + "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700", + url + "/assets/css/style.bundle.css", + url + "/assets/css/flashbang.css", + url + "/assets/css/snow.css", + url + "/assets/css/utils.css", + url + "/assets/css/blazor.css", + url + "/_content/XtermBlazor/XtermBlazor.css", + url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.css", + url + "/_content/Blazor.ContextMenu/blazorContextMenu.min.css", + url + "/assets/plugins/global/plugins.bundle.css" + }); + + #endregion CacheId = Guid.NewGuid().ToString(); Task.Run(Bundle); } - + + // Javascript public string BundledJs { get; private set; } + public readonly List JsFiles; + + // CSS public string BundledCss { get; private set; } + public readonly List CssFiles; + + // States public string CacheId { get; private set; } public bool BundledFinished { get; set; } = false; private bool IsBundling { get; set; } = false; - public async Task Bundle() + private async Task Bundle() { if (!IsBundling) IsBundling = true; - + Logger.Info("Bundling js and css files"); - + BundledJs = ""; BundledCss = ""; - var url = ConfigService - .GetSection("Moonlight") - .GetValue("AppUrl"); - BundledJs = await BundleFiles( - new[] - { - url + "/_framework/blazor.server.js", - url + "/assets/plugins/global/plugins.bundle.js", - url + "/_content/XtermBlazor/XtermBlazor.min.js", - url + "/_content/BlazorTable/BlazorTable.min.js", - url + "/_content/CurrieTechnologies.Razor.SweetAlert2/sweetAlert2.min.js", - url + "/_content/Blazor.ContextMenu/blazorContextMenu.min.js", - "https://www.google.com/recaptcha/api.js", - "https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.5.0/lib/xterm-addon-fit.min.js", - "https://cdn.jsdelivr.net/npm/xterm-addon-search@0.8.2/lib/xterm-addon-search.min.js", - "https://cdn.jsdelivr.net/npm/xterm-addon-web-links@0.5.0/lib/xterm-addon-web-links.min.js", - url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/loader.js", - "require.config({ paths: { 'vs': '/_content/BlazorMonaco/lib/monaco-editor/min/vs' } });", - url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.js", - url + "/_content/BlazorMonaco/jsInterop.js", - url + "/assets/js/scripts.bundle.js", - url + "/assets/js/moonlight.js", - "moonlight.loading.registerXterm();", - url + "/_content/Blazor-ApexCharts/js/apex-charts.min.js", - url + "/_content/Blazor-ApexCharts/js/blazor-apex-charts.js" - } + JsFiles ); BundledCss = await BundleFiles( - new[] - { - "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700", - url + "/assets/css/style.bundle.css", - url + "/assets/css/flashbang.css", - url + "/assets/css/snow.css", - url + "/assets/css/utils.css", - url + "/assets/css/blazor.css", - url + "/_content/XtermBlazor/XtermBlazor.css", - url + "/_content/BlazorMonaco/lib/monaco-editor/min/vs/editor/editor.main.css", - url + "/_content/Blazor.ContextMenu/blazorContextMenu.min.css", - url + "/assets/plugins/global/plugins.bundle.css", - } + CssFiles ); - + Logger.Info("Successfully bundled"); BundledFinished = true; } - - private async Task BundleFiles(string[] jsUrls) + + private async Task BundleFiles(IEnumerable items) { - var bundledJs = ""; + var bundled = ""; using HttpClient client = new HttpClient(); - foreach (string url in jsUrls) + foreach (string item in items) { - if (url.StartsWith("http")) + // Item is a url, fetch it + if (item.StartsWith("http")) { - var jsCode = await client.GetStringAsync(url); - bundledJs += jsCode + "\n"; + try + { + var jsCode = await client.GetStringAsync(item); + bundled += jsCode + "\n"; + } + catch (Exception e) + { + Logger.Warn($"Error fetching '{item}' while bundling"); + Logger.Warn(e); + } } - else - bundledJs += url + "\n"; + else // If not, it is probably a manual addition, so add it + bundled += item + "\n"; } - return bundledJs; + return bundled; } } \ No newline at end of file diff --git a/Moonlight/Pages/_Layout.cshtml b/Moonlight/Pages/_Layout.cshtml index fbc5889a..975533a9 100644 --- a/Moonlight/Pages/_Layout.cshtml +++ b/Moonlight/Pages/_Layout.cshtml @@ -38,6 +38,7 @@ + @*This import is not in the bundle because the files it references are linked relative to the current lath*@ @if (BundleService.BundledFinished) @@ -46,20 +47,19 @@ } else { - - - - - - - - - - - - - - + foreach (var cssFile in BundleService.CssFiles) + { + if (cssFile.StartsWith("http")) + { + + } + else + { + + } + } } @@ -113,31 +113,17 @@ } else { - - - - - - - - - - - - - - - - - - - - - - - - - + foreach (var jsFile in BundleService.JsFiles) + { + if (jsFile.StartsWith("http")) + { + + } + else + { + @Html.Raw("") + } + } } \ No newline at end of file