From cfa9adc87eb66a8c5f2aa31771e4c768c83d66e3 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 18 Feb 2021 10:07:09 +0100 Subject: [PATCH] Split backoffice core and extensions JS into two different bundles and disable minification for all core JS bundles --- .../WebAssets/IRuntimeMinifier.cs | 3 ++- .../WebAssets/BackOfficeWebAssets.cs | 23 ++++++++++--------- .../WebAssets/RuntimeMinifierExtensions.cs | 6 +++-- .../SmidgeRuntimeMinifier.cs | 5 ++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Core/WebAssets/IRuntimeMinifier.cs b/src/Umbraco.Core/WebAssets/IRuntimeMinifier.cs index 2b68a2f4ec..1edf134171 100644 --- a/src/Umbraco.Core/WebAssets/IRuntimeMinifier.cs +++ b/src/Umbraco.Core/WebAssets/IRuntimeMinifier.cs @@ -40,6 +40,7 @@ namespace Umbraco.Core.WebAssets /// Creates a JS bundle /// /// + /// /// /// /// All files must be absolute paths, relative paths will throw @@ -47,7 +48,7 @@ namespace Umbraco.Core.WebAssets /// /// Thrown if any of the paths specified are not absolute /// - void CreateJsBundle(string bundleName, params string[] filePaths); + void CreateJsBundle(string bundleName, bool optimizeOutput, params string[] filePaths); /// /// Renders the html script tag for the bundle diff --git a/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs b/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs index 93250ef981..3e82fd1dc4 100644 --- a/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs +++ b/src/Umbraco.Infrastructure/WebAssets/BackOfficeWebAssets.cs @@ -21,7 +21,8 @@ namespace Umbraco.Web.WebAssets public const string UmbracoPreviewCssBundleName = "umbraco-preview-css"; public const string UmbracoCssBundleName = "umbraco-backoffice-css"; public const string UmbracoInitCssBundleName = "umbraco-backoffice-init-css"; - public const string UmbracoJsBundleName = "umbraco-backoffice-js"; + public const string UmbracoCoreJsBundleName = "umbraco-backoffice-js"; + public const string UmbracoExtensionsJsBundleName = "umbraco-backoffice-extensions-js"; public const string UmbracoTinyMceJsBundleName = "umbraco-tinymce-js"; public const string UmbracoUpgradeCssBundleName = "umbraco-authorize-upgrade-css"; @@ -62,20 +63,23 @@ namespace Umbraco.Web.WebAssets _runtimeMinifier.CreateCssBundle(UmbracoPreviewCssBundleName, FormatPaths("assets/css/canvasdesigner.css")); - _runtimeMinifier.CreateJsBundle(UmbracoPreviewJsBundleName, + _runtimeMinifier.CreateJsBundle(UmbracoPreviewJsBundleName, false, FormatPaths(GetScriptsForPreview())); - _runtimeMinifier.CreateJsBundle(UmbracoTinyMceJsBundleName, + _runtimeMinifier.CreateJsBundle(UmbracoTinyMceJsBundleName, false, FormatPaths(GetScriptsForTinyMce())); + _runtimeMinifier.CreateJsBundle(UmbracoCoreJsBundleName, false, + FormatPaths(GetScriptsForBackOfficeCore())); + var propertyEditorAssets = ScanPropertyEditors() .GroupBy(x => x.AssetType) .ToDictionary(x => x.Key, x => x.Select(c => c.FilePath)); _runtimeMinifier.CreateJsBundle( - UmbracoJsBundleName, + UmbracoExtensionsJsBundleName, true, FormatPaths( - GetScriptsForBackOffice( + GetScriptsForBackOfficeExtensions( propertyEditorAssets.TryGetValue(AssetType.Javascript, out var scripts) ? scripts : Enumerable.Empty()))); _runtimeMinifier.CreateCssBundle( @@ -89,12 +93,9 @@ namespace Umbraco.Web.WebAssets /// Returns scripts used to load the back office /// /// - private string[] GetScriptsForBackOffice(IEnumerable propertyEditorScripts) + private string[] GetScriptsForBackOfficeExtensions(IEnumerable propertyEditorScripts) { - var umbracoInit = GetInitBackOfficeScripts(); var scripts = new HashSet(); - foreach (var script in umbracoInit) - scripts.Add(script); foreach (var script in _parser.Manifest.Scripts) scripts.Add(script); foreach (var script in propertyEditorScripts) @@ -107,10 +108,10 @@ namespace Umbraco.Web.WebAssets /// Returns the list of scripts for back office initialization /// /// - private IEnumerable GetInitBackOfficeScripts() + private string[] GetScriptsForBackOfficeCore() { var resources = JsonConvert.DeserializeObject(Resources.JsInitialize); - return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()); + return resources.Where(x => x.Type == JTokenType.String).Select(x => x.ToString()).ToArray(); } /// diff --git a/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs b/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs index cc3be4d785..ec60f632d7 100644 --- a/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs +++ b/src/Umbraco.Infrastructure/WebAssets/RuntimeMinifierExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text; using System.Threading.Tasks; using Umbraco.Core.Configuration.Models; @@ -15,8 +16,9 @@ namespace Umbraco.Web.WebAssets /// public static async Task GetScriptForLoadingBackOfficeAsync(this IRuntimeMinifier minifier, GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment) { - var files = await minifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoJsBundleName); - var result = BackOfficeJavaScriptInitializer.GetJavascriptInitialization(files, "umbraco", globalSettings, hostingEnvironment); + var coreScripts = await minifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoCoreJsBundleName); + var extensionsScripts = await minifier.GetAssetPathsAsync(BackOfficeWebAssets.UmbracoExtensionsJsBundleName); + var result = BackOfficeJavaScriptInitializer.GetJavascriptInitialization(coreScripts.Union(extensionsScripts), "umbraco", globalSettings, hostingEnvironment); result += await GetStylesheetInitializationAsync(minifier); return result; diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs index 29a0e41998..d3e1be65b3 100644 --- a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs @@ -74,7 +74,7 @@ namespace Umbraco.Web.Common.RuntimeMinification public async Task RenderCssHereAsync(string bundleName) => (await _smidge.SmidgeHelper.CssHereAsync(bundleName, _hostingEnvironment.IsDebugMode)).ToString(); - public void CreateJsBundle(string bundleName, params string[] filePaths) + public void CreateJsBundle(string bundleName, bool optimizeOutput, params string[] filePaths) { if (filePaths.Any(f => !f.StartsWith("/") && !f.StartsWith("~/"))) throw new InvalidOperationException("All file paths must be absolute"); @@ -85,7 +85,8 @@ namespace Umbraco.Web.Common.RuntimeMinification // Here we could configure bundle options instead of using smidge's global defaults. // For example we can use our own custom cache buster for this bundle without having the global one // affect this or vice versa. - var bundle = _bundles.Create(bundleName, _jsPipeline.Value, WebFileType.Js, filePaths); + var pipeline = optimizeOutput ? _jsPipeline.Value : _bundles.PipelineFactory.Create(); + var bundle = _bundles.Create(bundleName, pipeline, WebFileType.Js, filePaths); } public async Task RenderJsHereAsync(string bundleName) => (await _smidge.SmidgeHelper.JsHereAsync(bundleName, _hostingEnvironment.IsDebugMode)).ToString();