Adds bundle options to the package manifest
to more control over how bundling works for static file processing for app_plugins
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Hosting;
|
||||
using Umbraco.Cms.Core.Manifest;
|
||||
using Umbraco.Cms.Core.WebAssets;
|
||||
using Umbraco.Cms.Infrastructure.WebAssets;
|
||||
|
||||
namespace Umbraco.Extensions
|
||||
{
|
||||
public static class RuntimeMinifierExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the JavaScript to load the back office's assets
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> GetScriptForLoadingBackOfficeAsync(
|
||||
this IRuntimeMinifier minifier,
|
||||
GlobalSettings globalSettings,
|
||||
IHostingEnvironment hostingEnvironment,
|
||||
IManifestParser manifestParser,
|
||||
IUrlHelper urlHelper)
|
||||
{
|
||||
var files = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||
foreach(var file in await minifier.GetJsAssetPathsAsync(BackOfficeWebAssets.UmbracoCoreJsBundleName))
|
||||
{
|
||||
files.Add(file);
|
||||
}
|
||||
|
||||
foreach (var file in await minifier.GetJsAssetPathsAsync(BackOfficeWebAssets.UmbracoExtensionsJsBundleName))
|
||||
{
|
||||
files.Add(file);
|
||||
}
|
||||
|
||||
// process the independent bundles
|
||||
if (manifestParser.CombinedManifest.Scripts.TryGetValue(BundleOptions.Independent, out IReadOnlyList<ManifestAssets> independentManifestAssetsList))
|
||||
{
|
||||
foreach (ManifestAssets manifestAssets in independentManifestAssetsList)
|
||||
{
|
||||
var bundleName = BackOfficeWebAssets.GetIndependentPackageBundleName(manifestAssets, AssetType.Javascript);
|
||||
foreach(var asset in await minifier.GetJsAssetPathsAsync(bundleName))
|
||||
{
|
||||
files.Add(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// process the "None" bundles, meaning we'll just render the script as-is
|
||||
if (manifestParser.CombinedManifest.Scripts.TryGetValue(BundleOptions.None, out IReadOnlyList<ManifestAssets> noneManifestAssetsList))
|
||||
{
|
||||
foreach (ManifestAssets manifestAssets in noneManifestAssetsList)
|
||||
{
|
||||
foreach(var asset in manifestAssets.Assets)
|
||||
{
|
||||
files.Add(urlHelper.Content(asset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var result = BackOfficeJavaScriptInitializer.GetJavascriptInitialization(
|
||||
files,
|
||||
"umbraco",
|
||||
globalSettings,
|
||||
hostingEnvironment);
|
||||
|
||||
result += await GetStylesheetInitializationAsync(minifier, manifestParser, urlHelper);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the back office css bundle paths and formats a JS call to lazy load them
|
||||
/// </summary>
|
||||
private static async Task<string> GetStylesheetInitializationAsync(
|
||||
IRuntimeMinifier minifier,
|
||||
IManifestParser manifestParser,
|
||||
IUrlHelper urlHelper)
|
||||
{
|
||||
var files = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||
foreach(var file in await minifier.GetCssAssetPathsAsync(BackOfficeWebAssets.UmbracoCssBundleName))
|
||||
{
|
||||
files.Add(file);
|
||||
}
|
||||
|
||||
// process the independent bundles
|
||||
if (manifestParser.CombinedManifest.Stylesheets.TryGetValue(BundleOptions.Independent, out IReadOnlyList<ManifestAssets> independentManifestAssetsList))
|
||||
{
|
||||
foreach (ManifestAssets manifestAssets in independentManifestAssetsList)
|
||||
{
|
||||
var bundleName = BackOfficeWebAssets.GetIndependentPackageBundleName(manifestAssets, AssetType.Css);
|
||||
foreach (var asset in await minifier.GetCssAssetPathsAsync(bundleName))
|
||||
{
|
||||
files.Add(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// process the "None" bundles, meaning we'll just render the script as-is
|
||||
if (manifestParser.CombinedManifest.Stylesheets.TryGetValue(BundleOptions.None, out IReadOnlyList<ManifestAssets> noneManifestAssetsList))
|
||||
{
|
||||
foreach (ManifestAssets manifestAssets in noneManifestAssetsList)
|
||||
{
|
||||
foreach (var asset in manifestAssets.Assets)
|
||||
{
|
||||
files.Add(urlHelper.Content(asset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (string file in files)
|
||||
{
|
||||
sb.AppendFormat("{0}LazyLoad.css('{1}');", Environment.NewLine, file);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user