Split backoffice core and extensions JS into two different bundles and disable minification for all core JS bundles

This commit is contained in:
Kenn Jacobsen
2021-02-18 10:07:09 +01:00
parent 2ccd4a61f4
commit cfa9adc87e
4 changed files with 21 additions and 16 deletions

View File

@@ -40,6 +40,7 @@ namespace Umbraco.Core.WebAssets
/// Creates a JS bundle
/// </summary>
/// <param name="bundleName"></param>
/// <param name="optimizeOutput"></param>
/// <param name="filePaths"></param>
/// <remarks>
/// All files must be absolute paths, relative paths will throw <see cref="InvalidOperationException"/>
@@ -47,7 +48,7 @@ namespace Umbraco.Core.WebAssets
/// <exception cref="InvalidOperationException">
/// Thrown if any of the paths specified are not absolute
/// </exception>
void CreateJsBundle(string bundleName, params string[] filePaths);
void CreateJsBundle(string bundleName, bool optimizeOutput, params string[] filePaths);
/// <summary>
/// Renders the html script tag for the bundle

View File

@@ -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<string>())));
_runtimeMinifier.CreateCssBundle(
@@ -89,12 +93,9 @@ namespace Umbraco.Web.WebAssets
/// Returns scripts used to load the back office
/// </summary>
/// <returns></returns>
private string[] GetScriptsForBackOffice(IEnumerable<string> propertyEditorScripts)
private string[] GetScriptsForBackOfficeExtensions(IEnumerable<string> propertyEditorScripts)
{
var umbracoInit = GetInitBackOfficeScripts();
var scripts = new HashSet<string>();
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
/// </summary>
/// <returns></returns>
private IEnumerable<string> GetInitBackOfficeScripts()
private string[] GetScriptsForBackOfficeCore()
{
var resources = JsonConvert.DeserializeObject<JArray>(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();
}
/// <summary>

View File

@@ -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
/// <returns></returns>
public static async Task<string> 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;

View File

@@ -74,7 +74,7 @@ namespace Umbraco.Web.Common.RuntimeMinification
public async Task<string> 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<string> RenderJsHereAsync(string bundleName) => (await _smidge.SmidgeHelper.JsHereAsync(bundleName, _hostingEnvironment.IsDebugMode)).ToString();